PrintableNewAnonClassNode.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. // We don't assert that $class->name is null here, to allow consumers to assign unique names
  38. // to anonymous classes for their own purposes. We simplify ignore the name here.
  39. return new self(
  40. $newNode->args, $class->extends, $class->implements,
  41. $class->stmts, $newNode->getAttributes()
  42. );
  43. }
  44. public function getType() : string {
  45. return 'Expr_PrintableNewAnonClass';
  46. }
  47. public function getSubNodeNames() : array {
  48. return ['args', 'extends', 'implements', 'stmts'];
  49. }
  50. }