xref: /PHP-Parser/lib/PhpParser/Node/Stmt/Class_.php (revision ab51e9d3)
1<?php declare(strict_types=1);
2
3namespace PhpParser\Node\Stmt;
4
5use PhpParser\Modifiers;
6use PhpParser\Node;
7
8class Class_ extends ClassLike {
9    /** @deprecated Use Modifiers::PUBLIC instead */
10    public const MODIFIER_PUBLIC    =  1;
11    /** @deprecated Use Modifiers::PROTECTED instead */
12    public const MODIFIER_PROTECTED =  2;
13    /** @deprecated Use Modifiers::PRIVATE instead */
14    public const MODIFIER_PRIVATE   =  4;
15    /** @deprecated Use Modifiers::STATIC instead */
16    public const MODIFIER_STATIC    =  8;
17    /** @deprecated Use Modifiers::ABSTRACT instead */
18    public const MODIFIER_ABSTRACT  = 16;
19    /** @deprecated Use Modifiers::FINAL instead */
20    public const MODIFIER_FINAL     = 32;
21    /** @deprecated Use Modifiers::READONLY instead */
22    public const MODIFIER_READONLY  = 64;
23
24    /** @deprecated Use Modifiers::VISIBILITY_MASK instead */
25    public const VISIBILITY_MODIFIER_MASK = 7; // 1 | 2 | 4
26
27    /** @var int Modifiers */
28    public int $flags;
29    /** @var null|Node\Name Name of extended class */
30    public ?Node\Name $extends;
31    /** @var Node\Name[] Names of implemented interfaces */
32    public array $implements;
33
34    /**
35     * Constructs a class node.
36     *
37     * @param string|Node\Identifier|null $name Name
38     * @param array{
39     *     flags?: int,
40     *     extends?: Node\Name|null,
41     *     implements?: Node\Name[],
42     *     stmts?: Node\Stmt[],
43     *     attrGroups?: Node\AttributeGroup[],
44     * } $subNodes Array of the following optional subnodes:
45     *             'flags'       => 0      : Flags
46     *             'extends'     => null   : Name of extended class
47     *             'implements'  => array(): Names of implemented interfaces
48     *             'stmts'       => array(): Statements
49     *             'attrGroups'  => array(): PHP attribute groups
50     * @param array<string, mixed> $attributes Additional attributes
51     */
52    public function __construct($name, array $subNodes = [], array $attributes = []) {
53        $this->attributes = $attributes;
54        $this->flags = $subNodes['flags'] ?? $subNodes['type'] ?? 0;
55        $this->name = \is_string($name) ? new Node\Identifier($name) : $name;
56        $this->extends = $subNodes['extends'] ?? null;
57        $this->implements = $subNodes['implements'] ?? [];
58        $this->stmts = $subNodes['stmts'] ?? [];
59        $this->attrGroups = $subNodes['attrGroups'] ?? [];
60    }
61
62    public function getSubNodeNames(): array {
63        return ['attrGroups', 'flags', 'name', 'extends', 'implements', 'stmts'];
64    }
65
66    /**
67     * Whether the class is explicitly abstract.
68     */
69    public function isAbstract(): bool {
70        return (bool) ($this->flags & Modifiers::ABSTRACT);
71    }
72
73    /**
74     * Whether the class is final.
75     */
76    public function isFinal(): bool {
77        return (bool) ($this->flags & Modifiers::FINAL);
78    }
79
80    public function isReadonly(): bool {
81        return (bool) ($this->flags & Modifiers::READONLY);
82    }
83
84    /**
85     * Whether the class is anonymous.
86     */
87    public function isAnonymous(): bool {
88        return null === $this->name;
89    }
90
91    public function getType(): string {
92        return 'Stmt_Class';
93    }
94}
95