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 |
||
13 | abstract class AbstractEnum |
||
14 | { |
||
15 | protected $value; |
||
16 | |||
17 | 15 | protected function __construct($value) |
|
32 | |||
33 | /** |
||
34 | * @return static |
||
35 | */ |
||
36 | 13 | public static function get($value): self |
|
40 | |||
41 | 3 | View Code Duplication | public static function __callStatic(string $methodName, array $arguments): self |
52 | |||
53 | 3 | View Code Duplication | public function __call(string $methodName, array $arguments) |
63 | |||
64 | |||
65 | /** |
||
66 | * @return mixed |
||
67 | */ |
||
68 | 8 | public function getValue() |
|
72 | |||
73 | 6 | public function equals(self $other): bool |
|
77 | |||
78 | 3 | public function __toString(): string |
|
82 | |||
83 | 15 | protected function isValid($value): bool |
|
87 | |||
88 | 16 | public static function getValidOptions(): array |
|
92 | |||
93 | 4 | protected function getClassName(): string |
|
97 | |||
98 | 17 | private static function getConstants(): array |
|
102 | |||
103 | 4 | private static function getValidOptionsAsString(): string |
|
112 | } |
||
113 |
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.