TypeReflector
Raxos\Reflection\TypeReflector
Represents and validates a resolved PHP type. It normalizes a native reflection type, a reflector or a plain type string into a single definition, then answers questions about it: what kind of type it is, whether it accepts a given value, and how it relates to other types. It handles builtins, classes, interfaces, enums, unions and intersections.
final readonly class TypeReflector implements ReflectorInterfaceThe class implements the ReflectorInterface contract from raxos/contract.
Properties
bool $isNullable: whether the type allowsnull. It is also available throughisNullable().
Methods
__construct()
public function __construct(Reflector|ReflectionType|string $type)Creates a reflector from a native ReflectionType, a ReflectionParameter, a ReflectionProperty, a ReflectionClass, any other Reflector, or a plain type string.
accepts()
public function accepts(mixed $input): boolChecks whether a runtime value matches the type, including builtins, classes, iterables, unions and intersections.
matches()
public function matches(string $type): boolChecks whether the type is the same as, or a subtype of, the given class or interface name.
class()
public function class(): ClassReflectorReturns a ClassReflector for the type, when it names a class.
equals()
public function equals(string|self $type): boolChecks whether two types have the same definition.
isBuiltIn()
public function isBuiltIn(): boolChecks whether the type is a PHP builtin such as string, int or array.
isNullable()
public function isNullable(): boolChecks whether the type allows null.
isScalar()
public function isScalar(): boolChecks whether the type is bool, float, int or string.
isClass()
public function isClass(): boolChecks whether the type names an existing class. Interfaces are not considered classes here.
isInterface()
public function isInterface(): boolChecks whether the type names an existing interface.
isEnum()
public function isEnum(): boolChecks whether the type is a unit or backed enum.
isBackedEnum()
public function isBackedEnum(): boolChecks whether the type is a backed enum.
isUnitEnum()
public function isUnitEnum(): boolChecks whether the type is a unit enum. This is true for any enum.
isIterable()
public function isIterable(): boolChecks whether the type is iterable: array, iterable, Generator or any Iterator.
isStringable()
public function isStringable(): boolChecks whether the type is a string or implements Stringable.
split()
public function split(): arraySplits a union or intersection type into its individual TypeReflector parts.
getName()
public function getName(): stringReturns the raw type definition.
getShortName()
public function getShortName(): stringReturns the type name without its namespace.
Usage
<?php
declare(strict_types=1);
use Raxos\Reflection\TypeReflector;
$type = new TypeReflector('int|string');
$type->accepts(42); // true
$type->accepts('foo'); // true
$type->accepts(3.14); // false
foreach ($type->split() as $part) {
echo $part->getName() . "\n"; // int, string
}