- How do I install ergebnis/classy in a Laravel project?
- Install it with Composer by running composer require ergebnis/classy in your Laravel project. The package is pure PHP and does not require a Laravel service provider, so you can use its collectors directly after Composer autoloading. Commit the composer.json and composer.lock changes as usual.
- Does ergebnis/classy support PHP 8 enums and which collector should I use?
- Yes, ergebnis/classy detects classes, enums, interfaces, and traits, including modern PHP enum syntax and namespaces. For PHP 8 source, use the PhpTokenTokenizeConstructFromSourceCollector; the TokenGetAllConstructFromSourceCollector is documented for PHP 7. Check the package's PHP version constraints against your Laravel PHP matrix before upgrading.
- What is the difference between token-based and file-based collectors in ergebnis/classy?
- Token-based collectors parse source strings with token_get_all or PhpToken::tokenize and return typed construct objects without loading the code. File-based collectors, including DefaultConstructFromFileCollector, DefaultConstructFromSplFileInfoCollector, and DefaultConstructFromFinderCollector, read files or Finder results and return the same structured representations. Use token-based collection for in-memory source and file-based collection for project scans.
- Can ergebnis/classy scan all classes in a Laravel app directory?
- Yes, you can pass a Symfony Finder configured for app_path() to DefaultConstructFromFinderCollector to scan your Laravel application directory. Restrict scans to application code such as app, config, or custom paths and avoid vendor or bootstrap directories unless you explicitly need them. Make sure namespaces and PSR-4 autoloading are configured correctly so discovered constructs match your application.
- How can I find all classes implementing a specific interface with ergebnis/classy?
- ergebnis/classy collects class constructs from source, so you can filter the returned typed objects by the interface names recorded in each construct. It does not resolve runtime inheritance or autoload every class for you. If you need to verify actual runtime implementations, combine source discovery with Laravel's container, reflection, or class_implements after autoloading.
- Is ergebnis/classy intended for runtime Laravel requests or development tooling?
- It is best suited for development-time tooling such as static analysis, code generation, documentation, and architectural checks. Runtime use in a Laravel request is possible, but scanning source has overhead on large codebases, so cache results and avoid scanning on every request. Prefer Artisan commands or queued jobs for heavier scans.
- How does ergebnis/classy compare to Laravel's reflection and container helpers?
- Laravel's reflection and container helpers are designed for runtime binding resolution and tagged services, while ergebnis/classy scans source files for declarations without loading them. Use classy when you need batch source discovery, and use Laravel helpers when you need actual container instances or runtime metadata. If app()->tagged() or container bindings already answer your question, you may not need this package.
- Can I cache ergebnis/classy results in Laravel?
- The package does not include a Laravel cache integration, but you can wrap collector calls in Cache::remember and store the resulting typed construct data. Choose a cache key that includes the scanned paths and file modification state, and clear the cache when source files change. Avoid caching sensitive source details or scanning vendor and bootstrap directories unnecessarily.
- How do I test code that uses ergebnis/classy?
- Unit test collectors with fixture source strings or temporary files and assert the returned typed constructs, namespaces, and construct types. For Laravel integration, test an Artisan command or service that wraps the collector rather than mocking PHP tokens. Use real fixture files for Finder-based collectors and keep tests focused on discovery behavior.
- What are the limitations and maintenance considerations for ergebnis/classy in Laravel?
- ergebnis/classy only detects declared classes, enums, interfaces, and traits in source; it does not support anonymous classes or dynamic class generation through closures or eval. The package is actively maintained, but PHP 8.3+ features can introduce API shifts, so pin versions and test against your Laravel PHP matrix. Restrict scans to trusted paths to avoid exposing internal class details.