1: <?php declare(strict_types=1);
2:
3: namespace PhpParser\Node\Stmt;
4:
5: use PhpParser\Error;
6: use PhpParser\Node;
7:
8: class Class_ extends ClassLike
9: {
10: const MODIFIER_PUBLIC = 1;
11: const MODIFIER_PROTECTED = 2;
12: const MODIFIER_PRIVATE = 4;
13: const MODIFIER_STATIC = 8;
14: const MODIFIER_ABSTRACT = 16;
15: const MODIFIER_FINAL = 32;
16: const MODIFIER_READONLY = 64;
17:
18: const VISIBILITY_MODIFIER_MASK = 7; // 1 | 2 | 4
19:
20: /** @var int Type */
21: public $flags;
22: /** @var null|Node\Name Name of extended class */
23: public $extends;
24: /** @var Node\Name[] Names of implemented interfaces */
25: public $implements;
26:
27: /**
28: * Constructs a class node.
29: *
30: * @param string|Node\Identifier|null $name Name
31: * @param array $subNodes Array of the following optional subnodes:
32: * 'flags' => 0 : Flags
33: * 'extends' => null : Name of extended class
34: * 'implements' => array(): Names of implemented interfaces
35: * 'stmts' => array(): Statements
36: * 'attrGroups' => array(): PHP attribute groups
37: * @param array $attributes Additional attributes
38: */
39: public function __construct($name, array $subNodes = [], array $attributes = []) {
40: $this->attributes = $attributes;
41: $this->flags = $subNodes['flags'] ?? $subNodes['type'] ?? 0;
42: $this->name = \is_string($name) ? new Node\Identifier($name) : $name;
43: $this->extends = $subNodes['extends'] ?? null;
44: $this->implements = $subNodes['implements'] ?? [];
45: $this->stmts = $subNodes['stmts'] ?? [];
46: $this->attrGroups = $subNodes['attrGroups'] ?? [];
47: }
48:
49: public function getSubNodeNames() : array {
50: return ['attrGroups', 'flags', 'name', 'extends', 'implements', 'stmts'];
51: }
52:
53: /**
54: * Whether the class is explicitly abstract.
55: *
56: * @return bool
57: */
58: public function isAbstract() : bool {
59: return (bool) ($this->flags & self::MODIFIER_ABSTRACT);
60: }
61:
62: /**
63: * Whether the class is final.
64: *
65: * @return bool
66: */
67: public function isFinal() : bool {
68: return (bool) ($this->flags & self::MODIFIER_FINAL);
69: }
70:
71: public function isReadonly() : bool {
72: return (bool) ($this->flags & self::MODIFIER_READONLY);
73: }
74:
75: /**
76: * Whether the class is anonymous.
77: *
78: * @return bool
79: */
80: public function isAnonymous() : bool {
81: return null === $this->name;
82: }
83:
84: /**
85: * @internal
86: */
87: public static function verifyClassModifier($a, $b) {
88: if ($a & self::MODIFIER_ABSTRACT && $b & self::MODIFIER_ABSTRACT) {
89: throw new Error('Multiple abstract modifiers are not allowed');
90: }
91:
92: if ($a & self::MODIFIER_FINAL && $b & self::MODIFIER_FINAL) {
93: throw new Error('Multiple final modifiers are not allowed');
94: }
95:
96: if ($a & self::MODIFIER_READONLY && $b & self::MODIFIER_READONLY) {
97: throw new Error('Multiple readonly modifiers are not allowed');
98: }
99:
100: if ($a & 48 && $b & 48) {
101: throw new Error('Cannot use the final modifier on an abstract class');
102: }
103: }
104:
105: /**
106: * @internal
107: */
108: public static function verifyModifier($a, $b) {
109: if ($a & self::VISIBILITY_MODIFIER_MASK && $b & self::VISIBILITY_MODIFIER_MASK) {
110: throw new Error('Multiple access type modifiers are not allowed');
111: }
112:
113: if ($a & self::MODIFIER_ABSTRACT && $b & self::MODIFIER_ABSTRACT) {
114: throw new Error('Multiple abstract modifiers are not allowed');
115: }
116:
117: if ($a & self::MODIFIER_STATIC && $b & self::MODIFIER_STATIC) {
118: throw new Error('Multiple static modifiers are not allowed');
119: }
120:
121: if ($a & self::MODIFIER_FINAL && $b & self::MODIFIER_FINAL) {
122: throw new Error('Multiple final modifiers are not allowed');
123: }
124:
125: if ($a & self::MODIFIER_READONLY && $b & self::MODIFIER_READONLY) {
126: throw new Error('Multiple readonly modifiers are not allowed');
127: }
128:
129: if ($a & 48 && $b & 48) {
130: throw new Error('Cannot use the final modifier on an abstract class member');
131: }
132: }
133:
134: public function getType() : string {
135: return 'Stmt_Class';
136: }
137: }
138: