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 |
||
8 | abstract class Enumeration implements Comparable |
||
9 | { |
||
10 | /** |
||
11 | * @var mixed |
||
12 | */ |
||
13 | private $value; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private static $constantsCache = []; |
||
19 | |||
20 | /** |
||
21 | * @param mixed $value |
||
22 | * @throws InvalidArgumentException |
||
23 | */ |
||
24 | final public function __construct($value) |
||
32 | |||
33 | /** |
||
34 | * @param string $value |
||
35 | * @return static |
||
36 | */ |
||
37 | final public static function fromValue($value) |
||
41 | |||
42 | /** |
||
43 | * @return mixed |
||
44 | */ |
||
45 | final public function getValue() |
||
49 | |||
50 | /** |
||
51 | * @param mixed $other |
||
52 | * @return bool |
||
53 | */ |
||
54 | View Code Duplication | public function equals($other) |
|
63 | |||
64 | /** |
||
65 | * @param string $value |
||
66 | * @return bool |
||
67 | */ |
||
68 | private function isKnown($value) |
||
72 | |||
73 | /** |
||
74 | * @return array |
||
75 | */ |
||
76 | private static function getConstants() |
||
86 | |||
87 | /** |
||
88 | * @param string $className |
||
89 | * @return array |
||
90 | */ |
||
91 | private static function getConstantsForClass($className) |
||
96 | |||
97 | /** |
||
98 | * @return string |
||
99 | */ |
||
100 | public function __toString() |
||
104 | } |
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.