{"record":{"id":"edd3ba4296dd8af3","repo":"mockery/mockery","slug":"could-not-load-mock-s-class-already-exists","errorCode":null,"errorMessage":"Could not load mock %s, class already exists","messagePattern":"Could not load mock (.+?), class already exists","errorType":"exception","errorClass":"Mockery\\Exception\\RuntimeException","httpStatus":null,"severity":"error","filePath":"library/Mockery/Container.php","lineNumber":572,"sourceCode":"    }\n\n    /**\n     * @return class-string\n     *\n     * @throws Throwable\n     */\n    private function generateMock(MockConfiguration $mockConfiguration): string\n    {\n        $mockDefinition = $this->getGenerator()\n            ->generate($mockConfiguration);\n\n        $className = $mockDefinition->getClassName();\n\n        if (class_exists($className, $attemptAutoload = false)) {\n            $reflectionClass = new ReflectionClass($className);\n\n            if (! $reflectionClass->implementsInterface(LegacyMockInterface::class)) {\n                throw new RuntimeException(sprintf('Could not load mock %s, class already exists', $className));\n            }\n        }\n\n        $this->getLoader()\n            ->load($mockDefinition);\n\n        return $className;\n    }\n\n    /**\n     * @return null|Closure(MockInterface):void\n     */\n    private function handleClosure(array &$arguments): ?Closure\n    {\n        if (count($arguments) < 2) {\n            return null;\n        }\n","sourceCodeStart":554,"sourceCodeEnd":590,"githubUrl":"https://github.com/mockery/mockery/blob/c6401d35bcd28bd997382f2dae3545355ee77865/library/Mockery/Container.php#L554-L590","documentation":"After the generator produces a mock class definition, Container::generateMock() checks whether that class name already exists (without triggering autoload). If it exists and does not implement LegacyMockInterface - meaning it is a real pre-existing class, not a Mockery-generated mock - Mockery refuses to redefine it and throws RuntimeException.","triggerScenarios":"Mockery::mock('overload:App\\Service') when App\\Service has already been autoloaded earlier in the process; Mockery::namedMock('SomeRealClass') where a class with that name exists; creating the same overloaded mock twice in one process (e.g. between tests without process isolation).","commonSituations":"Overload (instance-mock) tests in long-running suites (Paratest, Laravel) where the real class got loaded by another test first; mocks created in PHPUnit data providers; mock names chosen to equal real class names.","solutions":["Create the overload mock before anything autoloads the real class, and isolate such tests with @runInSeparateProcess + @preserveGlobalState disabled","Prefer passing an instance (Mockery::mock(new Service())) over 'overload:' where possible","Choose named-mock names that never collide with real class names"],"exampleFix":"// before\nuse App\\Mailer; // real class now loaded\n$mailer = Mockery::mock('overload:App\\Mailer'); // throws: class already exists\n\n// after\n/**\n * @runInSeparateProcess\n * @preserveGlobalState disabled\n */\npublic function testMailSending(): void {\n    $mailer = Mockery::mock('overload:App\\Mailer'); // created before autoloader loads the class\n    // ...\n}","handlingStrategy":"validation","validationCode":"// Before an overload mock, ensure the real class is not loaded yet:\nif (class_exists($targetClass, false)) {\n    throw new RuntimeException($targetClass . ' already loaded; overload mock impossible in this process');\n}\n$mock = Mockery::mock('overload:' . $targetClass);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Isolate overload tests with @runInSeparateProcess and @preserveGlobalState disabled","Create the overload mock before anything can autoload the real class (no use statements pulling it in earlier)","Avoid mock names equal to real class names; prefer instance mocks (passing an object) over 'overload:'"],"tags":["mockery","php","class-collision","overload-mock","process-isolation"],"backgroundTag":"class-already-defined","analyzedSha":"c6401d35bcd28bd997382f2dae3545355ee77865","analyzedAt":"2026-08-21T04:45:00.060Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}