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