nikic/php-parser
Parse PHP code into an Abstract Syntax Tree (AST) for static analysis, manipulation, and code generation. Supports PHP 5.x to 8.4, handles errors gracefully, and preserves formatting during AST-to-code conversion. Easily traverse, modify, and convert ASTs back to PHP, with JSON serialization support...
array_filter → Arr::where).routes/web.php or AppServiceProvider.php).vendor/) in hot paths.composer require nikic/php-parser.@foreach). Workaround: Pre-process Blade to plain PHP or use regex to isolate directives.eval()/create_function() (e.g., in Route::controller()). The parser can’t analyze these; require runtime instrumentation..php files in bootstrap/cache/) is obfuscated. Parse source files instead.| Risk Area | Mitigation Strategy |
|---|---|
| AST Complexity | Start with simple transformations (e.g., renaming functions) before tackling Laravel-specific nodes (e.g., Stmt\Namespace_ with use statements). |
| Performance | Cache parsed ASTs (e.g., FileCache for repeated analysis). Avoid parsing in request scope. |
| Error Handling | Use ParserFactory::create() with ParserFactory::PREFER_PHP7 for broader compatibility. |
| Laravel-Specific Nodes | Extend the parser with custom visitors for Laravel constructs (e.g., Route::get → Expr\MethodCall). |
| Regression Risk | Test against Laravel’s PHP version matrix (e.g., ensure v5.x works with PHP 8.1+). |
| Tooling Dependencies | Document dependencies (e.g., "Requires PHP 8.1+ for full Laravel 10 support"). |
Route::middleware)?clone changes)?php artisan optimize) or PHPStan for some use cases?php artisan parse:refactor).PhpParserServiceProvider binding the parser).php artisan ast:analyze App/Models/User.php).Debugbar or Tinker for runtime AST inspection.php artisan command).app/Http/Controllers/ to detect deprecated Route::controller() usage.framework/src/Illuminate/) to understand AST structure.Route definitions).Booted, Registered) for runtime analysis.storage/framework/ast_cache/).parallel:workers in Artisan).| Component | Compatibility Notes |
|---|---|
| PHP 8.4/8.5 | Use v5.6.x (supports pipe operator, property hooks, clone changes). |
| Laravel 10/11 | v5.x works; test for PHP 8.3+ features (e.g., readonly properties). |
| Blade Templates | Not natively supported. Pre-process with regex or a custom lexer. |
| Dynamic Code | Cannot parse eval()/create_function. Use runtime proxies or whitelists. |
| Bytecode | Avoid parsing .php files in bootstrap/cache/. Use source files instead. |
| Third-Party Packages | May use different PHP versions. Pin parser version to match (e.g., ^5.6). |
routes/web.php).@php artisan optimize to all routes).phpunit).Route::middleware).FileCache for ASTs).weakReferences in NodeConnectingVisitor).master branch).How can I help you explore Laravel packages today?