{"record":{"id":"4de32779a6a3310c","repo":"sebastianbergmann/phpunit","slug":"the-key-must-be-an-integer-or-a-string-s-given","errorCode":null,"errorMessage":"The key must be an integer or a string, %s given","messagePattern":"The key must be an integer or a string, (.+?) given","errorType":"exception","errorClass":"InvalidDataProviderException","httpStatus":null,"severity":"error","filePath":"src/Metadata/Api/DataProvider.php","lineNumber":182,"sourceCode":"                            'Data Provider method %s::%s() does not return an iterable',\n                            $className,\n                            $methodName,\n                        ),\n                    );\n                }\n            } catch (Throwable $e) {\n                Event\\Facade::emitter()->dataProviderMethodFinished(\n                    $testMethodValueObject,\n                    ...$methodsCalled,\n                );\n\n                throw InvalidDataProviderException::forException($e, $providerLabel);\n            }\n\n            try {\n                foreach ($data as $key => $value) {\n                    if (!is_int($key) && !is_string($key)) {\n                        throw new InvalidDataProviderException(\n                            sprintf(\n                                'The key must be an integer or a string, %s given',\n                                get_debug_type($key),\n                            ),\n                        );\n                    }\n\n                    if ($key === '') {\n                        throw new InvalidDataProviderException(\n                            'The key must not be an empty string',\n                        );\n                    }\n\n                    if (!is_array($value)) {\n                        throw new InvalidDataProviderException(\n                            sprintf(\n                                'Data set %s provided by %s is invalid, expected array but got %s',\n                                $this->formatKey($key),","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/Metadata/Api/DataProvider.php#L164-L200","documentation":"Thrown while iterating a provider's returned data when a key is neither an integer nor a string (get_debug_type() names the actual type in the message). PHPUnit uses each key as the dataset name in test IDs and output, so arrays keyed by, e.g., floats or objects are rejected.","triggerScenarios":"A provider returning [0.5 => [...]] or [$objectKey => [...]] (object/array keys from array_fill_keys or dynamic maps); generators yielding non-scalar keys.","commonSituations":"Providers built from array_combine with float/string-cast values; spreads of enums-as-keys; data derived from JSON with unexpected key types.","solutions":["Ensure every dataset key is an int or a non-empty string; cast keys: [(string) $id => $row]","Rebuild the provider with array_values() or a foreach that assigns sequential integer keys","Inspect the provider output (var_export) to find the offending key of the reported type"],"exampleFix":"// before\npublic static function provideCases(): array\n{\n    $rows = $repository->all(); // keys are float ids\n    return $rows;\n}\n\n// after\npublic static function provideCases(): array\n{\n    $cases = [];\n\n    foreach ($repository->all() as $id => $row) {\n        $cases['row ' . $id] = $row;\n    }\n\n    return $cases;\n}","handlingStrategy":"validation","validationCode":"// Normalize keys before returning from a provider\n$cases = [];\n\nforeach ($raw as $key => $value) {\n    if (!is_int($key) && !is_string($key)) {\n        $key = 'case ' . count($cases);\n    }\n\n    $cases[$key] = $value;\n}\n\nreturn $cases;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Cast dynamic keys to string when building providers: [(string) $id => $row]","Use array_values($data) when keys carry no meaning","Inspect provider output with var_export() when adding non-standard key sources"],"tags":["phpunit","data-provider","array-key","type-validation"],"backgroundTag":"invalid-data-provider-key","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}