reflection
0.4.4
Typhoon reflection是原生 PHP reflection的替代方案。这是:
composer require typhoon/ reflection typhoon/phpstorm- reflection -stubs
typhoon/phpstorm- reflection -stubs
是jetbrains/phpstorm-stubs
的桥梁。如果没有这个包,内部类和函数就会从没有模板的本机reflection中反映出来。
use Typhoon reflection TyphoonReflector ;
use Typhoon Type types ;
use function Typhoon Type stringify ;
/**
* @template TTag of non-empty-string
*/
final readonly class Article
{
/**
* @param list<TTag> $tags
*/
public function __construct (
private array $ tags ,
) {}
}
$ reflector = TyphoonReflector:: build ();
$ class = $ reflector -> reflectClass (Article::class);
$ tagsType = $ class -> properties ()[ ' tags ' ]-> type ();
var_dump ( stringify ( $ tagsType )); // "list<TTag#Article>"
$ templateResolver = $ class -> createTemplateResolver ([
types:: union (
types:: string ( ' PHP ' ),
types:: string ( ' Architecture ' ),
),
]);
var_dump ( stringify ( $ tagsType -> accept ( $ templateResolver ))); // "list<'PHP'|'Architecture'>"
文档还远未完成。请毫不犹豫地提出问题来阐明事情是如何运作的。