voku / simple-php-code-parser
Get a simple data structure from your php code.
Requires
- php: >=7.4
- nikic/php-parser: ~4.16
- phpdocumentor/reflection-common: ~2.2
- phpdocumentor/reflection-docblock: ~5.3
- phpdocumentor/type-resolver: ~1.7.2
- phpstan/phpdoc-parser: ~1.23
- react/async: ~3.0.0 || ~4.1.0
- react/filesystem: ^0.2@dev
- voku/simple-cache: ~4.1
Requires (Dev)
- phpunit/phpunit: ~6.0 || ~7.0 || ~9.0
- dev-master
- 0.20.1
- 0.20.0
- 0.19.6
- 0.19.5
- 0.19.4
- 0.19.3
- 0.19.2
- 0.19.1
- 0.19.0
- 0.18.2
- 0.18.0
- 0.17.0
- 0.16.6
- 0.16.5
- 0.16.4
- 0.16.3
- 0.16.2
- 0.16.1
- 0.16.0
- 0.15.3
- 0.15.2
- 0.15.1
- 0.15.0
- 0.14.0
- 0.13.2
- 0.13.1
- 0.13.0
- 0.12.0
- 0.11.0
- 0.10.0
- 0.9.0
- 0.8.0
- 0.7.0
- 0.6.0
- 0.5.0
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.1
- 0.2.0
- 0.1.0
- dev-renovate/actions-cache-5.x
- dev-copilot/add-tests-for-php-parser-validation
- dev-copilot/fix-failing-phpdoc-test
- dev-copilot/update-for-php-8-4
- dev-copilot/add-missing-php-8-features-again
- dev-analysis-NdDNk1
- dev-copilot/update-php-dependencies
- dev-copilot/replace-get-cpu-cores
- dev-copilot/adapt-php-parser-compatibility
- dev-copilot/add-missing-php-8-features
- dev-analysis-dj2339
- dev-analysis-O3BxNL
- dev-analysis-YO03Ro
- dev-analysis-vZMo3r
- dev-analysis-PG5Ew9
- dev-analysis-marjMb
- dev-analysis-M1QJ1K
- dev-analysis-L37kEk
- dev-analysis-1boDOw
- dev-analysis-QMj9Yp
- dev-analysis-QM2ejO
- dev-analysis-QMpwV3
- dev-analysis-ajJopY
- dev-analysis-OMrl1l
This package is auto-updated.
Last update: 2026-04-13 17:20:34 UTC
README
❤ Simple PHP Code Parser
You can simply scan a string, a file or a full directory and you can see a simple data structure from your php code.
- Classes (PHPClass)
- Properties (PHPProperty)
- Constants (PHPConst)
- Methods (PHPMethod)
- Interfaces (PHPInterface)
- Traits (PHPTrait)
- Enums (PHPEnum)
- Functions (PHPFunction)
- Parameter (PHPParameter)
- Attributes (PHPAttribute)
This code is forked from JetBrains/phpstorm-stubs but you can't use the classes from "phpstorm-stubs" directly, because they are in a test namespace and the autoloader is "autoload-dev", so here is a extended version.
We will use:
Requirements
- PHP >= 8.1
Supported PHP Features
Runtime support for this library is PHP 8.1+, while the parser test suite validates analyzable source syntax from PHP 5.3 through PHP 8.5.
Legacy source syntax that can still be analyzed
| Source Syntax Generation | Representative coverage |
|---|---|
| PHP 5.3 | namespaces, closures with use, array() syntax |
| PHP 5.4 | traits, callable, short arrays |
| PHP 5.5 | generators / yield, finally |
| PHP 5.6 | variadics, argument unpacking-ready syntax, constant arrays |
| PHP 7.0 | scalar parameter types, return types, anonymous classes |
| PHP 7.1 | nullable types, iterable, void |
| PHP 7.2 | object type |
| PHP 7.3 | trailing commas in calls |
| PHP 7.4 | typed properties, arrow functions |
Modern source syntax coverage
| Feature | PHP Version | Supported |
|---|---|---|
| Attributes (class, method, property, parameter, constant) | 8.0+ | ✅ |
| Constructor property promotion | 8.0+ | ✅ |
| Union types | 8.0+ | ✅ |
| Named arguments | 8.0+ | ✅ |
| Match expressions | 8.0+ | ✅ |
| Nullsafe operator | 8.0+ | ✅ |
| Enums (unit, string-backed, int-backed) | 8.1+ | ✅ |
| Readonly properties | 8.1+ | ✅ |
| Intersection types | 8.1+ | ✅ |
never return type |
8.1+ | ✅ |
| First-class callable syntax | 8.1+ | ✅ |
| Readonly classes | 8.2+ | ✅ |
| DNF types | 8.2+ | ✅ |
Standalone true, false, null types |
8.2+ | ✅ |
| Trait constants | 8.2+ | ✅ |
| Typed class constants | 8.3+ | ✅ |
#[\Override] attribute detection |
8.3+ | ✅ |
| Property hooks / asymmetric visibility | 8.4+ | ✅ |
Install via "composer require"
composer require voku/simple-php-code-parser
Quick Start
Parse a string:
$code = ' <?php namespace voku\tests; class SimpleClass {} $obja = new class() {}; $objb = new class {}; class AnotherClass {} '; $phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getFromString($code); $phpClasses = $phpCode->getClasses(); var_dump($phpClasses['voku\tests\SimpleClass']); // "PHPClass"-object
Parse one class:
$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getFromClassName(Dummy::class); $phpClasses = $phpCode->getClasses(); var_dump($phpClasses[Dummy::class]); // "PHPClass"-object var_dump($phpClasses[Dummy::class]->methods); // "PHPMethod[]"-objects var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']); // "PHPMethod"-object var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters); // "PHPParameter[]"-objects var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters['useRandInt']); // "PHPParameter"-object var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters['useRandInt']->type); // "bool"
Parse one file:
$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getPhpFiles(__DIR__ . '/Dummy.php'); $phpClasses = $phpCode->getClasses(); var_dump($phpClasses[Dummy::class]); // "PHPClass"-object var_dump($phpClasses[Dummy::class]->methods); // "PHPMethod[]"-objects var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']); // "PHPMethod"-object var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters); // "PHPParameter[]"-objects var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters['useRandInt']); // "PHPParameter"-object var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters['useRandInt']->type); // "bool"
Parse many files:
$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getPhpFiles(__DIR__ . '/src'); $phpClasses = $phpCode->getClasses(); var_dump($phpClasses[Dummy::class]); // "PHPClass"-object
Unified metadata API:
$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getPhpFiles(__DIR__ . '/src'); $phpClasses = $phpCode->getClasses(); $phpInterfaces = $phpCode->getInterfaces(); $phpTraits = $phpCode->getTraits(); $phpEnums = $phpCode->getEnums(); $phpFunctions = $phpCode->getFunctions(); $phpConstants = $phpCode->getConstants(); $functionInfo = $phpCode->getFunctionsInfo(); $methodInfo = $phpClasses[MyService::class]->getMethodsInfo(); $propertyInfo = $phpClasses[MyService::class]->getPropertiesInfo();
The library is meant to be the simple integration layer that other tools can call instead of wiring together nikic/php-parser, phpstan/phpdoc-parser, phpDocumentor, and native reflection themselves. The test suite validates analyzable PHP source from PHP 5.3 through PHP 8.5, including legacy syntax generations and modern type declarations / metadata such as attributes, enums, readonly constructs, typed constants, and property hooks.
Access enums:
$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getPhpFiles(__DIR__ . '/src'); $phpEnums = $phpCode->getEnums(); // PHPEnum objects with scalarType, cases, methods, constants, attributes
Access attributes:
$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getPhpFiles(__DIR__ . '/src'); $phpClasses = $phpCode->getClasses(); $class = $phpClasses['MyClass']; // Class-level attributes foreach ($class->attributes as $attr) { echo $attr->name; // e.g. "MyAttribute" print_r($attr->arguments); // constructor arguments (array) } // Method/property/parameter attributes work the same way foreach ($class->methods['myMethod']->attributes as $attr) { ... } foreach ($class->properties['myProp']->attributes as $attr) { ... } foreach ($class->methods['myMethod']->parameters['param']->attributes as $attr) { ... }
Support
For support and donations please visit GitHub | Issues | PayPal | Patreon.
For status updates and release announcements please visit Releases | Patreon.
For professional support please contact me.
Thanks
- Thanks to GitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Management, etc.
- Thanks to IntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
- Thanks to PHPStan && Psalm for really great Static analysis tools and for discover bugs in the code!