| 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 int Modifiers */ |
| 23 |
public $flags; |
| 24 |
/** @var Node\Arg[] Arguments */ |
| 25 |
public $args; |
| 26 |
/** @var null|Node\Name Name of extended class */ |
| 27 |
public $extends; |
| 28 |
/** @var Node\Name[] Names of implemented interfaces */ |
| 29 |
public $implements; |
| 30 |
/** @var Node\Stmt[] Statements */ |
| 31 |
public $stmts; |
| 32 |
|
| 33 |
public function __construct( |
| 34 |
array $attrGroups, int $flags, array $args, ?Node\Name $extends, array $implements, |
| 35 |
array $stmts, array $attributes |
| 36 |
) { |
| 37 |
parent::__construct($attributes); |
| 38 |
$this->attrGroups = $attrGroups; |
| 39 |
$this->flags = $flags; |
| 40 |
$this->args = $args; |
| 41 |
$this->extends = $extends; |
| 42 |
$this->implements = $implements; |
| 43 |
$this->stmts = $stmts; |
| 44 |
} |
| 45 |
|
| 46 |
public static function fromNewNode(Expr\New_ $newNode) { |
| 47 |
$class = $newNode->class; |
| 48 |
assert($class instanceof Node\Stmt\Class_); |
| 49 |
// We don't assert that $class->name is null here, to allow consumers to assign unique names |
| 50 |
// to anonymous classes for their own purposes. We simplify ignore the name here. |
| 51 |
return new self( |
| 52 |
$class->attrGroups, $class->flags, $newNode->args, $class->extends, $class->implements, |
| 53 |
$class->stmts, $newNode->getAttributes() |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
public function getType() : string { |
| 58 |
return 'Expr_PrintableNewAnonClass'; |
| 59 |
} |
| 60 |
|
| 61 |
public function getSubNodeNames() : array { |
| 62 |
return ['attrGroups', 'flags', 'args', 'extends', 'implements', 'stmts']; |
| 63 |
} |
| 64 |
} |
| 65 |
|