Rename Expr\ClosureUse -> ClosureUse

This is not a real expression, treat it similarly to Node\Arg
or Node\Param.

The old name is retained as an alias for compatibility.
This commit is contained in:
Nikita Popov 2022-06-12 21:55:56 +02:00
parent 23be1f9bd1
commit b0469d127e
10 changed files with 73 additions and 40 deletions

View File

@ -59,6 +59,14 @@ $parser = $factory->create(ParserFactory::ONLY_PHP5);
$parser = $factory->createForVersion("5.6");
```
### Renamed nodes
A number of AST nodes have been renamed or moved in the AST hierarchy:
* `Node\Expr\ClosureUse` is now `Node\ClosureUse` and no longer extends `Node\Expr`. The `ClosureUse` node can only occur inside closure use lists, not as a general expression.
The old class names have been retained as aliases for backwards compatibility. However, the `Node::getType()` method will now always return the new name (e.g. `ClosureUse` instead of `Expr_ClosureUse`).
### Changes to the default pretty printer
A number of changes to the standard pretty printer have been made, to make it match contemporary coding style conventions (and in particular PSR-12). Options to restore the previous behavior are not provided, but it is possible to override the formatting methods (such as `pStmt_ClassMethod`) with your preferred formatting.

View File

@ -0,0 +1,37 @@
<?php declare(strict_types=1);
namespace PhpParser\Node;
use PhpParser\NodeAbstract;
class ClosureUse extends NodeAbstract
{
/** @var Expr\Variable Variable to use */
public $var;
/** @var bool Whether to use by reference */
public $byRef;
/**
* Constructs a closure use node.
*
* @param Expr\Variable $var Variable to use
* @param bool $byRef Whether to use by reference
* @param array $attributes Additional attributes
*/
public function __construct(Expr\Variable $var, bool $byRef = false, array $attributes = []) {
$this->attributes = $attributes;
$this->var = $var;
$this->byRef = $byRef;
}
public function getSubNodeNames() : array {
return ['var', 'byRef'];
}
public function getType() : string {
return 'ClosureUse';
}
}
// @deprecated compatibility alias
class_alias(ClosureUse::class, Expr\ClosureUse::class);

View File

@ -3,6 +3,7 @@
namespace PhpParser\Node\Expr;
use PhpParser\Node;
use PhpParser\Node\ClosureUse;
use PhpParser\Node\Expr;
use PhpParser\Node\FunctionLike;

View File

@ -1,34 +1,3 @@
<?php declare(strict_types=1);
namespace PhpParser\Node\Expr;
use PhpParser\Node\Expr;
class ClosureUse extends Expr
{
/** @var Expr\Variable Variable to use */
public $var;
/** @var bool Whether to use by reference */
public $byRef;
/**
* Constructs a closure use node.
*
* @param Expr\Variable $var Variable to use
* @param bool $byRef Whether to use by reference
* @param array $attributes Additional attributes
*/
public function __construct(Expr\Variable $var, bool $byRef = false, array $attributes = []) {
$this->attributes = $attributes;
$this->var = $var;
$this->byRef = $byRef;
}
public function getSubNodeNames() : array {
return ['var', 'byRef'];
}
public function getType() : string {
return 'Expr_ClosureUse';
}
}
require __DIR__ . '/../ClosureUse.php';

View File

@ -2465,7 +2465,7 @@ class Php7 extends \PhpParser\ParserAbstract
$this->semStack[$stackPos-(3-1)][] = $this->semStack[$stackPos-(3-3)]; $this->semValue = $this->semStack[$stackPos-(3-1)];
},
480 => function ($stackPos) {
$this->semValue = new Expr\ClosureUse($this->semStack[$stackPos-(2-2)], $this->semStack[$stackPos-(2-1)], $this->startAttributeStack[$stackPos-(2-1)] + $this->endAttributes);
$this->semValue = new Node\ClosureUse($this->semStack[$stackPos-(2-2)], $this->semStack[$stackPos-(2-1)], $this->startAttributeStack[$stackPos-(2-1)] + $this->endAttributes);
},
481 => function ($stackPos) {
$this->semValue = new Expr\FuncCall($this->semStack[$stackPos-(2-1)], $this->semStack[$stackPos-(2-2)], $this->startAttributeStack[$stackPos-(2-1)] + $this->endAttributes);

View File

@ -649,7 +649,7 @@ class Standard extends PrettyPrinterAbstract
. $this->p($node->expr);
}
protected function pExpr_ClosureUse(Expr\ClosureUse $node) {
protected function pClosureUse(Node\ClosureUse $node) {
return ($node->byRef ? '&' : '') . $this->p($node->var);
}

View File

@ -0,0 +1,18 @@
<?php declare(strict_types=1);
namespace PhpParser;
use PhpParser\Node\Expr;
class CompatibilityTest extends \PHPUnit\Framework\TestCase
{
public function testAliases1() {
$node = new Node\ClosureUse(new Expr\Variable('x'));
$this->assertTrue($node instanceof Expr\ClosureUse);
}
public function testAliases2() {
$node = new Node\Expr\ClosureUse(new Expr\Variable('x'));
$this->assertTrue($node instanceof Node\ClosureUse);
}
}

View File

@ -63,7 +63,7 @@ array(
)
)
uses: array(
0: Expr_ClosureUse(
0: ClosureUse(
var: Expr_Variable(
name: b
)
@ -84,13 +84,13 @@ array(
params: array(
)
uses: array(
0: Expr_ClosureUse(
0: ClosureUse(
var: Expr_Variable(
name: a
)
byRef: false
)
1: Expr_ClosureUse(
1: ClosureUse(
var: Expr_Variable(
name: b
)
@ -182,7 +182,7 @@ array(
params: array(
)
uses: array(
0: Expr_ClosureUse(
0: ClosureUse(
var: Expr_Variable(
name: a
)

View File

@ -13,7 +13,7 @@ array(
params: array(
)
uses: array(
0: Expr_ClosureUse(
0: ClosureUse(
var: Expr_Variable(
name: a
)

View File

@ -302,7 +302,7 @@ array(
)
)
uses: array(
0: Expr_ClosureUse(
0: ClosureUse(
var: Expr_Variable(
name: f
)