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 |
||
12 | View Code Duplication | final class OrX extends CompositeSpecification |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * @var Specification |
||
16 | */ |
||
17 | private $firstPart; |
||
18 | |||
19 | /** |
||
20 | * @var Specification |
||
21 | */ |
||
22 | private $secondPart; |
||
23 | |||
24 | /** |
||
25 | * @param Specification $firstPart |
||
26 | * @param Specification $secondPart |
||
27 | */ |
||
28 | 7 | public function __construct(Specification $firstPart, Specification $secondPart) |
|
33 | |||
34 | /** |
||
35 | * {inheritdoc} |
||
36 | */ |
||
37 | 5 | public function isSatisfiedBy($candidate): bool |
|
43 | |||
44 | /** |
||
45 | * Get the first part of the condition |
||
46 | * |
||
47 | * @return Specification |
||
48 | */ |
||
49 | 1 | public function getFirstPart(): Specification |
|
53 | |||
54 | /** |
||
55 | * Get the second part of the condition |
||
56 | * |
||
57 | * @return Specification |
||
58 | */ |
||
59 | 1 | public function getSecondPart(): Specification |
|
63 | } |
||
64 |
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.