{"record":{"id":"bc3267692bbdcc02","repo":"doctrine/instantiator","slug":"the-provided-class-s-does-not-exist","errorCode":null,"errorMessage":"The provided class \"%s\" does not exist","messagePattern":"The provided class \"(.+?)\" does not exist","errorType":"exception","errorClass":"Doctrine\\Instantiator\\Exception\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Exception/InvalidArgumentException.php","lineNumber":29,"sourceCode":"use function sprintf;\nuse function trait_exists;\n\n/**\n * Exception for invalid arguments provided to the instantiator\n */\nclass InvalidArgumentException extends BaseInvalidArgumentException implements ExceptionInterface\n{\n    public static function fromNonExistingClass(string $className): self\n    {\n        if (interface_exists($className)) {\n            return new self(sprintf('The provided type \"%s\" is an interface, and cannot be instantiated', $className));\n        }\n\n        if (trait_exists($className)) {\n            return new self(sprintf('The provided type \"%s\" is a trait, and cannot be instantiated', $className));\n        }\n\n        return new self(sprintf('The provided class \"%s\" does not exist', $className));\n    }\n\n    /**\n     * @phpstan-param ReflectionClass<T> $reflectionClass\n     *\n     * @template T of object\n     */\n    public static function fromAbstractClass(ReflectionClass $reflectionClass): self\n    {\n        return new self(sprintf(\n            'The provided class \"%s\" is abstract, and cannot be instantiated',\n            $reflectionClass->getName(),\n        ));\n    }\n\n    public static function fromEnum(string $className): self\n    {\n        return new self(sprintf(","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/doctrine/instantiator/blob/cbb879d6eea7ce605d95f4c455679df73ee682ea/src/Exception/InvalidArgumentException.php#L11-L47","documentation":"This is the fallback branch of fromNonExistingClass(): the given string is not a class, not an interface, and not a trait, so PHP simply does not know the type. It is thrown from getReflectionClass() (src/Instantiator.php:143-144) the moment class_exists($className) returns false, before any reflection happens. In practice it almost always means a typo in the class name or a broken autoloading setup, not a problem inside the library.","triggerScenarios":"Calling (new Instantiator())->instantiate($name) with a misspelled FQCN string ('App\\Servce\\Mailer'), a class whose file is not autoloadable (missing composer PSR-4 mapping, stale vendor/autoload after moving files), or a dynamically built name (concatenated namespace + basename) that does not match any declared class. class_exists() returns false and the 'does not exist' variant is thrown.","commonSituations":"Typo in a service alias in YAML/XML container config; composer autoload maps are stale after a directory rename or namespace change (fix with composer dump-autoload); the class is defined conditionally inside an if (class_exists(...)) block or by an extension that is not loaded; the class name string lost or gained a leading backslash during string manipulation.","solutions":["Check the class name for typos and exact namespace matches; prefer the ClassName::class constant over hand-built strings.","Run composer dump-autoload and verify the PSR-4 mapping in composer.json matches the file path of the class.","If the name is user/env-supplied, validate it early with class_exists($name) and reject unknown values with a descriptive error at the config boundary."],"exampleFix":"// before\n$object = $instantiator->instantiate('App\\\\Service\\\\MailServce'); // typo\n// InvalidArgumentException: The provided class \"App\\Service\\MailServce\" does not exist\n\n// after: use the ::class constant so the IDE and static analysis catch mistakes\nuse App\\Service\\MailService;\n$object = $instantiator->instantiate(MailService::class);","handlingStrategy":"validation","validationCode":"use function class_exists;\n\nif (! class_exists($className)) {\n    throw new RuntimeException(sprintf('Unknown class \"%s\": check spelling and run composer dump-autoload.', $className));\n}\n\n$object = (new Instantiator())->instantiate($className);","typeGuard":"function isKnownClassName(string $className): bool\n{\n    return class_exists($className); // false for missing classes, interfaces, traits\n}","tryCatchPattern":"use Doctrine\\Instantiator\\Exception\\InvalidArgumentException;\n\ntry {\n    $instance = $instantiator->instantiate($className);\n} catch (InvalidArgumentException $e) {\n    if (str_contains($e->getMessage(), 'does not exist')) {\n        // typo or autoload failure — fail loudly with the offending name\n        throw new ClassNotFoundException($e->getMessage(), 0, $e);\n    }\n    throw $e;\n}","preventionTips":["Use ClassName::class constants instead of string literals for class names.","Run composer dump-autoload after moving or renaming files/namespaces.","Validate user- or config-supplied class names with class_exists() at the input boundary, not deep in factory code."],"tags":["php","class-not-found","autoloading","typo","composer"],"backgroundTag":"class-not-found","analyzedSha":"cbb879d6eea7ce605d95f4c455679df73ee682ea","analyzedAt":"2026-08-21T03:06:19.624Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}