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 |
||
7 | class Call |
||
8 | { |
||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | private $method; |
||
13 | |||
14 | /** |
||
15 | * @var bool |
||
16 | */ |
||
17 | private $hasWith = false; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $with = []; |
||
23 | |||
24 | /** |
||
25 | * @var \Throwable|null |
||
26 | */ |
||
27 | private $exception; |
||
28 | |||
29 | /** |
||
30 | * @var bool |
||
31 | */ |
||
32 | private $hasReturnSelf = false; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | private $hasReturn = false; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | private $hasReturnCallback = false; |
||
43 | |||
44 | /** |
||
45 | * @var mixed |
||
46 | */ |
||
47 | private $return; |
||
48 | |||
49 | /** |
||
50 | * @var callable|null |
||
51 | */ |
||
52 | private $returnCallback; |
||
53 | |||
54 | 19 | private function __construct() |
|
57 | |||
58 | /** |
||
59 | * @param string $method |
||
60 | * |
||
61 | * @return self |
||
62 | */ |
||
63 | 19 | public static function create(string $method): self |
|
70 | |||
71 | /** |
||
72 | * @param mixed ...$with |
||
73 | * |
||
74 | * @return self |
||
75 | */ |
||
76 | 2 | public function with(...$with): self |
|
83 | |||
84 | /** |
||
85 | * @param \Throwable $exception |
||
86 | * |
||
87 | * @return self |
||
88 | */ |
||
89 | 8 | View Code Duplication | public function willThrowException(\Throwable $exception): self |
107 | |||
108 | /** |
||
109 | * @return self |
||
110 | */ |
||
111 | 7 | View Code Duplication | public function willReturnSelf(): self |
129 | |||
130 | /** |
||
131 | * @param mixed $return |
||
132 | * |
||
133 | * @return self |
||
134 | */ |
||
135 | 7 | View Code Duplication | public function willReturn($return): self |
154 | |||
155 | /** |
||
156 | * @return self |
||
157 | */ |
||
158 | 7 | View Code Duplication | public function willReturnCallback(callable $returnCallback): self |
177 | |||
178 | /** |
||
179 | * @return string |
||
180 | */ |
||
181 | 7 | public function getMethod(): string |
|
185 | |||
186 | /** |
||
187 | * @return bool |
||
188 | */ |
||
189 | 7 | public function hasWith(): bool |
|
193 | |||
194 | /** |
||
195 | * @return bool |
||
196 | */ |
||
197 | 7 | public function hasReturnSelf(): bool |
|
201 | |||
202 | /** |
||
203 | * @return bool |
||
204 | */ |
||
205 | 7 | public function hasReturn(): bool |
|
209 | |||
210 | /** |
||
211 | * @return bool |
||
212 | */ |
||
213 | 7 | public function hasReturnCallback(): bool |
|
217 | |||
218 | /** |
||
219 | * @return array |
||
220 | */ |
||
221 | 7 | public function getWith(): array |
|
225 | |||
226 | /** |
||
227 | * @return \Throwable|null |
||
228 | */ |
||
229 | 7 | public function getException() |
|
233 | |||
234 | /** |
||
235 | * @return mixed |
||
236 | */ |
||
237 | 7 | public function getReturn() |
|
241 | |||
242 | /** |
||
243 | * @return mixed |
||
244 | */ |
||
245 | 7 | public function getReturnCallback() |
|
249 | } |
||
250 |
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.