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 BaseEnum |
||
17 | { |
||
18 | /** |
||
19 | * @var string message category |
||
20 | */ |
||
21 | public static $messageCategory = 'app'; |
||
22 | |||
23 | /** |
||
24 | * The cached list of constants by name. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected static $byName = []; |
||
29 | |||
30 | /** |
||
31 | * The cached list of constants by value. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected static $byValue = []; |
||
36 | |||
37 | /** |
||
38 | * @var array list of properties |
||
39 | */ |
||
40 | protected static $list = []; |
||
41 | |||
42 | /** |
||
43 | * The value managed by this type instance. |
||
44 | * |
||
45 | * @var mixed |
||
46 | */ |
||
47 | protected $value; |
||
48 | |||
49 | /** |
||
50 | * Sets the value that will be managed by this type instance. |
||
51 | * |
||
52 | * @param mixed $value The value to be managed |
||
53 | * |
||
54 | * @throws UnexpectedValueException If the value is not valid |
||
55 | */ |
||
56 | public function __construct($value) |
||
64 | |||
65 | /** |
||
66 | * Creates a new type instance using the name of a value. |
||
67 | * |
||
68 | * @param string $name The name of a value |
||
69 | * |
||
70 | * @throws UnexpectedValueException |
||
71 | * |
||
72 | * @return $this The new type instance |
||
73 | */ |
||
74 | View Code Duplication | public static function createByName($name) |
|
84 | |||
85 | /** |
||
86 | * get constant key by value(label) |
||
87 | * |
||
88 | * @param $value |
||
89 | * |
||
90 | * @return mixed |
||
91 | */ |
||
92 | public static function getValueByName($value) |
||
96 | |||
97 | /** |
||
98 | * Creates a new type instance using the value. |
||
99 | * |
||
100 | * @param mixed $value The value |
||
101 | * |
||
102 | * @throws UnexpectedValueException |
||
103 | * |
||
104 | * @return $this The new type instance |
||
105 | */ |
||
106 | public static function createByValue($value) |
||
114 | |||
115 | /** |
||
116 | * Get list data |
||
117 | * |
||
118 | * @return mixed |
||
119 | */ |
||
120 | public static function listData() |
||
126 | |||
127 | /** |
||
128 | * Get label by value |
||
129 | * |
||
130 | * @var string value |
||
131 | * |
||
132 | * @return string label |
||
133 | */ |
||
134 | public static function getLabel($value) |
||
144 | |||
145 | /** |
||
146 | * Returns the list of constants (by name) for this type. |
||
147 | * |
||
148 | * @return array The list of constants by name |
||
149 | */ |
||
150 | public static function getConstantsByName() |
||
161 | |||
162 | /** |
||
163 | * Returns the list of constants (by value) for this type. |
||
164 | * |
||
165 | * @return array The list of constants by value |
||
166 | */ |
||
167 | public static function getConstantsByValue() |
||
177 | |||
178 | /** |
||
179 | * Returns the name of the value. |
||
180 | * |
||
181 | * @return array|string The name, or names, of the value |
||
182 | */ |
||
183 | public function getName() |
||
189 | |||
190 | /** |
||
191 | * Unwraps the type and returns the raw value. |
||
192 | * |
||
193 | * @return mixed The raw value managed by the type instance |
||
194 | */ |
||
195 | public function getValue() |
||
199 | |||
200 | /** |
||
201 | * Checks if a name is valid for this type. |
||
202 | * |
||
203 | * @param string $name The name of the value |
||
204 | * |
||
205 | * @return bool If the name is valid for this type, `true` is returned. |
||
206 | * Otherwise, the name is not valid and `false` is returned |
||
207 | */ |
||
208 | public static function isValidName($name) |
||
212 | |||
213 | /** |
||
214 | * Checks if a value is valid for this type. |
||
215 | * |
||
216 | * @param string $value The value |
||
217 | * |
||
218 | * @return bool If the value is valid for this type, `true` is returned. |
||
219 | * Otherwise, the value is not valid and `false` is returned |
||
220 | */ |
||
221 | public static function isValidValue($value) |
||
225 | |||
226 | /** |
||
227 | * Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant |
||
228 | * |
||
229 | * @param string $name |
||
230 | * @param array $arguments |
||
231 | * |
||
232 | * @return static |
||
233 | * |
||
234 | * @throws BadMethodCallException |
||
235 | */ |
||
236 | View Code Duplication | public static function __callStatic($name, $arguments) |
|
246 | |||
247 | /** |
||
248 | * @return string |
||
249 | */ |
||
250 | public function __toString() |
||
254 | } |
||
255 |
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.