1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Prophecy. |
5
|
|
|
* (c) Konstantin Kudryashov <[email protected]> |
6
|
|
|
* Marcello Duarte <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Prophecy\Doubler\Generator\Node; |
13
|
|
|
|
14
|
|
|
use Prophecy\Exception\Doubler\MethodNotExtendableException; |
15
|
|
|
use Prophecy\Exception\InvalidArgumentException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class node. |
19
|
|
|
* |
20
|
|
|
* @author Konstantin Kudryashov <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class ClassNode |
23
|
|
|
{ |
24
|
|
|
private $parentClass = 'stdClass'; |
25
|
|
|
private $interfaces = array(); |
26
|
|
|
private $properties = array(); |
27
|
|
|
private $unextendableMethods = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var MethodNode[] |
31
|
|
|
*/ |
32
|
|
|
private $methods = array(); |
33
|
|
|
|
34
|
|
|
public function getParentClass() |
35
|
|
|
{ |
36
|
|
|
return $this->parentClass; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $class |
41
|
|
|
*/ |
42
|
|
|
public function setParentClass($class) |
43
|
|
|
{ |
44
|
|
|
$this->parentClass = $class ?: 'stdClass'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return string[] |
49
|
|
|
*/ |
50
|
|
|
public function getInterfaces() |
51
|
|
|
{ |
52
|
|
|
return $this->interfaces; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $interface |
57
|
|
|
*/ |
58
|
|
|
public function addInterface($interface) |
59
|
|
|
{ |
60
|
|
|
if ($this->hasInterface($interface)) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
array_unshift($this->interfaces, $interface); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $interface |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function hasInterface($interface) |
73
|
|
|
{ |
74
|
|
|
return in_array($interface, $this->interfaces); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getProperties() |
78
|
|
|
{ |
79
|
|
|
return $this->properties; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function addProperty($name, $visibility = 'public') |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$visibility = strtolower($visibility); |
85
|
|
|
|
86
|
|
|
if (!in_array($visibility, array('public', 'private', 'protected'))) { |
87
|
|
|
throw new InvalidArgumentException(sprintf( |
88
|
|
|
'`%s` property visibility is not supported.', $visibility |
89
|
|
|
)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->properties[$name] = $visibility; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return MethodNode[] |
97
|
|
|
*/ |
98
|
|
|
public function getMethods() |
99
|
|
|
{ |
100
|
|
|
return $this->methods; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function addMethod(MethodNode $method) |
104
|
|
|
{ |
105
|
|
|
if (!$this->isExtendable($method->getName())){ |
106
|
|
|
$message = sprintf( |
107
|
|
|
'Method `%s` is not extendable, so can not be added.', $method->getName() |
108
|
|
|
); |
109
|
|
|
throw new MethodNotExtendableException($message, $this->getParentClass(), $method->getName()); |
110
|
|
|
} |
111
|
|
|
$this->methods[$method->getName()] = $method; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function removeMethod($name) |
115
|
|
|
{ |
116
|
|
|
unset($this->methods[$name]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $name |
121
|
|
|
* |
122
|
|
|
* @return MethodNode|null |
123
|
|
|
*/ |
124
|
|
|
public function getMethod($name) |
125
|
|
|
{ |
126
|
|
|
return $this->hasMethod($name) ? $this->methods[$name] : null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $name |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function hasMethod($name) |
135
|
|
|
{ |
136
|
|
|
return isset($this->methods[$name]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return string[] |
141
|
|
|
*/ |
142
|
|
|
public function getUnextendableMethods() |
143
|
|
|
{ |
144
|
|
|
return $this->unextendableMethods; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $unextendableMethod |
149
|
|
|
*/ |
150
|
|
|
public function addUnextendableMethod($unextendableMethod) |
151
|
|
|
{ |
152
|
|
|
if (!$this->isExtendable($unextendableMethod)){ |
153
|
|
|
return; |
154
|
|
|
} |
155
|
|
|
$this->unextendableMethods[] = $unextendableMethod; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $method |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function isExtendable($method) |
163
|
|
|
{ |
164
|
|
|
return !in_array($method, $this->unextendableMethods); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
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.