bac91b426e
Hi there, I'm working on mutation testing framework ([Infection](https://github.com/infection/infection/)) that is distributed as a PHAR. One of this goal is to run target project's test suite against mutated code. Since we use reflection and load project's autoloader, we want to avoid potential conflicts between vendor files of Infection itself and the target project. To avoid this issue, there is a project calld [PHP-Scoper](https://github.com/humbug/php-scoper). What it does is it prefixes all the namespaces of the library (including vendor folder) with some character(s), for example namespace `Infection\Mutator\PublicVisibility` is transformed to `ScoperAbc123\Infection\Mutant\PublicVisibility`. But since it also prefixes vendor folder, PHP-Parser's classes are prefixed as well and `NodeAbstract::getType()` after this prefixing works incorrectly. There is a hardcoded number `15` which means to remove `'PhpParser\Node'` (length=15) substring from the FQCN. Code: ```php // PHPParser\Node\Stmt\Declare_ -> Stmt_Declare return strtr(substr(rtrim(get_class($this), '_'), 15), '\\', '_'); ``` What I suggest is a little be more dynamic solution, to correctly extract class name (type) from the ***prefixed*** FQCL: `ScoperAbc123\PHPParser\Node\Stmt\Declare_` -> `Stmt_Declare` |
||
---|---|---|
bin | ||
doc | ||
grammar | ||
lib | ||
test | ||
test_old | ||
.gitignore | ||
.travis.yml | ||
CHANGELOG.md | ||
composer.json | ||
LICENSE | ||
phpunit.xml.dist | ||
README.md | ||
UPGRADE-1.0.md | ||
UPGRADE-2.0.md | ||
UPGRADE-3.0.md |
PHP Parser
This is a PHP 5.2 to PHP 7.1 parser written in PHP. Its purpose is to simplify static code analysis and manipulation.
Documentation for version 3.x (stable; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.1).
Documentation for version 2.x (stable; for running on PHP >= 5.4; for parsing PHP 5.2 to PHP 7.0).
Documentation for version 1.x (unsupported; for running on PHP >= 5.3; for parsing PHP 5.2 to PHP 5.6).
In a Nutshell
The parser turns PHP source code into an abstract syntax tree. For example, if you pass the following code into the parser:
<?php
echo 'Hi', 'World';
hello\world('foo', 'bar' . 'baz');
You'll get a syntax tree looking roughly like this:
array(
0: Stmt_Echo(
exprs: array(
0: Scalar_String(
value: Hi
)
1: Scalar_String(
value: World
)
)
)
1: Expr_FuncCall(
name: Name(
parts: array(
0: hello
1: world
)
)
args: array(
0: Arg(
value: Scalar_String(
value: foo
)
byRef: false
)
1: Arg(
value: Expr_Concat(
left: Scalar_String(
value: bar
)
right: Scalar_String(
value: baz
)
)
byRef: false
)
)
)
)
You can then work with this syntax tree, for example to statically analyze the code (e.g. to find programming errors or security issues).
Additionally, you can convert a syntax tree back to PHP code. This allows you to do code preprocessing (like automatedly porting code to older PHP versions).
Installation
The preferred installation method is composer:
php composer.phar require nikic/php-parser
Documentation
Component documentation: