PrintableNewAnonClassNode.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\Arg[] Arguments */
  18. public $args;
  19. /** @var null|Node\Name Name of extended class */
  20. public $extends;
  21. /** @var Node\Name[] Names of implemented interfaces */
  22. public $implements;
  23. /** @var Node\Stmt[] Statements */
  24. public $stmts;
  25. public function __construct(
  26. array $args, Node\Name $extends = null, array $implements, array $stmts, array $attributes
  27. ) {
  28. parent::__construct($attributes);
  29. $this->args = $args;
  30. $this->extends = $extends;
  31. $this->implements = $implements;
  32. $this->stmts = $stmts;
  33. }
  34. public static function fromNewNode(Expr\New_ $newNode) {
  35. $class = $newNode->class;
  36. assert($class instanceof Node\Stmt\Class_);
  37. assert($class->name === null);
  38. return new self(
  39. $newNode->args, $class->extends, $class->implements,
  40. $class->stmts, $newNode->getAttributes()
  41. );
  42. }
  43. public function getType() : string {
  44. return 'Expr_PrintableNewAnonClass';
  45. }
  46. public function getSubNodeNames() : array {
  47. return ['args', 'extends', 'implements', 'stmts'];
  48. }
  49. }