PrintableNewAnonClassNode.php 2.2 KB

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