Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Token Reflection Laravel Package

andrewsville/php-token-reflection

PHP library for reflecting and analyzing PHP token streams. Parse source code into tokens and inspect structures like classes, functions, namespaces, and docblocks—useful for static analysis, code generation, and tooling without executing the code.

View on GitHub
Deep Wiki
Context7

Getting Started

Begin by installing via Composer: composer require andrewsville/php-token-reflection. Despite being archived, it remains usable for static inspection of PHP source without execution. Initialize the broker and parse a file:

$broker = new TokenReflection\Broker();
$broker->addCode(file_get_contents('src/MyService.php'));
$class = $broker->getClass('App\MyService');

First use case: Extract public method names and their docblock short descriptions for generating API documentation stubs or scaffolding service introspection.

Implementation Patterns

  • CI linter pipelines: Embed into pre-commit or CI checks (e.g., via Symfony Finder + TokenReflection\Broker) to enforce internal conventions—e.g., require @deprecated tags on methods with specific names.
  • Boilerplate generation: Inspect model classes for properties and type hints to scaffold DTOs, factories, or Doctrine listeners automatically.
  • Legacy migration scripts: Scan codebases for @method annotations referencing removed methods or deprecated patterns (e.g., mysql_* calls in docblocks).
  • Watch-mode tooling: Combine with a file watcher (e.g., chokidar CLI or Laravel’s artisan make:listener --watch) by calling clearCache() after addCode() to refresh reflection state incrementally.
  • IDE helper enrichment: Feed parsed class metadata into ide-php or intelephense plugins to补充 missing docblocks or improve autocomplete fidelity.

Gotchas and Tips

  • PHP 8+ limitations: Typed properties, union types (int|string), and attributes (#[Route(...)]) are not reliably parsed—verify against your PHP version; many modern syntax features will throw or misinterpret tokens.
  • Namespace resolution gap: The library gives you the declared namespace, but not resolved FQCNs from imports (use App\Models\UserApp\Models\User). You must manually resolve using getNamespaceAliases() and getImports().
  • Caching behavior: Broker caches parsed results in memory and may use opcache.file_cache. Always call $broker->clearCache() before re-parsing modified files in long-running processes (e.g., console commands).
  • No constant resolution: Class constants (self::MAX_RETRY) are exposed as strings—no runtime value resolution. Use ReflectionClass::getConstant() only works for declared constants, not values.
  • Fork responsibly: For production use, consider forking to patch critical gaps (e.g., PHP 8.1+ union types) or migrate to nikic/php-parser + custom AST visitors for better maintainability and PHP version support.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation