roave/better-reflection
Enhanced PHP reflection for static analysis: reflect classes without loading them, from PHP code strings or closures, extract AST from functions/methods, and read type declarations and docblocks. Feature-rich but slower than native reflection.
Reflection API, and some methods (e.g., newInstance, invoke) are unsupported.@Route, @Test) without runtime overhead.composer require roave/better-reflection. No Laravel-specific hooks or service providers required.ComposerSourceLocator), aligning with Laravel’s PSR-4 autoloading.php artisan) are low-risk, but integrating into request pipelines (e.g., middleware) could degrade performance.Reflection* methods are unsupported (e.g., newInstance, getExtension). Requires careful mapping to native reflection or fallback logic.app->make())?newInstance) be handled? Fallback to native reflection?php artisan make:, phpunit) or be a standalone library?php artisan analyze:types).SingleFileSourceLocator or StringSourceLocator for non-standard setups (e.g., vendor-agnostic plugins).getExtension is unsupported).php artisan command).BetterReflection APIs.BetterReflection calls in a service class with fallback to native reflection:
class ReflectionService {
public function reflectClass(string $class): ReflectionClass {
try {
return (new BetterReflection())->reflector()->reflectClass($class);
} catch (Exception $e) {
return new \ReflectionClass($class);
}
}
}
php artisan serve --load).app->make()-like logic but cannot instantiate classes (use new or native reflection instead).illuminate/support). Test with phpunit and laravel-debugbar.spatie/laravel-activitylog) could benefit from AST extraction.php artisan commands).BetterReflection adds/removes features.ReflectionAttribute.newInstance is called).memory_get_usage().vendor/) may be slow. Restrict to application code.| Scenario | Impact | Mitigation |
|---|---|---|
| Unsupported Reflection Method | Tool breaks or returns partial data | Fallback to native reflection. |
| Malformed PHP Code | AST parsing fails | Validate input or use try-catch. |
| Performance Degradation | Slow CLI commands or requests | Cache results, limit scope. |
| PHP Version Incompatibility | Features fail (e.g., PHP 8.0+) | Polyfills or version gating. |
| Autoloader Conflicts | Classes not found | Use SingleFileSourceLocator. |
How can I help you explore Laravel packages today?