Weave 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

A PHP parser written in PHP that converts PHP 7/8 code into an abstract syntax tree, with tools for dumping, traversing, modifying, and regenerating code, plus name resolution and builders for static analysis and code generation.

View on GitHub
Deep Wiki
Context7

A PHP parser written in PHP

Frequently asked questions about Php Parser
How do I install nikic/php-parser in a Laravel application or package?
Run composer require nikic/php-parser; there is no extension to install beyond the standard tokenizer, no native build, and no service to run. In Laravel, bind a configured Parser and PrettyPrinter\Standard as singletons in a service provider and wrap file access in Illuminate\Filesystem. Integration is a one-line dependency plus a thin adapter: days for a read-only analysis feature, a few weeks for a formatting-preserving codemod.
Which PHP and Laravel versions does nikic/php-parser support?
php-parser 5.x requires PHP 7.4 or newer and parses PHP 7.0 through 8.4 with limited PHP 5.x support, while the 4.x line runs on PHP 7.0 and up. There is no Laravel-specific constraint, so your real limit is your package's PHP floor. If you still support Laravel 9 on PHP 8.0, require ^4.15 || ^5.0 and avoid 5.x-only entry points such as ParserFactory::createForNewestSupportedVersion(). Decide this before writing the first visitor, because it shapes your entire public API.
How do I parse PHP source code into an AST with nikic/php-parser?
Build a parser through ParserFactory, for example (new ParserFactory())->createForNewestSupportedVersion() on 5.x or create() and createForHostVersion() on 4.x. Call parse($code) inside a try/catch for PhpParser\Error, since invalid code can still produce a partial AST. Inspect the result with NodeDumper, which shows node types, subnodes, and the accurate location attributes attached to each node.
How do I traverse and modify AST nodes in nikic/php-parser?
Use NodeTraverser with one or more visitors; NodeVisitorAbstract lets you override enterNode and leaveNode to inspect or replace nodes. Return a node to replace the current one, NodeTraverser::REMOVE_NODE to delete it, or an array to splice several nodes in. Modifying only a subset of nodes lets the pretty printer reuse the original tokens, which is what keeps the rest of the file byte-identical.
Can nikic/php-parser rewrite PHP files while preserving the original formatting?
Yes, within limits. Parse the file, change only the nodes you care about, and print with PrettyPrinter\Standard, which reuses original tokens for untouched nodes so surrounding code and formatting stay intact. Edge cases cluster around comments, docblocks, indentation, and attributes on enums and cases, so validate output against real repositories before shipping a codemod. In Laravel, run this in an artisan command or queued job rather than at request time.
Does nikic/php-parser resolve class names and constant expressions on its own?
The library ships NameResolver for namespaced names and ConstExprEvaluator for constant expressions, but neither provides a cross-file symbol table, type inference, or autoloader awareness. Anything that needs to know what an identifier actually refers to must supply its own index. Keep that boundary explicit so later features do not assume the parser understands your application's symbols.
What are the alternatives to nikic/php-parser for PHP static analysis?
There is no credible pure-PHP alternative for parsing PHP 8.x into a traversable AST; nikic/php-parser is the reference implementation that PHPStan, Psalm, and Rector all build on. Reach for those higher-level tools when you need type-aware analysis instead of raw nodes. Write your own parser only if you need a narrow, fixed subset of syntax.
Is nikic/php-parser production-ready and actively maintained?
It is the de facto standard and is battle-tested across the PHP ecosystem, but it is effectively single-maintainer software funded by that dependence. The BSD-3-Clause license, dependency-free design, and small surface make it easy to vendor and possible to fork, though a fork is a multi-month commitment rather than a weekend. Treat maintainer concentration as a tracked, accepted risk: pin the version and upgrade deliberately.
How should I cache or persist ASTs generated by nikic/php-parser?
The AST is not a stable public contract across minor versions; every 5.x release adds node types and subnodes for new PHP syntax such as property hooks, asymmetric visibility, and the pipe operator. Keep the AST internal to your package, and if you cache the JSON representation, key the cache by parser version and deployment so stale trees are never reused. Do not expose raw nodes as a third-party plugin API without accepting that upgrade cost.
How do I test code that uses nikic/php-parser in a Laravel app?
Keep parsing pure: feed fixture strings or files into a parser instance resolved from the container, then assert on the resulting nodes or on NodeDumper and JSON output. Bind a configured Parser and PrettyPrinter\Standard as singletons, as the documentation recommends reusing instances, and stub file IO through Illuminate\Filesystem so tests never touch disk. Cover invalid-code paths with try/catch around PhpParser\Error and snapshot-test pretty-printed output for any formatting-preserving transform.
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.
vento/laravel-airbrake
wfeller/laravel-batch
maco-studios/mage-os-installer
busarm/armie
siberfx/laravel-logger
kfteam1991/dcat-admin
dashed/dashed-laposta
dashed/dashed-ecommerce-montaportal
dashed/dashed-ecommerce-etsy
neohq/string-blade-compiler
log1x/acf-composer
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core
nodisrupt/laravel-agent
sierratecnologia/media-manager