1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpParser\Node\Stmt; |
4
|
|
|
|
5
|
|
|
use PhpParser\Node; |
6
|
|
|
use PhpParser\Error; |
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
|
|
|
|
17
|
|
|
const VISIBILITY_MODIFER_MASK = 7; // 1 | 2 | 4 |
18
|
|
|
|
19
|
|
|
/** @var int Type */ |
20
|
|
|
public $type; |
21
|
|
|
/** @var null|Node\Name Name of extended class */ |
22
|
|
|
public $extends; |
23
|
|
|
/** @var Node\Name[] Names of implemented interfaces */ |
24
|
|
|
public $implements; |
25
|
|
|
|
26
|
|
|
protected static $specialNames = array( |
27
|
|
|
'self' => true, |
28
|
|
|
'parent' => true, |
29
|
|
|
'static' => true, |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructs a class node. |
34
|
|
|
* |
35
|
|
|
* @param string|null $name Name |
36
|
|
|
* @param array $subNodes Array of the following optional subnodes: |
37
|
|
|
* 'type' => 0 : Type |
38
|
|
|
* 'extends' => null : Name of extended class |
39
|
|
|
* 'implements' => array(): Names of implemented interfaces |
40
|
|
|
* 'stmts' => array(): Statements |
41
|
|
|
* @param array $attributes Additional attributes |
42
|
|
|
*/ |
43
|
|
|
public function __construct($name, array $subNodes = array(), array $attributes = array()) { |
44
|
|
|
parent::__construct($attributes); |
45
|
|
|
$this->type = isset($subNodes['type']) ? $subNodes['type'] : 0; |
46
|
|
|
$this->name = $name; |
47
|
|
|
$this->extends = isset($subNodes['extends']) ? $subNodes['extends'] : null; |
48
|
|
|
$this->implements = isset($subNodes['implements']) ? $subNodes['implements'] : array(); |
49
|
|
|
$this->stmts = isset($subNodes['stmts']) ? $subNodes['stmts'] : array(); |
50
|
|
|
|
51
|
|
View Code Duplication |
if (null !== $this->name && isset(self::$specialNames[strtolower($this->name)])) { |
|
|
|
|
52
|
|
|
throw new Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->name)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
if (isset(self::$specialNames[strtolower($this->extends)])) { |
|
|
|
|
56
|
|
|
throw new Error( |
57
|
|
|
sprintf('Cannot use \'%s\' as class name as it is reserved', $this->extends), |
58
|
|
|
$this->extends->getAttributes() |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
foreach ($this->implements as $interface) { |
|
|
|
|
63
|
|
|
if (isset(self::$specialNames[strtolower($interface)])) { |
64
|
|
|
throw new Error( |
65
|
|
|
sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface), |
66
|
|
|
$interface->getAttributes() |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getSubNodeNames() { |
73
|
|
|
return array('type', 'name', 'extends', 'implements', 'stmts'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function isAbstract() { |
77
|
|
|
return (bool) ($this->type & self::MODIFIER_ABSTRACT); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function isFinal() { |
81
|
|
|
return (bool) ($this->type & self::MODIFIER_FINAL); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function isAnonymous() { |
85
|
|
|
return null === $this->name; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @internal |
90
|
|
|
*/ |
91
|
|
|
public static function verifyModifier($a, $b) { |
92
|
|
|
if ($a & self::VISIBILITY_MODIFER_MASK && $b & self::VISIBILITY_MODIFER_MASK) { |
93
|
|
|
throw new Error('Multiple access type modifiers are not allowed'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($a & self::MODIFIER_ABSTRACT && $b & self::MODIFIER_ABSTRACT) { |
97
|
|
|
throw new Error('Multiple abstract modifiers are not allowed'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($a & self::MODIFIER_STATIC && $b & self::MODIFIER_STATIC) { |
101
|
|
|
throw new Error('Multiple static modifiers are not allowed'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($a & self::MODIFIER_FINAL && $b & self::MODIFIER_FINAL) { |
105
|
|
|
throw new Error('Multiple final modifiers are not allowed'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($a & 48 && $b & 48) { |
109
|
|
|
throw new Error('Cannot use the final modifier on an abstract class member'); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.