Skip to content

Commit

Permalink
Update to PHP-Parser 5
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Jan 2, 2025
1 parent 1a223df commit f46ad45
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 82 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The constructed SSA form is minimal and pure (or is supposed to be).
To bootstrap the parser, you need to give it a `PhpParser` instance:
```php
$parser = new PHPCfg\Parser(
(new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7)
(new PhpParser\ParserFactory)->createForNewestSupportedVersion()
);
```
Then, just call parse on a block of code, giving it a filename:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"require": {
"php": ">=7.4",
"nikic/php-parser": "^4.0",
"nikic/php-parser": "^5.0",
"phpdocumentor/graphviz": "^1.0.4"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
$graphviz = false;
list($fileName, $code) = getCode($argc, $argv);

$parser = new PHPCfg\Parser((new ParserFactory())->create(ParserFactory::PREFER_PHP7));
$parser = new PHPCfg\Parser((new ParserFactory())->createForNewestSupportedVersion());

$declarations = new PHPCfg\Visitor\DeclarationFinder();
$calls = new PHPCfg\Visitor\CallFinder();
Expand Down
4 changes: 2 additions & 2 deletions lib/PHPCfg/AstVisitor/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ function ($match) {
},
$comment->getText()
),
$comment->getLine(),
$comment->getFilePos()
$comment->getStartLine(),
$comment->getStartFilePos()
);

$node->setDocComment($comment);
Expand Down
10 changes: 6 additions & 4 deletions lib/PHPCfg/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,9 @@ protected function parseExprNode($expr)
if ($expr instanceof Node\Scalar) {
return $this->parseScalarNode($expr);
}
if ($expr instanceof Node\InterpolatedStringPart) {
return new Literal($expr->value);
}
if ($expr instanceof Node\Expr\AssignOp) {
$var = $this->parseExprNode($expr->var);
$read = $this->readVariable($var);
Expand Down Expand Up @@ -1524,15 +1527,14 @@ private function compileJumptableSwitch(Stmt\Switch_ $node)
private function parseScalarNode(Node\Scalar $scalar)
{
switch ($scalar->getType()) {
case 'Scalar_Encapsed':
case 'Scalar_InterpolatedString':
$op = new Op\Expr\ConcatList($this->parseExprList($scalar->parts, self::MODE_READ), $this->mapAttributes($scalar));
$this->block->children[] = $op;

return $op->result;
case 'Scalar_DNumber':
case 'Scalar_LNumber':
case 'Scalar_Float':
case 'Scalar_Int':
case 'Scalar_String':
case 'Scalar_EncapsedStringPart':
return new Literal($scalar->value);
case 'Scalar_MagicConst_Class':
// TODO
Expand Down
112 changes: 41 additions & 71 deletions test/PHPCfg/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace PHPCfg;

use PhpParser;
use PhpParser\ParserFactory;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -41,7 +40,7 @@ function foo(\$a) {
expr: Var#1<\$a>
EOF;

$parser = new Parser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), null);
$parser = new Parser((new ParserFactory())->createForNewestSupportedVersion(), null);
$traverser = new Traverser();
$traverser->addVisitor(new Visitor\Simplifier());
$printer = new Printer\Text();
Expand Down Expand Up @@ -71,120 +70,91 @@ function foowithattribute(\$a) {
}
EOF;

$expected = <<< EOF
$expected = <<<'EOF'
Block#1
Stmt_Function<'foo'>
attribute['filename']: foo.php
attribute['startLine']: 2
attribute['startTokenPos']: 1
attribute['startFilePos']: 6
attribute['endLine']: 4
attribute['endTokenPos']: 15
attribute['endFilePos']: 40
Stmt_Function<'foowithattribute'>
attribute['filename']: foo.php
attribute['startLine']: 6
attribute['startTokenPos']: 17
attribute['startFilePos']: 43
attribute['endLine']: 9
attribute['endTokenPos']: 35
attribute['endFilePos']: 98
attrGroup[0]:
attribute['filename']: foo.php
attribute['startLine']: 6
attribute['startTokenPos']: 17
attribute['startFilePos']: 43
attribute['endLine']: 6
attribute['endTokenPos']: 19
attribute['endFilePos']: 49
attr[0]:
attribute['filename']: foo.php
attribute['startLine']: 6
attribute['startTokenPos']: 18
attribute['startFilePos']: 45
attribute['endLine']: 6
attribute['endTokenPos']: 18
attribute['endFilePos']: 48
name: LITERAL('Attr')
Terminal_Return
Function 'foo': mixed
Block#1
Expr_Param
attribute['filename']: foo.php
attribute['startLine']: 2
attribute['startTokenPos']: 5
attribute['startFilePos']: 19
attribute['endLine']: 2
attribute['endTokenPos']: 5
attribute['endFilePos']: 20
declaredType: mixed
name: LITERAL('a')
result: Var#1<\$a>
result: Var#1<$a>
Terminal_Return
attribute['filename']: foo.php
attribute['startLine']: 3
attribute['startTokenPos']: 10
attribute['startFilePos']: 29
attribute['endLine']: 3
expr: Var#1<\$a>
attribute['endTokenPos']: 13
attribute['endFilePos']: 38
expr: Var#1<$a>
Function 'foowithattribute': mixed
Block#1
Expr_Param
attribute['filename']: foo.php
attribute['startLine']: 7
attribute['startTokenPos']: 25
attribute['startFilePos']: 77
attribute['endLine']: 7
attribute['endTokenPos']: 25
attribute['endFilePos']: 78
declaredType: mixed
name: LITERAL('a')
result: Var#1<\$a>
result: Var#1<$a>
Terminal_Return
attribute['filename']: foo.php
attribute['startLine']: 8
attribute['startTokenPos']: 30
attribute['startFilePos']: 87
attribute['endLine']: 8
expr: Var#1<\$a>
attribute['endTokenPos']: 33
attribute['endFilePos']: 96
expr: Var#1<$a>
EOF;

$parser = new Parser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), null);
$traverser = new Traverser();
$traverser->addVisitor(new Visitor\Simplifier());
$printer = new Printer\Text(true);

try {
$script = $parser->parse($code, 'foo.php');
$traverser->traverse($script);
$result = $printer->printScript($script);
} catch (\RuntimeException $e) {
$result = $e->getMessage();
}

$this->assertEquals($this->canonicalize($expected), $this->canonicalize($result));
}

public function testAdditionalAttributes()
{
$code = <<< EOF
<?php
function foo(\$a) {
return \$a;
}
EOF;

$expected = <<< EOF
Block#1
Stmt_Function<'foo'>
attribute['filename']: foo.php
attribute['startLine']: 2
attribute['startFilePos']: 6
attribute['endLine']: 4
attribute['endFilePos']: 40
Terminal_Return
Function 'foo': mixed
Block#1
Expr_Param
attribute['filename']: foo.php
attribute['startLine']: 2
attribute['startFilePos']: 19
attribute['endLine']: 2
attribute['endFilePos']: 20
declaredType: mixed
name: LITERAL('a')
result: Var#1<\$a>
Terminal_Return
attribute['filename']: foo.php
attribute['startLine']: 3
attribute['startFilePos']: 29
attribute['endLine']: 3
attribute['endFilePos']: 38
expr: Var#1<\$a>
EOF;

$lexer = new \PhpParser\Lexer(array(
'usedAttributes' => array(
'comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos'
)
));

$parser = new Parser((new ParserFactory())->create(ParserFactory::PREFER_PHP7, $lexer), null);
$parser = new Parser((new ParserFactory())->createForNewestSupportedVersion(), null);
$traverser = new Traverser();
$traverser->addVisitor(new Visitor\Simplifier());
$printer = new Printer\Text(true);
Expand Down
2 changes: 1 addition & 1 deletion test/PHPCfg/NameResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class NameResolverTest extends TestCase

protected function setUp(): void
{
$this->astParser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$this->astParser = (new ParserFactory())->createForNewestSupportedVersion();
}

/** @dataProvider getIgnoresInvalidParamTypeInDocCommentCases */
Expand Down
2 changes: 1 addition & 1 deletion test/PHPCfg/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testParseAndDump($code, $expectedDump)
{
$astTraverser = new PhpParser\NodeTraverser();
$astTraverser->addVisitor(new PhpParser\NodeVisitor\NameResolver());
$parser = new Parser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $astTraverser);
$parser = new Parser((new ParserFactory())->createForNewestSupportedVersion(), $astTraverser);
$traverser = new Traverser();
$traverser->addVisitor(new Visitor\Simplifier());
$printer = new Printer\Text();
Expand Down

0 comments on commit f46ad45

Please sign in to comment.