Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
16 | abstract class ReflectionEnum implements Enum, \Serializable |
||
17 | { |
||
18 | /** |
||
19 | * @var mixed |
||
20 | */ |
||
21 | private $value = ''; |
||
22 | |||
23 | /** |
||
24 | * @var Enum[] |
||
25 | */ |
||
26 | private static $instances = []; |
||
27 | |||
28 | /** |
||
29 | * @var mixed[][] |
||
30 | */ |
||
31 | private static $create_methods = []; |
||
32 | |||
33 | |||
34 | /** |
||
35 | * @var mixed[][] |
||
36 | */ |
||
37 | private static $constants = []; |
||
38 | |||
39 | /** |
||
40 | * @var mixed[][] |
||
41 | */ |
||
42 | private static $choices = []; |
||
43 | |||
44 | /** |
||
45 | * @var EnumEnum[] |
||
46 | */ |
||
47 | private static $values = []; |
||
48 | |||
49 | /** |
||
50 | * @param mixed $value |
||
51 | */ |
||
52 | final private function __construct($value) |
||
56 | |||
57 | /** |
||
58 | * @param mixed $value |
||
59 | * |
||
60 | * @return Enum |
||
61 | */ |
||
62 | 23 | final public static function byValue($value) |
|
80 | |||
81 | /** |
||
82 | * @return mixed |
||
83 | */ |
||
84 | final public function value() |
||
88 | |||
89 | /** |
||
90 | * Available values. |
||
91 | * |
||
92 | * @return Enum[] |
||
93 | */ |
||
94 | 3 | View Code Duplication | final public static function values() |
107 | |||
108 | /** |
||
109 | * @param Enum $enum |
||
110 | * |
||
111 | * @return bool |
||
112 | */ |
||
113 | final public function equals(Enum $enum) |
||
117 | |||
118 | /** |
||
119 | * Is value supported. |
||
120 | * |
||
121 | * @param mixed $value |
||
122 | * |
||
123 | * @return bool |
||
124 | */ |
||
125 | 10 | final public static function isValid($value) |
|
129 | |||
130 | /** |
||
131 | * Get choices for radio group. |
||
132 | * |
||
133 | * <code> |
||
134 | * { |
||
135 | * value1: 'Readable value 1', |
||
136 | * value2: 'Readable value 2', |
||
137 | * } |
||
138 | * </code> |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | 3 | View Code Duplication | final public static function choices() |
155 | |||
156 | /** |
||
157 | * Return readable value. |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public function __toString() |
||
165 | |||
166 | final public function __clone() |
||
170 | |||
171 | /** |
||
172 | * @return mixed |
||
173 | */ |
||
174 | public function serialize() |
||
178 | |||
179 | /** |
||
180 | * @param mixed $data |
||
181 | */ |
||
182 | public function unserialize($data) |
||
186 | |||
187 | /** |
||
188 | * @param string $class |
||
189 | */ |
||
190 | 61 | private static function detectConstants($class) |
|
208 | |||
209 | /** |
||
210 | * @return array |
||
211 | */ |
||
212 | 16 | private static function constants() |
|
219 | |||
220 | /** |
||
221 | * @return string |
||
222 | */ |
||
223 | private function constant() |
||
227 | |||
228 | /** |
||
229 | * @param string $method |
||
230 | * @param array $arguments |
||
231 | * |
||
232 | * @return bool |
||
233 | */ |
||
234 | public function __call($method, array $arguments = []) |
||
245 | |||
246 | /** |
||
247 | * @param string $method |
||
248 | * @param array $arguments |
||
249 | * |
||
250 | * @return Enum |
||
251 | */ |
||
252 | 22 | public static function __callStatic($method, array $arguments = []) |
|
267 | } |
||
268 |