- What is phpstan/phpdoc-parser used for?
- phpstan/phpdoc-parser parses PHPDoc comments and Doctrine annotations into an AST so tools can inspect and manipulate type information precisely. It is primarily used by PHPStan but also fits static analysis, code generation, and documentation processing. In a Laravel codebase it can support custom analyzers, generators, or docblock tooling.
- How do I install phpstan/phpdoc-parser in a Laravel project?
- Install it with Composer by running composer require phpstan/phpdoc-parser in your Laravel project. No Laravel service provider or configuration file is required because it is a framework-agnostic PHP library. You can then use its parser classes directly in application code, Artisan commands, or analysis tools.
- Does phpstan/phpdoc-parser support Laravel 10 and 11?
- phpstan/phpdoc-parser is not tied to a specific Laravel version, so it can be used with Laravel 10, 11, or other Composer-based Laravel applications. The real constraint is the PHP version required by the package release you install. Check the package requirements on Packagist before adding it to older Laravel projects.
- What PHPDoc types can phpstan/phpdoc-parser parse?
- It supports basic types, nullable types, union and intersection types, generics with variance, array shapes, object shapes, callable and closure types, conditional types, offset access types, and constant type expressions. It also parses constant expressions used in PHPDoc tags, including scalars, arrays, and class constant fetches. This makes it suitable for tools that need more than simple docblock text extraction.
- Can I use phpstan/phpdoc-parser to modify existing PHPDoc comments?
- Yes, the library represents PHPDocs as an AST and explicitly supports parsing and modifying PHPDocs. You can update type nodes or tags and then output the modified PHPDoc. That makes it useful for code generators and automated refactoring tools, not only read-only analysis.
- Does phpstan/phpdoc-parser parse Doctrine annotations?
- Yes, phpstan/phpdoc-parser also supports parsing Doctrine annotations. The Doctrine-related AST nodes live in the PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine namespace. This is helpful when your docblocks mix PHPDoc types with Doctrine annotation metadata.
- Do I need PHPStan installed to use phpstan/phpdoc-parser?
- No, you do not need PHPStan installed to use phpstan/phpdoc-parser as a dependency in your own code. It is primarily used by PHPStan, but it is a standalone Composer package with parser classes you can call directly. If you already run PHPStan in a Laravel project, the package may already be present transitively.
- Is phpstan/phpdoc-parser safe for production Laravel applications?
- It is MIT licensed and is the PHPDoc parser used by PHPStan, so it is a mature choice for static analysis tooling. Production suitability depends on your use case, input size, and error handling around malformed docblocks. Test it against your own Laravel codebase if you plan to parse large numbers of files at request time.
- How do I test PHPDoc parsing in Laravel with phpstan/phpdoc-parser?
- Use PHPUnit or Pest to instantiate the parser and lexer, parse a sample docblock, and assert the resulting AST node types and values. There are no Laravel-specific testing helpers in this package, so tests focus on the parser API itself. Keep fixture docblocks representative of generics, array shapes, and nullable types you expect in your project.
- What are alternatives to phpstan/phpdoc-parser for parsing PHPDocs?
- phpDocumentor/ReflectionDocBlock is a common alternative for general docblock parsing in PHP projects. phpstan/phpdoc-parser is particularly strong when you need rich PHPDoc type ASTs, PHPStan compatibility, Doctrine annotations, or PHPDoc modification. Compare alternatives based on whether you need to parse only tags or fully understand and rewrite advanced type syntax.