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 |
||
14 | abstract class AbstractEnum extends AbstractValue implements Enum |
||
15 | { |
||
16 | /** @var array */ |
||
17 | private static $constantsCache = []; |
||
18 | |||
19 | /** @var array */ |
||
20 | private static $reflectionClassCache = []; |
||
21 | |||
22 | /** |
||
23 | * Returns array of all Enum instances index by their values. |
||
24 | * |
||
25 | * @return Enum[] Constant name => Enum |
||
26 | */ |
||
27 | public static function all() |
||
40 | |||
41 | /** |
||
42 | * Returns array of constant values. |
||
43 | * |
||
44 | * @return array |
||
45 | */ |
||
46 | public static function values() |
||
50 | |||
51 | /** |
||
52 | * Check if is valid enum value. |
||
53 | * |
||
54 | * @param $value |
||
55 | * |
||
56 | * @return bool |
||
57 | */ |
||
58 | public static function isValid($value) |
||
62 | |||
63 | /** |
||
64 | * Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant. |
||
65 | * |
||
66 | * @param string $name |
||
67 | * @param array $arguments |
||
68 | * |
||
69 | * @return static |
||
70 | * |
||
71 | * @throws \BadMethodCallException |
||
72 | */ |
||
73 | public static function __callStatic($name, array $arguments = []) |
||
85 | |||
86 | /** |
||
87 | * @return \ReflectionClass |
||
88 | */ |
||
89 | View Code Duplication | private static function getReflectionClass() |
|
98 | |||
99 | View Code Duplication | private static function getConstants() |
|
108 | |||
109 | private static function inspect($class) |
||
117 | } |
||
118 |
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.