- Can I use roave/better-reflection for runtime dependency injection in Laravel?
- No, this package is explicitly designed for static analysis, not runtime reflection. It’s significantly slower than PHP’s native Reflection API, making it unsuitable for dependency injection or dynamic method calls. Stick to native reflection for runtime use cases.
- How do I integrate BetterReflection into Laravel’s service container?
- Register it as a singleton in `AppServiceProvider` using `BetterReflection::class`. For global access, bind it to the container with a closure that instantiates `BetterReflection`. Example: `$this->app->singleton(BetterReflection::class, fn() => new BetterReflection());`.
- Will this work with Laravel’s autoloader, or do I need custom setup?
- Yes, it works seamlessly with Laravel’s Composer autoloader. Use `ComposerSourceLocator` to locate classes. For non-standard setups (e.g., monorepos), combine it with `AggregateSourceLocator` to include custom paths.
- Can I reflect on closures or anonymous classes with this package?
- Yes, but with limitations. Use `ClosureSourceLocator` to reflect closures, though it requires the closure’s source code. Anonymous classes can be reflected if their source is available, but direct static reflection isn’t supported without workarounds.
- How does performance compare to PHP’s native ReflectionClass?
- Performance is **much slower**—expect 10–100x overhead. Avoid runtime use; cache the `BetterReflection` instance in the container or reuse it across requests. For CI or development tools, this is acceptable, but not for production-critical paths.
- Can I use this to extract AST (Abstract Syntax Tree) from Laravel classes?
- Absolutely. Use `$reflector->reflectClass()` to get a `ClassReflection` object, then call `getMethods()` or `getProperties()` and access the `getNode()` method to retrieve the AST for method bodies, docblocks, or type hints.
- Is there a Laravel-specific facade or helper for this package?
- No built-in facade, but you can create one (e.g., `BetterReflectionFacade`) to simplify usage. Example: `BetterReflectionFacade::reflectClass(MyClass::class)`. This avoids repeated instantiation and improves readability in your codebase.
- Does this package support PHP 8.2+ features like read-only properties or enums?
- Yes, it supports PHP 8.2+ features via PHP-Parser under the hood. Ensure your project’s PHP version matches the package’s requirements (check `php-parser/php-parser` compatibility). For older PHP versions, some features may be limited.
- How can I test a Laravel package that uses BetterReflection?
- Mock the `BetterReflection` instance in tests to avoid performance hits. Use PHPUnit’s mocking to return pre-defined `ClassReflection` or `MethodReflection` objects. For AST-based tests, pre-generate nodes or use `ReflectionTestCase` from `phpunit/phpunit`.
- Are there alternatives to BetterReflection for static analysis in Laravel?
- For basic static analysis, PHP’s native reflection suffices. For advanced use cases (AST, closures, or non-loaded classes), alternatives include `nikic/php-parser` (lower-level) or `rector/rector` (for refactoring). However, BetterReflection is the most feature-complete for Laravel tooling.