Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Parser Laravel Package

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...

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer: composer require nikic/php-parser. The library provides three core capabilities: parsing PHP code into an Abstract Syntax Tree (AST), traversing and modifying that AST, and generating PHP code back from AST nodes. The fastest way to begin is:

  1. Use ParserFactory::createForNewestSupportedVersion() to create a parser tuned to your PHP version.
  2. Parse code and inspect the AST with NodeDumper.
  3. Use NodeTraverser with a custom NodeVisitorAbstract to walk and mutate nodes.
  4. Output modified code with PrettyPrinter\Standard::prettyPrintFile().

The Quick Start in the README contains a complete example showing these steps.

Implementation Patterns

  • AST visitors: Extend NodeVisitorAbstract to implement analysis (e.g., detecting unused variables) or transformations (e.g., adding return type declarations). Chain multiple visitors via NodeTraverser.
  • AST builders: Use the PhpParser\Builder namespace to programmatically construct AST nodes (e.g., new Function_('myFunc')->makePublic()->addParam('arg')) instead of instantiating nodes directly.
  • Name resolution: Wrap your traverser with NameResolver when you need to resolve Name nodes (e.g., converting Foo\Bar to Fully\Qualified\Class)—essential for tools that operate on fully qualified names.
  • Formatting preservation: Use PrettyPrinter\FormattingPreserver when modifying only specific nodes (e.g., renaming a class), to retain original indentation and comments.
  • AST → JSON / JSON → AST: For caching, debugging, or inter-process communication, serialize ASTs with NodeDumper::dump() or JsonDecoder/Encoder.

For code generation, combine builders with constants evaluation (ConstExprEvaluator) to compute runtime-safe literals in generated code.

Gotchas and Tips

  • Version targeting: Use ParserFactory::createForHostVersion() (not createForNewestSupportedVersion()) to parse code matching the host PHP version exactly—critical for handling PHP 8.x syntax correctly. The parser version may lag behind PHP’s latest features.
  • Error recovery: Pass ParserFactory::PREFER_PHP7 or enable error recovery to get partial ASTs for invalid code; useful for linters but expect incomplete node lists.
  • Attribute mutations: When modifying node attributes (e.g., flags, attrGroups) or visibility, ensure you use Param::makePrivate(), makeReadonly(), etc., rather than direct assignment—this avoids pretty-print bugs (noted in v5.7, v5.6.x releases).
  • Parentheses and precedence: Pretty printer rules changed significantly between 4.x and 5.x (see UPGRADE-5.0); always test generated code, especially around arrow functions, new, instanceof, and unary operators.
  • Performance: Disable Xdebug during AST processing. Reuse parser/pretty printer instances (they’re stateless), and avoid excessive var_dump() on large ASTs—use NodeDumper instead.
  • Node type guard pitfalls: instanceof checks can fail if you accidentally compare Name vs Name_FullyQualified nodes—use NameResolver early in your pipeline to normalize names.
  • PHP version compatibility: Version 5.x requires PHP ≥7.4 and parses up to PHP 8.4; version 4.x remains actively supported for PHP ≥7.0 (and parses up to 8.3). Don’t assume forward compatibility—check the release notes for breaking AST node changes (e.g., Name::$partsName::getParts()).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport