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 | * @var mixed[][] |
||
35 | */ |
||
36 | private static $is_methods = []; |
||
37 | |||
38 | /** |
||
39 | * @var mixed[][] |
||
40 | */ |
||
41 | private static $constants = []; |
||
42 | |||
43 | /** |
||
44 | * @param mixed $value |
||
45 | */ |
||
46 | 2 | final private function __construct($value) |
|
50 | |||
51 | /** |
||
52 | * @param mixed $value |
||
53 | * |
||
54 | * @return Enum |
||
55 | */ |
||
56 | 30 | final public static function byValue($value) |
|
74 | |||
75 | /** |
||
76 | * @return mixed |
||
77 | */ |
||
78 | 20 | final public function value() |
|
82 | |||
83 | /** |
||
84 | * Available values. |
||
85 | * |
||
86 | * @return Enum[] |
||
87 | */ |
||
88 | 2 | final public static function values() |
|
97 | |||
98 | /** |
||
99 | * @param Enum $enum |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | 8 | final public function equals(Enum $enum) |
|
107 | |||
108 | /** |
||
109 | * Is value supported. |
||
110 | * |
||
111 | * @param mixed $value |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | 8 | final public static function isValid($value) |
|
119 | |||
120 | /** |
||
121 | * Get choices for radio group. |
||
122 | * |
||
123 | * <code> |
||
124 | * { |
||
125 | * value1: 'Readable value 1', |
||
126 | * value2: 'Readable value 2', |
||
127 | * } |
||
128 | * </code> |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | 2 | final public static function choices() |
|
141 | |||
142 | /** |
||
143 | * Return readable value. |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | 10 | public function __toString() |
|
151 | |||
152 | 2 | final public function __clone() |
|
156 | |||
157 | /** |
||
158 | * @return mixed |
||
159 | */ |
||
160 | 8 | public function serialize() |
|
164 | |||
165 | /** |
||
166 | * @param mixed $data |
||
167 | */ |
||
168 | 8 | public function unserialize($data) |
|
172 | |||
173 | /** |
||
174 | * @param string $class |
||
175 | */ |
||
176 | 42 | private static function detectConstants($class) |
|
212 | |||
213 | /** |
||
214 | * @return array |
||
215 | */ |
||
216 | 12 | private static function constants() |
|
223 | |||
224 | /** |
||
225 | * @return string |
||
226 | */ |
||
227 | 10 | private function constant() |
|
231 | |||
232 | /** |
||
233 | * @param string $method |
||
234 | * @param array $arguments |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | 10 | public function __call($method, array $arguments = []) |
|
249 | |||
250 | /** |
||
251 | * @param string $method |
||
252 | * @param array $arguments |
||
253 | * |
||
254 | * @return Enum |
||
255 | */ |
||
256 | 14 | public static function __callStatic($method, array $arguments = []) |
|
271 | } |
||
272 |
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.