{"record":{"id":"8189907362e29ae4","repo":"sebastianbergmann/phpunit","slug":"class-or-interface-s-does-not-exist-818990","errorCode":null,"errorMessage":"Class or interface \"%s\" does not exist","messagePattern":"Class or interface \"(.+?)\" does not exist","errorType":"exception","errorClass":"UnknownClassOrInterfaceException","httpStatus":null,"severity":"error","filePath":"src/Framework/Constraint/Type/IsInstanceOf.php","lineNumber":42,"sourceCode":"     */\n    private readonly string $name;\n\n    /**\n     * @var 'class'|'interface'\n     */\n    private readonly string $type;\n\n    /**\n     * @throws UnknownClassOrInterfaceException\n     */\n    public function __construct(string $name)\n    {\n        if (class_exists($name)) {\n            $this->type = 'class';\n        } elseif (interface_exists($name)) {\n            $this->type = 'interface';\n        } else {\n            throw new UnknownClassOrInterfaceException($name);\n        }\n\n        $this->name = $name;\n    }\n\n    /**\n     * Returns a string representation of the constraint.\n     */\n    public function toString(): string\n    {\n        return sprintf(\n            'is an instance of %s %s',\n            $this->type,\n            $this->name,\n        );\n    }\n\n    /**","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/Framework/Constraint/Type/IsInstanceOf.php#L24-L60","documentation":"The IsInstanceOf constraint is constructed with a class or interface name, and its constructor validates immediately that the name refers to an existing class or interface via class_exists()/interface_exists(). If neither exists, this exception is thrown at constraint construction time — before any value is even evaluated.","triggerScenarios":"assertInstanceOf(SomeClass::class, $object) or createConfiguredMock/isInstanceOf checks where SomeClass is misspelled, not imported (missing use statement), namespaced incorrectly, or lives in an autoloader path not loaded in the test run; also assertInstanceOf('App\\Service\\UserRepo') with wrong casing/backslashes, or referencing a class from a package that is not installed.","commonSituations":"Missing `use App\\Domain\\User;` after copy-pasting a test; refactors that renamed/moved classes while tests still reference old names; running tests before composer dump-autoload; typo in string class names passed dynamically; dev dependencies holding the class not installed in CI.","solutions":["Fix the reference: add the missing use statement or correct the fully-qualified name — prefer SomeClass::class over hand-written strings so IDE/refactors catch it","Run composer dump-autoload / verify the package providing the class is installed (composer show)"],"exampleFix":"// before\nuse App\\Domain\\Userr; // typo / missing import\n$this->assertInstanceOf(Userr::class, $user);\n\n// after\nuse App\\Domain\\User;\n$this->assertInstanceOf(User::class, $user);","handlingStrategy":"validation","validationCode":"if (!class_exists($className) && !interface_exists($className)) {\n    $this->markTestSkipped(\"Unknown class/interface: $className\");\n}\n$this->assertInstanceOf($className, $object);","typeGuard":"function isKnownClassOrInterface(string $name): bool\n{\n    return class_exists($name) || interface_exists($name);\n}","tryCatchPattern":null,"preventionTips":["Always use SomeClass::class instead of string literals in assertInstanceOf()","Run composer dump-autoload after adding classes; keep test imports tidy with an IDE/lint rule"],"tags":["php","phpunit","assertinstanceof","class-not-found","autoloading"],"backgroundTag":"class-not-found","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}