| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
|
|
|
3 |
namespace PhpParser\Internal;
|
|
|
4 |
|
|
|
5 |
use PhpParser\Node;
|
|
|
6 |
use PhpParser\Node\Expr;
|
|
|
7 |
|
|
|
8 |
/**
|
|
|
9 |
* This node is used internally by the format-preserving pretty printer to print anonymous classes.
|
|
|
10 |
*
|
|
|
11 |
* The normal anonymous class structure violates assumptions about the order of token offsets.
|
|
|
12 |
* Namely, the constructor arguments are part of the Expr\New_ node and follow the class node, even
|
|
|
13 |
* though they are actually interleaved with them. This special node type is used temporarily to
|
|
|
14 |
* restore a sane token offset order.
|
|
|
15 |
*
|
|
|
16 |
* @internal
|
|
|
17 |
*/
|
|
|
18 |
class PrintableNewAnonClassNode extends Expr
|
|
|
19 |
{
|
|
|
20 |
/** @var Node\AttributeGroup[] PHP attribute groups */
|
|
|
21 |
public $attrGroups;
|
|
|
22 |
/** @var Node\Arg[] Arguments */
|
|
|
23 |
public $args;
|
|
|
24 |
/** @var null|Node\Name Name of extended class */
|
|
|
25 |
public $extends;
|
|
|
26 |
/** @var Node\Name[] Names of implemented interfaces */
|
|
|
27 |
public $implements;
|
|
|
28 |
/** @var Node\Stmt[] Statements */
|
|
|
29 |
public $stmts;
|
|
|
30 |
|
|
|
31 |
public function __construct(
|
|
|
32 |
array $attrGroups, array $args, Node\Name $extends = null, array $implements,
|
|
|
33 |
array $stmts, array $attributes
|
|
|
34 |
) {
|
|
|
35 |
parent::__construct($attributes);
|
|
|
36 |
$this->attrGroups = $attrGroups;
|
|
|
37 |
$this->args = $args;
|
|
|
38 |
$this->extends = $extends;
|
|
|
39 |
$this->implements = $implements;
|
|
|
40 |
$this->stmts = $stmts;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
public static function fromNewNode(Expr\New_ $newNode) {
|
|
|
44 |
$class = $newNode->class;
|
|
|
45 |
assert($class instanceof Node\Stmt\Class_);
|
|
|
46 |
// We don't assert that $class->name is null here, to allow consumers to assign unique names
|
|
|
47 |
// to anonymous classes for their own purposes. We simplify ignore the name here.
|
|
|
48 |
return new self(
|
|
|
49 |
$class->attrGroups, $newNode->args, $class->extends, $class->implements,
|
|
|
50 |
$class->stmts, $newNode->getAttributes()
|
|
|
51 |
);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
public function getType() : string {
|
|
|
55 |
return 'Expr_PrintableNewAnonClass';
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
public function getSubNodeNames() : array {
|
|
|
59 |
return ['attrGroups', 'args', 'extends', 'implements', 'stmts'];
|
|
|
60 |
}
|
|
|
61 |
}
|