{"record":{"id":"088b90bde6baf3a2","repo":"doctrine/annotations","slug":"couldn-t-find-constant-s-s","errorCode":null,"errorMessage":"Couldn't find constant %s%s.","messagePattern":"Couldn't find constant (.+?)(.+?)\\.","errorType":"exception","errorClass":"AnnotationException","httpStatus":null,"severity":"error","filePath":"lib/Doctrine/Common/Annotations/DocParser.php","lineNumber":1164,"sourceCode":"            }\n        }\n\n        /**\n         * Checks if identifier ends with ::class and remove the leading backslash if it exists.\n         */\n        if (\n            $this->identifierEndsWithClassConstant($identifier) &&\n            ! $this->identifierStartsWithBackslash($identifier)\n        ) {\n            return substr($identifier, 0, $this->getClassConstantPositionInIdentifier($identifier));\n        }\n\n        if ($this->identifierEndsWithClassConstant($identifier) && $this->identifierStartsWithBackslash($identifier)) {\n            return substr($identifier, 1, $this->getClassConstantPositionInIdentifier($identifier) - 1);\n        }\n\n        if (! defined($identifier)) {\n            throw AnnotationException::semanticalErrorConstants($identifier, $this->context);\n        }\n\n        return constant($identifier);\n    }\n\n    private function identifierStartsWithBackslash(string $identifier): bool\n    {\n        return $identifier[0] === '\\\\';\n    }\n\n    private function identifierEndsWithClassConstant(string $identifier): bool\n    {\n        return $this->getClassConstantPositionInIdentifier($identifier) === strlen($identifier) - strlen('::class');\n    }\n\n    /** @return int|false */\n    private function getClassConstantPositionInIdentifier(string $identifier)\n    {","sourceCodeStart":1146,"sourceCodeEnd":1182,"githubUrl":"https://github.com/doctrine/annotations/blob/17815fb6b2f4c3c701ba2abdf2c6c97b219fb2e4/lib/Doctrine/Common/Annotations/DocParser.php#L1146-L1182","documentation":"DocParser resolves PHP constants referenced inside annotation values (e.g. @Annotation(MyClass::STATUS)). When the parsed identifier is not a defined constant (defined() returns false), AnnotationException::semanticalErrorConstants throws \"Couldn't find constant %s\" including the annotation context. This guards against silently passing an undefined constant name, which plain PHP would only warn about and cast to a string.","triggerScenarios":"Referencing Class::CONSTANT or FQCN\\CONSTANT in an annotation where the class or constant does not exist or is not autoloadable; using an undefined global constant in an annotation value; referencing an enum case on PHP < 8.1 or an older doctrine/annotations version; typo in the constant name.","commonSituations":"Refactoring that renamed or removed a class constant while annotations still reference the old name; missing use imports so the identifier resolves to the wrong FQCN; constants defined conditionally via define() in bootstrap code not yet executed.","solutions":["Verify the constant exists and its name/case matches: check the defining class or define() call.","Add the correct use import so the identifier resolves to the right class, or use the fully qualified class name in the annotation.","Ensure the file defining the constant is autoloaded/required before the annotation is parsed (check bootstrap order).","If referencing an enum case, upgrade doctrine/annotations and PHP to versions that support enums, or use a plain scalar instead."],"exampleFix":"// before\n/** @Column(type=Types::STRNG) */\n// after\nuse Doctrine\\DBAL\\Types;\n/** @Column(type=Types::STRING) */","handlingStrategy":"try-catch","validationCode":"if (!class_exists(MyClass::class)) {\n    throw new LogicException('Class used in annotation not autoloadable');\n}\nif (!defined('MyClass::STATUS') && !defined('STATUS')) {\n    throw new LogicException('Constant referenced in annotation does not exist');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $annot = $reader->getClassAnnotation($class, MyAnnotation::class);\n} catch (AnnotationException $e) {\n    if (str_contains($e->getMessage(), \"Couldn't find constant\")) {\n        $defaultValue = 'fallback'; // or re-raise with more context\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Always import classes used in annotations with use statements.","Never reference constants defined conditionally after parsing begins.","Avoid using enum cases in annotations with older doctrine/annotations; use scalars.","Verify '::' references in docblocks in CI with a static checker (e.g. PHPStan doctrine extension)."],"tags":["php","doctrine-annotations","constants","undefined-constant"],"backgroundTag":"class-not-found","analyzedSha":"17815fb6b2f4c3c701ba2abdf2c6c97b219fb2e4","analyzedAt":"2026-09-15T20:30:41.547Z","contentChangedAt":"2026-09-15T20:30:41.547Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}