Class Parser
Discover PHP attributes using tokenized declarations and reflection.
Method Summary
-
__construct() public
-
extractAttributeArguments() protected
Extract named arguments from a reflection attribute.
-
getAttributeConstructor() protected
Get the constructor for an attribute class (cached).
-
getClassNamesFromTokens() protected
Extract class name from PHP file using token parsing.
Leverages PSR-4 constraint: one class per file. Returns immediately when the first class/interface/trait/enum is found.
-
getClassesFromFile() protected
Extract class names from a PHP file.
Uses token parsing to safely detect classes, interfaces, traits, and enums. Then loads them either via autoloader or direct file inclusion.
-
getDeclaringClassType() protected
Detect declaring class kind from reflection.
-
getMethodVisibility() protected
Detect method visibility from reflection metadata.
-
isAnonymousClass() protected
Check if T_CLASS token represents an anonymous class.
Anonymous classes are preceded by the 'new' keyword.
-
isTypeLoaded() protected
Check if a type (class/interface/trait/enum) is already loaded.
-
loadType() protected
Try to load a type via autoloader.
-
nextSignificantTokenId() protected
Find the next token ID, skipping whitespace and comments.
-
parseAttributes() protected
Parse reflection attributes and convert to AttributeInfo.
-
parseClass() protected
Parse attributes from a class and its members.
-
parseConstant() protected
Parse class constant attributes.
-
parseFile() public
Parse a PHP file and extract all attributes.
-
parseMethod() protected
Parse method attributes.
-
parseParameter() protected
Parse parameter attributes.
-
parseProperty() protected
Parse property attributes.
-
previousSignificantTokenId() protected
Find the previous token ID, skipping whitespace and comments.
-
shouldExclude() protected
Check if an attribute should be excluded.
Method Detail
__construct() ¶ public
__construct(array<string> $excludeAttributes = [])
Parameters
-
array<string>$excludeAttributes optional
extractAttributeArguments() ¶ protected
extractAttributeArguments(ReflectionAttribute<object> $attribute): array<string, mixed>
Extract named arguments from a reflection attribute.
Parameters
-
ReflectionAttribute<object>$attribute Reflection attribute
Returns
array<string, mixed>Named arguments array
getAttributeConstructor() ¶ protected
getAttributeConstructor(string $attributeName): ReflectionMethod|null
Get the constructor for an attribute class (cached).
Parameters
-
string$attributeName Attribute class name
Returns
ReflectionMethod|nullConstructor or null if none exists
getClassNamesFromTokens() ¶ protected
getClassNamesFromTokens(string $filePath): array<string>
Extract class name from PHP file using token parsing.
Leverages PSR-4 constraint: one class per file. Returns immediately when the first class/interface/trait/enum is found.
Parameters
-
string$filePath File path to parse
Returns
array<string>Fully qualified class name (single element array)
getClassesFromFile() ¶ protected
getClassesFromFile(string $filePath): array<string>
Extract class names from a PHP file.
Uses token parsing to safely detect classes, interfaces, traits, and enums. Then loads them either via autoloader or direct file inclusion.
Parameters
-
string$filePath File path (should be normalized with realpath)
Returns
array<string>getDeclaringClassType() ¶ protected
getDeclaringClassType(ReflectionClass<object> $reflection): Cake\AttributeResolver\Enum\DeclaringClassType
Detect declaring class kind from reflection.
Parameters
-
ReflectionClass<object>$reflection Class reflection
Returns
Cake\AttributeResolver\Enum\DeclaringClassTypegetMethodVisibility() ¶ protected
getMethodVisibility(ReflectionMethod $method): Cake\AttributeResolver\Enum\MethodVisibility
Detect method visibility from reflection metadata.
Parameters
-
ReflectionMethod$method Method reflection
Returns
Cake\AttributeResolver\Enum\MethodVisibilityisAnonymousClass() ¶ protected
isAnonymousClass(array<PhpToken> $tokens, int $currentIndex): bool
Check if T_CLASS token represents an anonymous class.
Anonymous classes are preceded by the 'new' keyword.
Parameters
-
array<PhpToken>$tokens All tokens from PhpToken::tokenize()
-
int$currentIndex Current token index
Returns
boolTrue if anonymous class
isTypeLoaded() ¶ protected
isTypeLoaded(string $typeName): bool
Check if a type (class/interface/trait/enum) is already loaded.
Parameters
-
string$typeName Type name to check
Returns
boolTrue if type is loaded
loadType() ¶ protected
loadType(string $typeName): bool
Try to load a type via autoloader.
Parameters
-
string$typeName Type name to load
Returns
boolTrue if type was loaded
nextSignificantTokenId() ¶ protected
nextSignificantTokenId(array<PhpToken> $tokens, int $index): int|null
Find the next token ID, skipping whitespace and comments.
Parameters
-
array<PhpToken>$tokens Tokens to inspect
-
int$index Current token index
Returns
int|nullSignificant token ID, or null at the end of the stream
parseAttributes() ¶ protected
parseAttributes(array<ReflectionAttribute<object>> $attributes, string $className, string $filePath, int $lineNumber, int $fileTime, Cake\AttributeResolver\ValueObject\AttributeTarget $target, string|null $pluginName): Generator<Cake\AttributeResolver\ValueObject\AttributeInfo>
Parse reflection attributes and convert to AttributeInfo.
Parameters
-
array<ReflectionAttribute<object>>$attributes Reflection attributes
-
string$className Class name
-
string$filePath File path
-
int$lineNumber Line number
-
int$fileTime File modification time
-
Cake\AttributeResolver\ValueObject\AttributeTarget$target Attribute target
-
string|null$pluginName Plugin name
Returns
Generator<Cake\AttributeResolver\ValueObject\AttributeInfo>parseClass() ¶ protected
parseClass(ReflectionClass<object> $reflection, string $filePath, int $fileTime, string|null $pluginName): Generator<Cake\AttributeResolver\ValueObject\AttributeInfo>
Parse attributes from a class and its members.
Parameters
-
ReflectionClass<object>$reflection Class reflection
-
string$filePath File path
-
int$fileTime File modification time
-
string|null$pluginName Plugin name
Returns
Generator<Cake\AttributeResolver\ValueObject\AttributeInfo>parseConstant() ¶ protected
parseConstant(ReflectionClassConstant $constant, string $filePath, int $fileTime, string $className, bool $isDeclaringClassAbstract, Cake\AttributeResolver\Enum\DeclaringClassType $declaringClassType, string|null $pluginName): Generator<Cake\AttributeResolver\ValueObject\AttributeInfo>
Parse class constant attributes.
Parameters
-
ReflectionClassConstant$constant Constant reflection
-
string$filePath File path
-
int$fileTime File modification time
-
string$className Declaring class name
-
bool$isDeclaringClassAbstract Whether the declaring class is abstract
-
Cake\AttributeResolver\Enum\DeclaringClassType$declaringClassType Declaring class kind
-
string|null$pluginName Plugin name
Returns
Generator<Cake\AttributeResolver\ValueObject\AttributeInfo>parseFile() ¶ public
parseFile(SplFileInfo $file, string|null $pluginName = null): Generator<Cake\AttributeResolver\ValueObject\AttributeInfo>
Parse a PHP file and extract all attributes.
Parameters
-
SplFileInfo$file File object to parse
-
string|null$pluginName optional Plugin name if file belongs to a plugin
Returns
Generator<Cake\AttributeResolver\ValueObject\AttributeInfo>parseMethod() ¶ protected
parseMethod(ReflectionMethod $method, string $filePath, int $fileTime, string $className, bool $isDeclaringClassAbstract, Cake\AttributeResolver\Enum\DeclaringClassType $declaringClassType, string|null $pluginName): Generator<Cake\AttributeResolver\ValueObject\AttributeInfo>
Parse method attributes.
Parameters
-
ReflectionMethod$method Method reflection
-
string$filePath File path
-
int$fileTime File modification time
-
string$className Declaring class name
-
bool$isDeclaringClassAbstract Whether the declaring class is abstract
-
Cake\AttributeResolver\Enum\DeclaringClassType$declaringClassType Declaring class kind
-
string|null$pluginName Plugin name
Returns
Generator<Cake\AttributeResolver\ValueObject\AttributeInfo>parseParameter() ¶ protected
parseParameter(ReflectionParameter $parameter, string $filePath, int $fileTime, string $className, string $methodName, bool $isDeclaringClassAbstract, Cake\AttributeResolver\Enum\DeclaringClassType $declaringClassType, Cake\AttributeResolver\Enum\MethodVisibility $methodVisibility, string|null $pluginName): Generator<Cake\AttributeResolver\ValueObject\AttributeInfo>
Parse parameter attributes.
Parameters
-
ReflectionParameter$parameter Parameter reflection
-
string$filePath File path
-
int$fileTime File modification time
-
string$className Declaring class name
-
string$methodName Method name
-
bool$isDeclaringClassAbstract Whether the declaring class is abstract
-
Cake\AttributeResolver\Enum\DeclaringClassType$declaringClassType Declaring class kind
-
Cake\AttributeResolver\Enum\MethodVisibility$methodVisibility Method visibility
-
string|null$pluginName Plugin name
Returns
Generator<Cake\AttributeResolver\ValueObject\AttributeInfo>parseProperty() ¶ protected
parseProperty(ReflectionProperty $property, string $filePath, int $fileTime, string $className, bool $isDeclaringClassAbstract, Cake\AttributeResolver\Enum\DeclaringClassType $declaringClassType, string|null $pluginName): Generator<Cake\AttributeResolver\ValueObject\AttributeInfo>
Parse property attributes.
Parameters
-
ReflectionProperty$property Property reflection
-
string$filePath File path
-
int$fileTime File modification time
-
string$className Declaring class name
-
bool$isDeclaringClassAbstract Whether the declaring class is abstract
-
Cake\AttributeResolver\Enum\DeclaringClassType$declaringClassType Declaring class kind
-
string|null$pluginName Plugin name
Returns
Generator<Cake\AttributeResolver\ValueObject\AttributeInfo>previousSignificantTokenId() ¶ protected
previousSignificantTokenId(array<PhpToken> $tokens, int $index): int|null
Find the previous token ID, skipping whitespace and comments.
Parameters
-
array<PhpToken>$tokens Tokens to inspect
-
int$index Current token index
Returns
int|nullSignificant token ID, or null at the start of the stream
shouldExclude() ¶ protected
shouldExclude(string $attributeName): bool
Check if an attribute should be excluded.
Parameters
-
string$attributeName Attribute name
Returns
boolTrue if should be excluded