ReflectionUtils
in package
Just another place to put static functions.
..
Table of Contents
Methods
- castTypeSoftly() : mixed
- Cast the $value to the preferred $type (if we're fairly confident).
- findMethodHelpers() : iterable<string|int, mixed>
- Find any methods in this class which match the given prefix.
- findStandardProperties() : iterable<string|int, mixed>
- Get a list of standard properties which can be written+read by outside callers.
- getCodeDocs() : array<string|int, mixed>
- getTraits() : array<string|int, string>
- List all traits used by a class and its parents.
- isMethodDeprecated() : bool
- Check if a class method is deprecated
- parseDocBlock() : array<string|int, mixed>
- Parses a PHPDoc-style comment block into structured metadata.
- parseArrayShape() : array<string|int, mixed>
- Parses a complex PHPDoc array shape definition into a structured array.
- cleanLine() : string
Methods
castTypeSoftly()
Cast the $value to the preferred $type (if we're fairly confident).
public
static castTypeSoftly(mixed $value, array<string|int, mixed> $paramInfo) : mixed
This is like PHP's settype() but totally not. It only casts in narrow circumstances.
This reflects an opinion that some castings are better than others.
These will be converted:
cast('123', 'int') => 123 cast('123.4', 'float') => 123.4 cast('0', 'bool') => FALSE cast(1, 'bool') => TRUE
However, a string like 'hello' will never cast to bool, int, or float -- because that's a senseless request. We'll leave that to someone else to figure.
Parameters
- $value : mixed
- $paramInfo : array<string|int, mixed>
Return values
mixed —If the $value is agreeable to casting according to a type-rule from $paramInfo, then we return the converted value. Otherwise, return the original value.
findMethodHelpers()
Find any methods in this class which match the given prefix.
public
static findMethodHelpers(string $class, string $prefix) : iterable<string|int, mixed>
Parameters
- $class : string
- $prefix : string
Return values
iterable<string|int, mixed>findStandardProperties()
Get a list of standard properties which can be written+read by outside callers.
public
static findStandardProperties(string $class) : iterable<string|int, mixed>
Parameters
- $class : string
Return values
iterable<string|int, mixed>getCodeDocs()
public
static getCodeDocs(Reflector|ReflectionClass $reflection[, string $type = null ][, array<string|int, mixed> $vars = [] ]) : array<string|int, mixed>
Parameters
- $reflection : Reflector|ReflectionClass
- $type : string = null
-
If we are not reflecting the class itself, specify "Method", "Property", etc.
- $vars : array<string|int, mixed> = []
-
Variable substitutions to perform in the docblock
Return values
array<string|int, mixed>getTraits()
List all traits used by a class and its parents.
public
static getTraits(object|string $class) : array<string|int, string>
Parameters
- $class : object|string
Return values
array<string|int, string>isMethodDeprecated()
Check if a class method is deprecated
public
static isMethodDeprecated(string $className, string $methodName) : bool
Parameters
- $className : string
- $methodName : string
Tags
Return values
boolparseDocBlock()
Parses a PHPDoc-style comment block into structured metadata.
public
static parseDocBlock(string $comment) : array<string|int, mixed>
Supports array shapes in @param, @return, and @var, including multiline and nested array shapes.
Parameters
- $comment : string
-
The PHPDoc comment block.
Return values
array<string|int, mixed> —The structured parsed information.
parseArrayShape()
Parses a complex PHPDoc array shape definition into a structured array.
protected
static parseArrayShape(string $definition) : array<string|int, mixed>
Supports nested array shapes using recursion.
Parameters
- $definition : string
-
The array shape definition, e.g., 'array{foo: string, bar: array{baz: int}}'.
Return values
array<string|int, mixed> —A structured representation of the array shape.
cleanLine()
private
static cleanLine(string $line) : string
Parameters
- $line : string