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