Disable cloning in traverser by default

This commit is contained in:
Nikita Popov 2015-07-14 17:39:56 +02:00
parent 0ef15c111a
commit feb82eed33
4 changed files with 33 additions and 36 deletions

14
UPGRADE-2.0.md Normal file
View File

@ -0,0 +1,14 @@
Upgrading from PHP-Parser 1.x to 2.0
====================================
### PHP version requirements
PHP-Parser now requires PHP 5.4 or newer to run. It is however still possible to *parse* PHP 5.2 and
PHP 5.3 source code, while running on a newer version.
###
### Miscellaneous
* The `NodeTraverser` no longer clones nodes by default. If you want to restore the old behavior,
pass `true` to the constructor.

View File

@ -19,7 +19,7 @@ class NodeTraverser implements NodeTraverserInterface
*
* @param bool $cloneNodes Should the traverser clone the nodes when traversing the AST
*/
public function __construct($cloneNodes = true) {
public function __construct($cloneNodes = false) {
$this->visitors = array();
$this->cloneNodes = $cloneNodes;
}

View File

@ -185,18 +185,18 @@ class NodeTraverserTest extends \PHPUnit_Framework_TestCase
$this->assertAttributeSame($postExpected, 'visitors', $traverser, 'The appropriate visitors are not present after removal');
}
public function testCloneNodesByDefault() {
public function testCloneNodes() {
$stmts = array(new Node\Stmt\Echo_(array(new String_('Foo'), new String_('Bar'))));
$traverser = new NodeTraverser;
$traverser = new NodeTraverser(true);
$this->assertNotSame($stmts, $traverser->traverse($stmts));
}
public function testCloneNodesDisabled() {
public function testNoCloneNodesByDefault() {
$stmts = array(new Node\Stmt\Echo_(array(new String_('Foo'), new String_('Bar'))));
$traverser = new NodeTraverser(false);
$traverser = new NodeTraverser;
$this->assertSame($stmts, $traverser->traverse($stmts));
}

View File

@ -278,53 +278,36 @@ EOC;
$this->assertEquals($stmts, $traverser->traverse($stmts));
}
protected function createNamespacedAndNonNamespaced(array $stmts) {
return array(
new Stmt\Namespace_(new Name('NS'), $stmts),
new Stmt\Namespace_(null, $stmts),
);
}
public function testAddNamespacedName() {
$stmts = $this->createNamespacedAndNonNamespaced(array(
$nsStmts = array(
new Stmt\Class_('A'),
new Stmt\Interface_('B'),
new Stmt\Function_('C'),
new Stmt\Const_(array(
new Node\Const_('D', new Node\Scalar\String_('E'))
new Node\Const_('D', new Node\Scalar\LNumber(42))
)),
new Stmt\Trait_('E'),
new Expr\New_(new Stmt\Class_(null)),
));
);
$traverser = new PhpParser\NodeTraverser;
$traverser->addVisitor(new NameResolver);
$stmts = $traverser->traverse($stmts);
$stmts = $traverser->traverse([new Stmt\Namespace_(new Name('NS'), $nsStmts)]);
$this->assertSame('NS\\A', (string) $stmts[0]->stmts[0]->namespacedName);
$this->assertSame('NS\\B', (string) $stmts[0]->stmts[1]->namespacedName);
$this->assertSame('NS\\C', (string) $stmts[0]->stmts[2]->namespacedName);
$this->assertSame('NS\\D', (string) $stmts[0]->stmts[3]->consts[0]->namespacedName);
$this->assertObjectNotHasAttribute('namespacedName', $stmts[0]->stmts[4]->class);
$this->assertSame('A', (string) $stmts[1]->stmts[0]->namespacedName);
$this->assertSame('B', (string) $stmts[1]->stmts[1]->namespacedName);
$this->assertSame('C', (string) $stmts[1]->stmts[2]->namespacedName);
$this->assertSame('D', (string) $stmts[1]->stmts[3]->consts[0]->namespacedName);
$this->assertObjectNotHasAttribute('namespacedName', $stmts[1]->stmts[4]->class);
}
$this->assertSame('NS\\E', (string) $stmts[0]->stmts[4]->namespacedName);
$this->assertObjectNotHasAttribute('namespacedName', $stmts[0]->stmts[5]->class);
public function testAddTraitNamespacedName() {
$stmts = $this->createNamespacedAndNonNamespaced(array(
new Stmt\Trait_('A')
));
$traverser = new PhpParser\NodeTraverser;
$traverser->addVisitor(new NameResolver);
$stmts = $traverser->traverse($stmts);
$this->assertSame('NS\\A', (string) $stmts[0]->stmts[0]->namespacedName);
$this->assertSame('A', (string) $stmts[1]->stmts[0]->namespacedName);
$stmts = $traverser->traverse([new Stmt\Namespace_(null, $nsStmts)]);
$this->assertSame('A', (string) $stmts[0]->stmts[0]->namespacedName);
$this->assertSame('B', (string) $stmts[0]->stmts[1]->namespacedName);
$this->assertSame('C', (string) $stmts[0]->stmts[2]->namespacedName);
$this->assertSame('D', (string) $stmts[0]->stmts[3]->consts[0]->namespacedName);
$this->assertSame('E', (string) $stmts[0]->stmts[4]->namespacedName);
$this->assertObjectNotHasAttribute('namespacedName', $stmts[0]->stmts[5]->class);
}
/**