phpstan/phpdoc-parser
phpstan/phpdoc-parser parses PHPDocs into a typed AST so you can read and modify docblocks programmatically. It handles rich types like unions, generics, array shapes, callables, and conditional types, plus Doctrine annotations. Powers PHPStan.
@param/@return/@var/generic annotations — e.g., migrating array<string, Foo> styles, normalizing union order, or converting Doctrine annotations to PHP attributes — without reformatting unrelated comments. This is a distinct capability from "parse and read," and it directly unlocks a migration-tooling feature.Adopt when:
Look elsewhere when:
@deprecated or a single @author tag is cheaper with a targeted regex, and a full AST is over-engineering.nikic/php-parser territory, and the two are complementary, not substitutes.Diligence notes: MIT license is clean for commercial embedding. Release cadence is active and 2.3.x is current with PHP 8.5 in CI. The "0 dependents" figure understates reality — the library is a transitive dependency of PHPStan, so real-world exposure is far larger than the metric implies; validate against your own composer why output rather than the registry count.
For executives (one paragraph): We can accelerate a class of roadmap items — code quality gates, automated PHP upgrades, generated API documentation — by standing on an established, MIT-licensed PHPDoc parser instead of building one. It is the parsing engine behind PHPStan, is actively released, and supports modern PHP type syntax that regex-based approaches cannot handle. The decision is low-cost and reversible: no license fees, no data leaving our codebase, and the integration surface is a Composer dependency. The main watch-item is that it is a small, focused open-source project, so we should pin versions and expect to carry trivial patches rather than rely on a vendor SLA. Recommendation: adopt as an internal infrastructure dependency where a feature genuinely needs type-level understanding of docblocks; do not adopt it for shallow tag-reading.
For engineering (the short version): phpstan/phpdoc-parser gives us a typed AST for docblocks with a visitor-based traversal (AbstractNodeVisitor + NodeTraverser, with DONT_TRAVERSE_CHILDREN / STOP_TRAVERSAL / REMOVE_NODE controls), a CloningVisitor for safe deep copies, and a format-preserving Printer that round-trips edits with minimal diff noise. Setup is four objects (ParserConfig, Lexer, TypeParser/ConstExprParser, PhpDocParser); enable lines/indexes/comments attributes only when you need position mapping or the printer, since they cost tokens. Coverage includes generics with variance, intersection types, array and object shapes, conditional types, offset access, constant expressions, and Doctrine annotations. Node classes implement __set_state(), so ASTs can be cached or exported. It parses comments only — pair it with nikic/php-parser when we also need to walk PHP code, and keep a regression fixture suite around our own docblock dialect so upstream dialect changes surface in CI rather than in production.
How can I help you explore Laravel packages today?