1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-26 20:04:48 +01:00

Drop support for false return value in NodeTraverser

This commit is contained in:
Nikita Popov 2017-02-03 22:34:27 +01:00
parent 2b72bae3d9
commit 987c61e935
5 changed files with 22 additions and 8 deletions

View File

@ -21,3 +21,5 @@ source code, while running on a newer version.
* Removed `type` subnode on `Class`, `ClassMethod` and `Property` nodes. Use `flags` instead.
* The `ClassConst::isStatic()` method has been removed. Constants cannot have a static modifier.
* The `NodeTraverser` no longer accepts `false` as a return value from a `leaveNode()` method.
`NodeTraverser::REMOVE_NODE` should be returned instead.

View File

@ -410,6 +410,7 @@ The last thing we need to do is remove the `namespace` and `use` statements:
```php
use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverser;
class NodeVisitor_NamespaceConverter extends \PhpParser\NodeVisitorAbstract
{
@ -428,8 +429,8 @@ class NodeVisitor_NamespaceConverter extends \PhpParser\NodeVisitorAbstract
// returning an array merges is into the parent array
return $node->stmts;
} elseif ($node instanceof Stmt\Use_) {
// returning false removed the node altogether
return false;
// return use nodes altogether
return NodeTraverser::REMOVE_NODE;
}
}
}

View File

@ -28,7 +28,7 @@ class NodeTraverser implements NodeTraverserInterface
* For subsequent visitors leaveNode() will still be invoked for the
* removed node.
*/
const REMOVE_NODE = false;
const REMOVE_NODE = 3;
/** @var NodeVisitor[] Visitors */
protected $visitors;
@ -211,9 +211,14 @@ class NodeTraverser implements NodeTraverserInterface
} elseif (self::REMOVE_NODE === $return) {
$doNodes[] = array($i, array());
break;
} else if (self::STOP_TRAVERSAL === $return) {
} elseif (self::STOP_TRAVERSAL === $return) {
$this->stopTraversal = true;
break 2;
} elseif (false === $return) {
throw new \LogicException(
'bool(false) return from leaveNode() no longer supported. ' .
'Return NodeTraverser::REMOVE_NODE instead'
);
} else {
throw new \LogicException(
'leaveNode() returned invalid value of type ' . gettype($return)

View File

@ -53,7 +53,7 @@ interface NodeVisitor
*
* @param Node $node Node
*
* @return null|false|int|Node|Node[] Replacement node (or special return value)
* @return null|int|Node|Node[] Replacement node (or special return value)
*/
public function leaveNode(Node $node);

View File

@ -85,7 +85,7 @@ class NodeTraverserTest extends \PHPUnit_Framework_TestCase
// remove the string1 node, leave the string2 node
$visitor->expects($this->at(2))->method('leaveNode')->with($str1Node)
->will($this->returnValue(false));
->will($this->returnValue(NodeTraverser::REMOVE_NODE));
$traverser = new NodeTraverser;
$traverser->addVisitor($visitor);
@ -280,7 +280,12 @@ class NodeTraverserTest extends \PHPUnit_Framework_TestCase
->will($this->returnValue('foobar'));
$visitor5 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor5->method('leaveNode')->willReturn(array(new Node\Scalar\DNumber(42.0)));
$visitor5->expects($this->at(3))->method('leaveNode')
->willReturn(array(new Node\Scalar\DNumber(42.0)));
$visitor6 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor6->expects($this->at(4))->method('leaveNode')
->willReturn(false);
return [
[$visitor1, 'enterNode() returned invalid value of type string'],
@ -288,6 +293,7 @@ class NodeTraverserTest extends \PHPUnit_Framework_TestCase
[$visitor3, 'leaveNode() returned invalid value of type string'],
[$visitor4, 'leaveNode() returned invalid value of type string'],
[$visitor5, 'leaveNode() may only return an array if the parent structure is an array'],
[$visitor6, 'bool(false) return from leaveNode() no longer supported. Return NodeTraverser::REMOVE_NODE instead'],
];
}
}