goaop/parser-reflection
AST-based Reflection API for PHP: introspect classes, methods, and properties directly from source code without autoloading or executing anything. Built on nikic/php-parser and compatible with native Reflection classes—ideal for static analyzers, code generators, and IDE tooling.
#[Route], #[Middleware], #[Cacheable]). This is a critical upgrade for modern Laravel static analysis.UnaryMinus/UnaryPlus (PR #141) and new expressions in defaults (PR #154) improves analysis of:
Str::of($value)->limit(10)).Collection::macro('negate', fn($items) => array_map(fn($i) => -$i, $items))).getModifiers() (PR #152) and nullable types (PR #146) ensure future-proofing for Laravel’s upcoming PHP 8.4+ adoption.Updated Use Cases:
#[Route], #[Middleware], or custom attributes (e.g., #[ApiResource]).Model::query()->where() with dynamic conditions).Route::get() vs. #[Get]).Conflicts:
nikic/php-parser as a fallback.Illuminate\Foundation\Application) may still require hybrid static/runtime analysis.public function __construct(public array $items = [new class()])).__call(), __getStatic()), making it more reliable than pure reflection.nikic/php-parser.Updated Risks:
| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| PHP 8.2+ Mandate | Critical | Enforce via platform-check in composer.json; document upgrade path. |
| Attribute Parsing Errors | High | Validate against Laravel’s #[Route], #[Middleware] in CI. |
| AST Overhead | Medium | Benchmark with laravel/framework repo; optimize .parserignore. |
| False Negatives | High | Combine with phpstan for runtime edge cases. |
| New Expression Edge Cases | Medium | Test with complex default values (e.g., new class()). |
nikic/php-parser.Model observers, event listeners)? If so, plan for hybrid analysis.laravel/framework.phpstan or supplement it? Consider integrating with pestphp/pest for test-level analysis.#[Route], #[Middleware], or custom attributes.Str::macro(), Collection::macro().path in #[Route]).ReflectionClass for runtime dynamic features (e.g., __callStatic in Model).nikic/php-parser.php artisan analyze:attributes).// Example: Service to combine static and runtime analysis
class HybridAnalyzer {
public function analyzeClass(string $class): array {
$staticData = (new ParserReflection($class))->getAST();
$runtimeData = new ReflectionClass($class);
return [
'static' => [
'attributes' => $staticData->getAttributes(),
'methods' => $staticData->getMethods(),
],
'runtime' => [
'dynamic_calls' => $this->detectDynamicCalls($runtimeData),
],
];
}
private function detectDynamicCalls(ReflectionClass $class): array {
// Fallback for __call(), __callStatic(), etc.
return array_filter($class->getMethods(), fn($m) =>
$m->isPublic() && in_array($m->getName(), ['__call', '__callStatic'])
);
}
}
laravel/framework’s attribute tests.nikic/php-parser instead.new class() in defaults).phpstan/pest for overlapping AST parsing.{
"config": {
"platform-check": true,
"platform": {
"php": "8.2"
}
}
}
.parserignore to exclude:
vendor/bootstrap/cache/app/Models/{Model}.php if using php artisan make:model with events).php artisan make
How can I help you explore Laravel packages today?