{"record":{"id":"4bc25750f7608745","repo":"sebastianbergmann/phpunit","slug":"the-name-s-is-already-in-use","errorCode":null,"errorMessage":"The name \"%s\" is already in use","messagePattern":"The name \"(.+?)\" is already in use","errorType":"exception","errorClass":"NameAlreadyInUseException","httpStatus":null,"severity":"error","filePath":"src/Framework/MockObject/Generator/Generator.php","lineNumber":751,"sourceCode":"        if (preg_match('~\\A[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*\\z~', $className) === 0) {\n            throw new InvalidClassNameException($className);\n        }\n    }\n\n    /**\n     * @throws NameAlreadyInUseException\n     * @throws ReflectionException\n     */\n    private function ensureNameForTestDoubleClassIsAvailable(string $className): void\n    {\n        if ($className === '') {\n            return;\n        }\n\n        if (class_exists($className, false) ||\n            interface_exists($className, false) ||\n            trait_exists($className, false)) {\n            throw new NameAlreadyInUseException($className);\n        }\n    }\n\n    /**\n     * @template T of object\n     *\n     * @param class-string<T> $className\n     *\n     * @throws ReflectionException\n     *\n     * @return ReflectionClass<T>\n     *\n     * @phpstan-ignore throws.unusedType\n     */\n    private function reflectClass(string $className): ReflectionClass\n    {\n        try {\n            $class = new ReflectionClass($className);","sourceCodeStart":733,"sourceCodeEnd":769,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/Framework/MockObject/Generator/Generator.php#L733-L769","documentation":"Thrown by Generator::ensureNameForTestDoubleClassIsAvailable() when a class, interface, or trait with the requested mock class name is already declared. The check uses class_exists/interface_exists/trait_exists with autoload disabled, so it only sees symbols already loaded in the current PHP process. Its purpose is to prevent a fatal 'cannot redeclare class' during eval() of the generated double.","triggerScenarios":"setMockClassName('SomeExistingClass') where that class is already loaded; creating two doubles with the same explicit mock class name in one test run (e.g. from a loop or a shared helper); a generated name that collides with a class your bootstrap or another test loaded.","commonSituations":"Test helpers that hardcode a mock class name and are called more than once per process; data-provider loops creating mocks with a fixed name; parallel tests sharing a process with preloaded classes; long-running test suites where earlier generated classes remain declared.","solutions":["Stop reusing fixed names: omit setMockClassName() so PHPUnit generates unique names per double.","If a name is required, make it unique per call, e.g. setMockClassName('UserRepoMock_' . uniqid()).","Rename the mock so it cannot collide with real classes; avoid names of classes loaded by your autoloader or bootstrap."],"exampleFix":"// before\nforeach ($ids as $id) {\n    $mocks[] = $this->getMockBuilder(Repo::class)\n        ->setMockClassName('RepoMock')\n        ->getMock();\n}\n\n// after\nforeach ($ids as $id) {\n    $mocks[] = $this->getMockBuilder(Repo::class)\n        ->setMockClassName('RepoMock_' . $id)\n        ->getMock();\n}","handlingStrategy":"validation","validationCode":"$name = 'RepoMock_' . uniqid();\nwhile (class_exists($name, false) || interface_exists($name, false) || trait_exists($name, false)) {\n    $name .= '_';\n}","typeGuard":"function classNameAvailable(string $name): bool\n{\n    return !class_exists($name, false)\n        && !interface_exists($name, false)\n        && !trait_exists($name, false);\n}","tryCatchPattern":"try {\n    $mock = $builder->setMockClassName($name)->getMock();\n} catch (PHPUnit\\Framework\\MockObject\\NameAlreadyInUseException $e) {\n    // regenerate a unique name and rebuild the double\n}","preventionTips":["Never use one fixed mock class name for doubles created in loops or helpers.","Suffix fixed names with uniqid() or the test's name when a name is necessary.","Avoid names resembling real autoloadable classes."],"tags":["phpunit","mock-object","class-name","name-collision","redeclare"],"backgroundTag":"duplicate-class-name","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}