mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-01-17 15:18:17 +01:00
Drop support for false return value in NodeTraverser
This commit is contained in:
parent
2b72bae3d9
commit
987c61e935
@ -21,3 +21,5 @@ source code, while running on a newer version.
|
|||||||
|
|
||||||
* Removed `type` subnode on `Class`, `ClassMethod` and `Property` nodes. Use `flags` instead.
|
* 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 `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.
|
@ -410,6 +410,7 @@ The last thing we need to do is remove the `namespace` and `use` statements:
|
|||||||
```php
|
```php
|
||||||
use PhpParser\Node;
|
use PhpParser\Node;
|
||||||
use PhpParser\Node\Stmt;
|
use PhpParser\Node\Stmt;
|
||||||
|
use PhpParser\NodeTraverser;
|
||||||
|
|
||||||
class NodeVisitor_NamespaceConverter extends \PhpParser\NodeVisitorAbstract
|
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
|
// returning an array merges is into the parent array
|
||||||
return $node->stmts;
|
return $node->stmts;
|
||||||
} elseif ($node instanceof Stmt\Use_) {
|
} elseif ($node instanceof Stmt\Use_) {
|
||||||
// returning false removed the node altogether
|
// return use nodes altogether
|
||||||
return false;
|
return NodeTraverser::REMOVE_NODE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ class NodeTraverser implements NodeTraverserInterface
|
|||||||
* For subsequent visitors leaveNode() will still be invoked for the
|
* For subsequent visitors leaveNode() will still be invoked for the
|
||||||
* removed node.
|
* removed node.
|
||||||
*/
|
*/
|
||||||
const REMOVE_NODE = false;
|
const REMOVE_NODE = 3;
|
||||||
|
|
||||||
/** @var NodeVisitor[] Visitors */
|
/** @var NodeVisitor[] Visitors */
|
||||||
protected $visitors;
|
protected $visitors;
|
||||||
@ -211,9 +211,14 @@ class NodeTraverser implements NodeTraverserInterface
|
|||||||
} elseif (self::REMOVE_NODE === $return) {
|
} elseif (self::REMOVE_NODE === $return) {
|
||||||
$doNodes[] = array($i, array());
|
$doNodes[] = array($i, array());
|
||||||
break;
|
break;
|
||||||
} else if (self::STOP_TRAVERSAL === $return) {
|
} elseif (self::STOP_TRAVERSAL === $return) {
|
||||||
$this->stopTraversal = true;
|
$this->stopTraversal = true;
|
||||||
break 2;
|
break 2;
|
||||||
|
} elseif (false === $return) {
|
||||||
|
throw new \LogicException(
|
||||||
|
'bool(false) return from leaveNode() no longer supported. ' .
|
||||||
|
'Return NodeTraverser::REMOVE_NODE instead'
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
throw new \LogicException(
|
throw new \LogicException(
|
||||||
'leaveNode() returned invalid value of type ' . gettype($return)
|
'leaveNode() returned invalid value of type ' . gettype($return)
|
||||||
|
@ -53,7 +53,7 @@ interface NodeVisitor
|
|||||||
*
|
*
|
||||||
* @param Node $node Node
|
* @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);
|
public function leaveNode(Node $node);
|
||||||
|
|
||||||
|
@ -85,7 +85,7 @@ class NodeTraverserTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
// remove the string1 node, leave the string2 node
|
// remove the string1 node, leave the string2 node
|
||||||
$visitor->expects($this->at(2))->method('leaveNode')->with($str1Node)
|
$visitor->expects($this->at(2))->method('leaveNode')->with($str1Node)
|
||||||
->will($this->returnValue(false));
|
->will($this->returnValue(NodeTraverser::REMOVE_NODE));
|
||||||
|
|
||||||
$traverser = new NodeTraverser;
|
$traverser = new NodeTraverser;
|
||||||
$traverser->addVisitor($visitor);
|
$traverser->addVisitor($visitor);
|
||||||
@ -280,7 +280,12 @@ class NodeTraverserTest extends \PHPUnit_Framework_TestCase
|
|||||||
->will($this->returnValue('foobar'));
|
->will($this->returnValue('foobar'));
|
||||||
|
|
||||||
$visitor5 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
|
$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 [
|
return [
|
||||||
[$visitor1, 'enterNode() returned invalid value of type string'],
|
[$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'],
|
[$visitor3, 'leaveNode() returned invalid value of type string'],
|
||||||
[$visitor4, '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'],
|
[$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'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user