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 |
||
18 | abstract class Enum implements Serializable |
||
19 | { |
||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | private $id; |
||
24 | |||
25 | /** |
||
26 | * @var ReflectionClass[] |
||
27 | */ |
||
28 | private static $reflections = []; |
||
29 | |||
30 | /** |
||
31 | * @var ReflectionProperty[][] |
||
32 | */ |
||
33 | private static $properties = []; |
||
34 | |||
35 | /** |
||
36 | * @var Enum[][] |
||
37 | */ |
||
38 | private static $instances = []; |
||
39 | |||
40 | /** |
||
41 | * @param int $id |
||
42 | * |
||
43 | * @throws InvalidArgumentException |
||
44 | * @throws ReflectionException |
||
45 | */ |
||
46 | 8 | private function __construct(int $id) |
|
56 | |||
57 | /** |
||
58 | * @return string |
||
59 | */ |
||
60 | final public function __toString(): string |
||
64 | |||
65 | /** |
||
66 | * @param string $name |
||
67 | * @param array $arguments |
||
68 | * |
||
69 | * @throws ReflectionException |
||
70 | * @throws InvalidArgumentException |
||
71 | * @throws BadMethodCallException |
||
72 | * |
||
73 | * @return bool|string |
||
74 | */ |
||
75 | 5 | final public function __call(string $name, array $arguments) |
|
91 | |||
92 | /** |
||
93 | * @param string $name |
||
94 | * @param array $arguments |
||
95 | * |
||
96 | * @throws ReflectionException |
||
97 | * @throws BadMethodCallException |
||
98 | * @throws LogicException |
||
99 | * |
||
100 | * @return static |
||
101 | */ |
||
102 | 14 | final public static function __callStatic(string $name, array $arguments) |
|
121 | |||
122 | /** |
||
123 | * @param int $id |
||
124 | * |
||
125 | * @throws ReflectionException |
||
126 | * |
||
127 | * @return static |
||
128 | */ |
||
129 | 16 | final public static function create(int $id): self |
|
133 | |||
134 | /** |
||
135 | * @param string $property |
||
136 | * @param mixed $value |
||
137 | * |
||
138 | * @throws ReflectionException |
||
139 | * |
||
140 | * @return static |
||
141 | */ |
||
142 | 1 | final public static function from(string $property, $value): self |
|
146 | |||
147 | /** |
||
148 | * @return int |
||
149 | */ |
||
150 | 13 | final public function getId(): int |
|
154 | |||
155 | /** |
||
156 | * @throws ReflectionException |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | 5 | final public function getName(): string |
|
168 | |||
169 | /** |
||
170 | * @param string $property |
||
171 | * |
||
172 | * @return mixed |
||
173 | */ |
||
174 | 5 | final public function get(string $property) |
|
184 | |||
185 | /** |
||
186 | * @param Enum[]|int[] $ids |
||
187 | * @param bool $reverse |
||
188 | * |
||
189 | * @throws ReflectionException |
||
190 | * |
||
191 | * @return static[] |
||
192 | */ |
||
193 | 1 | final public static function all(array $ids = [], bool $reverse = false): array |
|
211 | |||
212 | /** |
||
213 | * @param Enum $enum |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | 5 | final public function eq(Enum $enum): bool |
|
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | 1 | final public function serialize(): string |
|
229 | |||
230 | /** |
||
231 | * @param string $serialized |
||
232 | */ |
||
233 | 1 | final public function unserialize($serialized): void |
|
237 | |||
238 | /** |
||
239 | * @param string $string |
||
240 | * |
||
241 | * @throws InvalidArgumentException |
||
242 | * |
||
243 | * @return string |
||
244 | */ |
||
245 | 14 | private static function stringToConstant(string $string): string |
|
255 | |||
256 | /** |
||
257 | * @throws ReflectionException |
||
258 | * @throws LogicException |
||
259 | * |
||
260 | * @return ReflectionClass |
||
261 | */ |
||
262 | 17 | private static function getReflection(): ReflectionClass |
|
268 | |||
269 | /** |
||
270 | * @param ReflectionClass $reflection |
||
271 | * |
||
272 | * @throws LogicException |
||
273 | * |
||
274 | * @return ReflectionClass |
||
275 | */ |
||
276 | 5 | private static function validate(ReflectionClass $reflection): ReflectionClass |
|
314 | } |
||
315 |
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.