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