2017-11-02 18:56:01 +01:00
|
|
|
<?php declare(strict_types=1);
|
2011-11-27 21:43:27 +01:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
2018-01-10 17:24:26 +01:00
|
|
|
class DummyNode extends NodeAbstract
|
|
|
|
{
|
2015-02-28 18:44:28 +01:00
|
|
|
public $subNode1;
|
|
|
|
public $subNode2;
|
|
|
|
|
|
|
|
public function __construct($subNode1, $subNode2, $attributes) {
|
2015-05-02 22:17:34 +02:00
|
|
|
parent::__construct($attributes);
|
2015-02-28 18:44:28 +01:00
|
|
|
$this->subNode1 = $subNode1;
|
|
|
|
$this->subNode2 = $subNode2;
|
|
|
|
}
|
|
|
|
|
2017-04-28 21:40:59 +02:00
|
|
|
public function getSubNodeNames() : array {
|
2017-08-13 14:35:03 +02:00
|
|
|
return ['subNode1', 'subNode2'];
|
2015-02-28 18:44:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method is only overwritten because the node is located in an unusual namespace
|
2017-04-28 21:40:59 +02:00
|
|
|
public function getType() : string {
|
2015-02-28 18:44:28 +01:00
|
|
|
return 'Dummy';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 11:18:00 +01:00
|
|
|
class NodeAbstractTest extends \PHPUnit\Framework\TestCase
|
2011-11-27 21:43:27 +01:00
|
|
|
{
|
2015-02-28 18:44:28 +01:00
|
|
|
public function provideNodes() {
|
2017-08-13 14:35:03 +02:00
|
|
|
$attributes = [
|
2012-05-06 18:24:26 +02:00
|
|
|
'startLine' => 10,
|
2017-09-29 17:34:15 +02:00
|
|
|
'endLine' => 11,
|
|
|
|
'startTokenPos' => 12,
|
|
|
|
'endTokenPos' => 13,
|
|
|
|
'startFilePos' => 14,
|
|
|
|
'endFilePos' => 15,
|
2017-08-13 14:35:03 +02:00
|
|
|
'comments' => [
|
2020-02-15 10:52:34 +01:00
|
|
|
new Comment('// Comment 1' . "\n"),
|
2014-02-06 14:44:16 +01:00
|
|
|
new Comment\Doc('/** doc comment */'),
|
2020-02-15 10:52:34 +01:00
|
|
|
new Comment('// Comment 2' . "\n"),
|
2017-08-13 14:35:03 +02:00
|
|
|
],
|
|
|
|
];
|
2012-04-29 23:32:09 +02:00
|
|
|
|
2015-05-02 22:17:34 +02:00
|
|
|
$node = new DummyNode('value1', 'value2', $attributes);
|
|
|
|
$node->notSubNode = 'value3';
|
2015-02-28 18:44:28 +01:00
|
|
|
|
2017-08-13 14:35:03 +02:00
|
|
|
return [
|
|
|
|
[$attributes, $node],
|
|
|
|
];
|
2015-02-28 18:44:28 +01:00
|
|
|
}
|
2011-11-27 21:43:27 +01:00
|
|
|
|
2015-02-28 18:44:28 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider provideNodes
|
|
|
|
*/
|
|
|
|
public function testConstruct(array $attributes, Node $node) {
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('Dummy', $node->getType());
|
2017-08-13 14:35:03 +02:00
|
|
|
$this->assertSame(['subNode1', 'subNode2'], $node->getSubNodeNames());
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame(10, $node->getLine());
|
2017-09-29 17:34:15 +02:00
|
|
|
$this->assertSame(10, $node->getStartLine());
|
|
|
|
$this->assertSame(11, $node->getEndLine());
|
|
|
|
$this->assertSame(12, $node->getStartTokenPos());
|
|
|
|
$this->assertSame(13, $node->getEndTokenPos());
|
|
|
|
$this->assertSame(14, $node->getStartFilePos());
|
|
|
|
$this->assertSame(15, $node->getEndFilePos());
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('/** doc comment */', $node->getDocComment()->getText());
|
2015-02-28 18:44:28 +01:00
|
|
|
$this->assertSame('value1', $node->subNode1);
|
|
|
|
$this->assertSame('value2', $node->subNode2);
|
2017-12-12 12:02:49 +01:00
|
|
|
$this->assertObjectHasAttribute('subNode1', $node);
|
|
|
|
$this->assertObjectHasAttribute('subNode2', $node);
|
|
|
|
$this->assertObjectNotHasAttribute('subNode3', $node);
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame($attributes, $node->getAttributes());
|
2017-09-29 17:09:16 +02:00
|
|
|
$this->assertSame($attributes['comments'], $node->getComments());
|
2011-11-27 21:43:27 +01:00
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2012-05-06 18:24:26 +02:00
|
|
|
/**
|
2015-02-28 18:44:28 +01:00
|
|
|
* @dataProvider provideNodes
|
2012-05-06 18:24:26 +02:00
|
|
|
*/
|
2015-02-28 18:44:28 +01:00
|
|
|
public function testGetDocComment(array $attributes, Node $node) {
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('/** doc comment */', $node->getDocComment()->getText());
|
2017-09-29 17:14:27 +02:00
|
|
|
$comments = $node->getComments();
|
|
|
|
|
2020-02-15 10:52:34 +01:00
|
|
|
array_splice($comments, 1, 1, []); // remove doc comment
|
2017-09-29 17:14:27 +02:00
|
|
|
$node->setAttribute('comments', $comments);
|
2012-05-06 18:24:26 +02:00
|
|
|
$this->assertNull($node->getDocComment());
|
2017-09-29 17:14:27 +02:00
|
|
|
|
2020-02-15 10:52:34 +01:00
|
|
|
// Remove all comments.
|
|
|
|
$node->setAttribute('comments', []);
|
2012-05-06 18:24:26 +02:00
|
|
|
$this->assertNull($node->getDocComment());
|
|
|
|
}
|
|
|
|
|
2016-09-17 20:48:51 +02:00
|
|
|
public function testSetDocComment() {
|
|
|
|
$node = new DummyNode(null, null, []);
|
|
|
|
|
|
|
|
// Add doc comment to node without comments
|
|
|
|
$docComment = new Comment\Doc('/** doc */');
|
|
|
|
$node->setDocComment($docComment);
|
|
|
|
$this->assertSame($docComment, $node->getDocComment());
|
|
|
|
|
|
|
|
// Replace it
|
|
|
|
$docComment = new Comment\Doc('/** doc 2 */');
|
|
|
|
$node->setDocComment($docComment);
|
|
|
|
$this->assertSame($docComment, $node->getDocComment());
|
|
|
|
|
|
|
|
// Add docmment to node with other comments
|
|
|
|
$c1 = new Comment('/* foo */');
|
|
|
|
$c2 = new Comment('/* bar */');
|
|
|
|
$docComment = new Comment\Doc('/** baz */');
|
|
|
|
$node->setAttribute('comments', [$c1, $c2]);
|
|
|
|
$node->setDocComment($docComment);
|
|
|
|
$this->assertSame([$c1, $c2, $docComment], $node->getAttribute('comments'));
|
2020-02-15 10:52:34 +01:00
|
|
|
|
|
|
|
// Replace doc comment that is not at the end.
|
|
|
|
$newDocComment = new Comment\Doc('/** new baz */');
|
|
|
|
$node->setAttribute('comments', [$c1, $docComment, $c2]);
|
|
|
|
$node->setDocComment($newDocComment);
|
|
|
|
$this->assertSame([$c1, $newDocComment, $c2], $node->getAttribute('comments'));
|
2016-09-17 20:48:51 +02:00
|
|
|
}
|
|
|
|
|
2011-11-27 21:43:27 +01:00
|
|
|
/**
|
2015-02-28 18:44:28 +01:00
|
|
|
* @dataProvider provideNodes
|
2011-11-27 21:43:27 +01:00
|
|
|
*/
|
2015-02-28 18:44:28 +01:00
|
|
|
public function testChange(array $attributes, Node $node) {
|
2011-12-02 17:52:03 +01:00
|
|
|
// direct modification
|
2011-11-27 21:43:27 +01:00
|
|
|
$node->subNode = 'newValue';
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('newValue', $node->subNode);
|
2011-11-27 21:43:27 +01:00
|
|
|
|
2011-12-02 17:52:03 +01:00
|
|
|
// indirect modification
|
|
|
|
$subNode =& $node->subNode;
|
|
|
|
$subNode = 'newNewValue';
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('newNewValue', $node->subNode);
|
2011-12-02 17:52:03 +01:00
|
|
|
|
|
|
|
// removal
|
2011-11-27 21:43:27 +01:00
|
|
|
unset($node->subNode);
|
2017-12-12 12:02:49 +01:00
|
|
|
$this->assertObjectNotHasAttribute('subNode', $node);
|
2011-11-27 21:43:27 +01:00
|
|
|
}
|
2012-04-09 12:37:47 +02:00
|
|
|
|
2015-02-28 18:44:28 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider provideNodes
|
|
|
|
*/
|
|
|
|
public function testIteration(array $attributes, Node $node) {
|
|
|
|
// Iteration is simple object iteration over properties,
|
|
|
|
// not over subnodes
|
|
|
|
$i = 0;
|
|
|
|
foreach ($node as $key => $value) {
|
|
|
|
if ($i === 0) {
|
|
|
|
$this->assertSame('subNode1', $key);
|
|
|
|
$this->assertSame('value1', $value);
|
2018-01-13 16:03:36 +01:00
|
|
|
} elseif ($i === 1) {
|
2015-02-28 18:44:28 +01:00
|
|
|
$this->assertSame('subNode2', $key);
|
|
|
|
$this->assertSame('value2', $value);
|
2018-01-13 16:03:36 +01:00
|
|
|
} elseif ($i === 2) {
|
2015-02-28 18:44:28 +01:00
|
|
|
$this->assertSame('notSubNode', $key);
|
|
|
|
$this->assertSame('value3', $value);
|
|
|
|
} else {
|
|
|
|
throw new \Exception;
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
$this->assertSame(3, $i);
|
|
|
|
}
|
|
|
|
|
2012-04-29 23:32:09 +02:00
|
|
|
public function testAttributes() {
|
2014-02-06 14:44:16 +01:00
|
|
|
/** @var $node Node */
|
2018-01-13 16:08:17 +01:00
|
|
|
$node = $this->getMockForAbstractClass(NodeAbstract::class);
|
2012-04-29 23:32:09 +02:00
|
|
|
|
2012-04-09 12:37:47 +02:00
|
|
|
$this->assertEmpty($node->getAttributes());
|
|
|
|
|
|
|
|
$node->setAttribute('key', 'value');
|
|
|
|
$this->assertTrue($node->hasAttribute('key'));
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('value', $node->getAttribute('key'));
|
2012-04-09 12:37:47 +02:00
|
|
|
|
|
|
|
$this->assertFalse($node->hasAttribute('doesNotExist'));
|
|
|
|
$this->assertNull($node->getAttribute('doesNotExist'));
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('default', $node->getAttribute('doesNotExist', 'default'));
|
2012-04-09 12:37:47 +02:00
|
|
|
|
|
|
|
$node->setAttribute('null', null);
|
|
|
|
$this->assertTrue($node->hasAttribute('null'));
|
|
|
|
$this->assertNull($node->getAttribute('null'));
|
|
|
|
$this->assertNull($node->getAttribute('null', 'default'));
|
|
|
|
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame(
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2012-04-29 23:32:09 +02:00
|
|
|
'key' => 'value',
|
2012-04-09 12:37:47 +02:00
|
|
|
'null' => null,
|
2017-08-13 14:35:03 +02:00
|
|
|
],
|
2012-04-09 12:37:47 +02:00
|
|
|
$node->getAttributes()
|
|
|
|
);
|
2017-05-05 18:18:44 +02:00
|
|
|
|
|
|
|
$node->setAttributes(
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2017-05-05 18:18:44 +02:00
|
|
|
'a' => 'b',
|
|
|
|
'c' => null,
|
2017-08-13 14:35:03 +02:00
|
|
|
]
|
2017-05-05 18:18:44 +02:00
|
|
|
);
|
|
|
|
$this->assertSame(
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2017-05-05 18:18:44 +02:00
|
|
|
'a' => 'b',
|
|
|
|
'c' => null,
|
2017-08-13 14:35:03 +02:00
|
|
|
],
|
2017-05-05 18:18:44 +02:00
|
|
|
$node->getAttributes()
|
|
|
|
);
|
2012-04-09 12:37:47 +02:00
|
|
|
}
|
2016-07-25 20:57:53 +02:00
|
|
|
|
|
|
|
public function testJsonSerialization() {
|
|
|
|
$code = <<<'PHP'
|
|
|
|
<?php
|
|
|
|
// comment
|
|
|
|
/** doc comment */
|
|
|
|
function functionName(&$a = 0, $b = 1.0) {
|
|
|
|
echo 'Foo';
|
|
|
|
}
|
|
|
|
PHP;
|
|
|
|
$expected = <<<'JSON'
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"nodeType": "Stmt_Function",
|
|
|
|
"byRef": false,
|
2017-04-28 19:09:39 +02:00
|
|
|
"name": {
|
|
|
|
"nodeType": "Identifier",
|
|
|
|
"name": "functionName",
|
|
|
|
"attributes": {
|
|
|
|
"startLine": 4,
|
|
|
|
"endLine": 4
|
|
|
|
}
|
|
|
|
},
|
2016-07-25 20:57:53 +02:00
|
|
|
"params": [
|
|
|
|
{
|
|
|
|
"nodeType": "Param",
|
|
|
|
"type": null,
|
|
|
|
"byRef": true,
|
|
|
|
"variadic": false,
|
2017-01-19 23:38:40 +01:00
|
|
|
"var": {
|
2017-01-19 23:23:38 +01:00
|
|
|
"nodeType": "Expr_Variable",
|
|
|
|
"name": "a",
|
|
|
|
"attributes": {
|
|
|
|
"startLine": 4,
|
|
|
|
"endLine": 4
|
|
|
|
}
|
|
|
|
},
|
2016-07-25 20:57:53 +02:00
|
|
|
"default": {
|
|
|
|
"nodeType": "Scalar_LNumber",
|
|
|
|
"value": 0,
|
|
|
|
"attributes": {
|
|
|
|
"startLine": 4,
|
|
|
|
"endLine": 4,
|
|
|
|
"kind": 10
|
|
|
|
}
|
|
|
|
},
|
2020-06-07 18:52:12 +02:00
|
|
|
"flags": 0,
|
2016-07-25 20:57:53 +02:00
|
|
|
"attributes": {
|
|
|
|
"startLine": 4,
|
|
|
|
"endLine": 4
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"nodeType": "Param",
|
|
|
|
"type": null,
|
|
|
|
"byRef": false,
|
|
|
|
"variadic": false,
|
2017-01-19 23:38:40 +01:00
|
|
|
"var": {
|
2017-01-19 23:23:38 +01:00
|
|
|
"nodeType": "Expr_Variable",
|
|
|
|
"name": "b",
|
|
|
|
"attributes": {
|
|
|
|
"startLine": 4,
|
|
|
|
"endLine": 4
|
|
|
|
}
|
|
|
|
},
|
2016-07-25 20:57:53 +02:00
|
|
|
"default": {
|
|
|
|
"nodeType": "Scalar_DNumber",
|
|
|
|
"value": 1,
|
|
|
|
"attributes": {
|
|
|
|
"startLine": 4,
|
|
|
|
"endLine": 4
|
|
|
|
}
|
|
|
|
},
|
2020-06-07 18:52:12 +02:00
|
|
|
"flags": 0,
|
2016-07-25 20:57:53 +02:00
|
|
|
"attributes": {
|
|
|
|
"startLine": 4,
|
|
|
|
"endLine": 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"returnType": null,
|
|
|
|
"stmts": [
|
|
|
|
{
|
|
|
|
"nodeType": "Stmt_Echo",
|
|
|
|
"exprs": [
|
|
|
|
{
|
|
|
|
"nodeType": "Scalar_String",
|
|
|
|
"value": "Foo",
|
|
|
|
"attributes": {
|
|
|
|
"startLine": 5,
|
|
|
|
"endLine": 5,
|
|
|
|
"kind": 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"attributes": {
|
|
|
|
"startLine": 5,
|
|
|
|
"endLine": 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"attributes": {
|
|
|
|
"startLine": 4,
|
|
|
|
"comments": [
|
|
|
|
{
|
|
|
|
"nodeType": "Comment",
|
2020-06-27 18:53:09 +02:00
|
|
|
"text": "\/\/ comment",
|
2016-07-25 20:57:53 +02:00
|
|
|
"line": 2,
|
2017-11-04 17:45:14 +01:00
|
|
|
"filePos": 6,
|
2020-02-09 16:53:46 +01:00
|
|
|
"tokenPos": 1,
|
2020-06-27 18:53:09 +02:00
|
|
|
"endLine": 2,
|
|
|
|
"endFilePos": 15,
|
2020-02-09 16:53:46 +01:00
|
|
|
"endTokenPos": 1
|
2016-07-25 20:57:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nodeType": "Comment_Doc",
|
|
|
|
"text": "\/** doc comment *\/",
|
|
|
|
"line": 3,
|
2017-11-04 17:45:14 +01:00
|
|
|
"filePos": 17,
|
2020-06-27 18:53:09 +02:00
|
|
|
"tokenPos": 3,
|
2020-02-09 16:53:46 +01:00
|
|
|
"endLine": 3,
|
|
|
|
"endFilePos": 34,
|
2020-06-27 18:53:09 +02:00
|
|
|
"endTokenPos": 3
|
2016-07-25 20:57:53 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"endLine": 6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
JSON;
|
|
|
|
|
|
|
|
$parser = new Parser\Php7(new Lexer());
|
2016-07-25 21:02:53 +02:00
|
|
|
$stmts = $parser->parse(canonicalize($code));
|
2016-07-25 20:57:53 +02:00
|
|
|
$json = json_encode($stmts, JSON_PRETTY_PRINT);
|
|
|
|
$this->assertEquals(canonicalize($expected), canonicalize($json));
|
|
|
|
}
|
2015-02-28 18:44:28 +01:00
|
|
|
}
|