web-fu / reflection
Reflection API
Installs: 1 411
Dependents: 3
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 1
Requires
- php: ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0
Requires (Dev)
- ext-mbstring: *
- friendsofphp/php-cs-fixer: ^3.13
- infection/infection: ^0.26.16
- phpbench/phpbench: ^1.2
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^5.4
This package is auto-updated.
Last update: 2024-11-14 10:58:30 UTC
README
This library is a type safe wrapper for PHP Reflection API.
This library is born with the purpose to solve the problem of type safety in PHP Reflection API. Reflection API is a very powerful tool, but presents some issues.
For example:
- the original
ReflectionClass::getConstant
returnfalse
if the constant does not exist or if the constant is equal tofalse
. ReflectionClass::newInstance
return a generic object, but it is possible to know the type of the object.- New interfaces are added to the Reflection API in different PHP versions, so it is not possible to use them in a cross-version way.
Installation
web-fu/reflection is available on Packagist and can be installed using Composer.
composer require web-fu/reflection
Requires PHP 8.0 or newer.
Usage
This wrapper try to use the same names of the original Reflection API, but with a different namespace.
<?php require_once __DIR__ . '/vendor/autoload.php'; use WebFu\Reflection\ReflectionClass; use MyNamespace\MyClass; $reflection = new ReflectionClass(MyClass::class); echo $reflection->getName(); // MyNamespace\MyClass echo $reflection->getShortName(); // MyClass
Type management
PHP Reflection API use different classes to manage types: ReflectionType
, ReflectionNamedType
and ReflectionUnionType
.
I created a single class to manage all types: WebFu\Reflection\ReflectionType
.
I added a helper function to infer the PHPDocType, if specified
<?php require_once __DIR__ . '/vendor/autoload.php'; use WebFu\Reflection\ReflectionClass; class ClassWithTypes { public int $simple; public int|string $union; public $noType; public ?int $nullable; /** @var class-string */ public string $className; } $reflection = new ReflectionClass(MyClass::class); echo $reflection->getProperty('simple')->getType()->getTypeNames(); // ['int'] echo $reflection->getProperty('union')->getType()->getTypeNames(); // ['int','string'] echo $reflection->getProperty('noType')->getType()->getTypeNames(); // ['mixed'] echo $reflection->getProperty('nullable')->getType()->getTypeNames(); // ['int','null'] echo $reflection->getProperty('nullable')->getType()->getPhpDocTypeNames(); // ['class-string']