ac6f221c50
Previously the pretty printer added unnecessary and odd-looking parentheses when several operators with the same precedence were chained: 'a' . 'b' . 'c' . 'd' . 'e' // was printed as 'a' . ('b' . ('c' . ('d' . 'e'))) Another issue reported as part of #39 was that assignments inside closures were wrapped in parentheses: function() { $a = $b; } // was printed as function() { ($a = $b); } This was caused by the automatic precedence handling, which just regarded the closure as an ordinal nested expression. With the new system the $predenceMap of PrettyPrinterAbstract contains both precedence and associativity and there is a new method pPrec() which prints a node taking precedence and associativity into account. For simpler usage there are additional function pInfixOp(), pPrefixOp() and pPostfixOp(). Prints not going through pPrec() do not have any precedence handling (fixing the closure issue). |
||
---|---|---|
doc | ||
grammar | ||
lib | ||
test | ||
test_old | ||
.travis.yml | ||
CHANGELOG.md | ||
composer.json | ||
LICENSE | ||
phpunit.xml.dist | ||
README.md |
PHP Parser
This is a PHP 5.4 (and older) parser written in PHP. It's purpose is to simplify static code analysis and manipulation.
Documentation can be found in the doc/
directory.
Note: This project is experimental, so the API is subject to change.
In a Nutshell
Basically, the parser does nothing more than turn some PHP code into an abstract syntax tree. ("nothing more" is kind of sarcastic here as PHP has a ... uhm, let's just say "not nice" ... grammar, which makes parsing PHP very hard.)
For example, if you stick this code in 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).
So, that's it, in a nutshell. You can find everything else in the docs.