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 declare(strict_types = 1); |
||
14 | class Evacuator |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | * @internal |
||
19 | */ |
||
20 | const CATCH_ALL_EXCEPTIONS = '*'; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | const INFINITY_RETRIES = -100; |
||
26 | |||
27 | /** |
||
28 | * @var \Closure |
||
29 | */ |
||
30 | private $context; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | private $retries = 0; |
||
36 | |||
37 | /** |
||
38 | * @var array|\Closure[] |
||
39 | */ |
||
40 | private $catches = []; |
||
41 | |||
42 | /** |
||
43 | * @var null|\Closure |
||
44 | */ |
||
45 | private $then; |
||
46 | |||
47 | /** |
||
48 | * @var array|\Closure |
||
49 | */ |
||
50 | private $everyError = []; |
||
51 | |||
52 | /** |
||
53 | * Evacuator constructor. |
||
54 | * @param \Closure $context |
||
55 | */ |
||
56 | public function __construct(\Closure $context) |
||
60 | |||
61 | /** |
||
62 | * @param array ...$args |
||
63 | * @return mixed |
||
64 | * @throws \Throwable |
||
65 | */ |
||
66 | public function __invoke(...$args) |
||
70 | |||
71 | /** |
||
72 | * @param int $count |
||
73 | * @return Evacuator |
||
74 | * @throws \InvalidArgumentException |
||
75 | */ |
||
76 | public function retries(int $count): Evacuator |
||
86 | |||
87 | /** |
||
88 | * @param \Closure $then |
||
89 | * @return Evacuator|$this |
||
90 | * @throws \InvalidArgumentException |
||
91 | */ |
||
92 | View Code Duplication | public function catch(\Closure $then): Evacuator |
|
100 | |||
101 | /** |
||
102 | * @param \Closure $then |
||
103 | * @return Evacuator |
||
104 | * @throws \InvalidArgumentException |
||
105 | */ |
||
106 | View Code Duplication | public function onError(\Closure $then): Evacuator |
|
114 | |||
115 | /** |
||
116 | * @param \Closure $closure |
||
117 | * @param string $instanceOf |
||
118 | * @return string |
||
119 | * @throws \InvalidArgumentException |
||
120 | */ |
||
121 | private function resolveTypeHint(\Closure $closure, string $instanceOf): string |
||
156 | |||
157 | /** |
||
158 | * @param array ...$args |
||
159 | * @return mixed |
||
160 | * @throws \Throwable |
||
161 | */ |
||
162 | public function invoke(...$args) |
||
183 | |||
184 | /** |
||
185 | * @param array ...$args |
||
186 | * @return mixed |
||
187 | * @throws \Throwable |
||
188 | */ |
||
189 | private function callClosure(...$args) |
||
208 | |||
209 | /** |
||
210 | * @return bool |
||
211 | * @throws \Throwable |
||
212 | */ |
||
213 | private function willBeThrows(): bool |
||
217 | |||
218 | /** |
||
219 | * @param \Throwable $e |
||
220 | * @param array $callbacks |
||
221 | * @param bool $throwAfter |
||
222 | * @return mixed |
||
223 | * @throws \Throwable |
||
224 | */ |
||
225 | private function throw(\Throwable $e, array $callbacks, bool $throwAfter = false) |
||
239 | |||
240 | /** |
||
241 | * @param \Closure $then |
||
242 | * @return Evacuator|$this |
||
243 | */ |
||
244 | public function finally(\Closure $then): Evacuator |
||
250 | } |
||
251 |
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.