Installation
-
Install the reflection:
Terminal window composer require good-php/reflection -
Build a reflector:
use GoodPhp\Reflection\ReflectorBuilder;$reflector = new ReflectorBuilder()->withFileCache()->withMemoryCache()->build();
PHPStan extension
Section titled “PHPStan extension”A PHPStan extension is shipped with the package. If you’re using phpstan/extension-installer, it will be
enabled automatically. We recommend keeping it on, but it’s not required.
If it’s causing you any issues or you just don’t want it, you can disable it through your composer.json:
{ "extra": { "phpstan/extension-installer": { "ignore": ["good-php/reflection"] } }}If you aren’t using phpstan/extension-installer
Section titled “If you aren’t using phpstan/extension-installer”You can, of course, enable it manually in your phpstan.neon:
includes: - vendor/good-php/reflection/phpstan-extension.neonCustomizing cache
Section titled “Customizing cache”It’s crucial that you use some kind of cache. Out of the box, we support file-based cache and in-memory cache. They work together - file cache allows faster startup times, while in-memory cache allows accessing the same reflections repeatedly during the PHP process lifetime.
By default, ->withFileCache() stores it’s cache in /tmp/good-php-reflection without any expiration. This
is configurable:
new ReflectorBuilder() ->withFileCache( path: '/other/directory', ttl: \DateInterval::createFromDateString('1 hour') );There are also defaults for ->withMemoryCache() - it stores at most 100 types, again without any expiration.
Configure it with:
new ReflectorBuilder() ->withMemoryCache( // 500 types metadata maxItems: 500, ttl: \DateInterval::createFromDateString('1 hour') );