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 |
||
24 | View Code Duplication | abstract class ConditionCollection implements Condition |
|
|
|||
25 | { |
||
26 | /** |
||
27 | * All child conditions of the collection. |
||
28 | * |
||
29 | * @var Condition[] |
||
30 | */ |
||
31 | protected $conditions = array(); |
||
32 | |||
33 | /** |
||
34 | * ConditionCollection constructor. |
||
35 | * |
||
36 | * @param Condition[]|iterable $conditions List of child conditions. |
||
37 | */ |
||
38 | public function __construct(iterable $conditions = []) |
||
42 | |||
43 | /** |
||
44 | * Add condition. |
||
45 | * |
||
46 | * @param Condition $condition Condition being added. |
||
47 | * |
||
48 | * @return $this |
||
49 | */ |
||
50 | public function addCondition(Condition $condition): self |
||
56 | |||
57 | /** |
||
58 | * Remove condition from collection. |
||
59 | * |
||
60 | * @param Condition $condition Condition being removed. |
||
61 | * |
||
62 | * @return $this |
||
63 | */ |
||
64 | public function removeCondition(Condition $condition): self |
||
74 | |||
75 | /** |
||
76 | * Get child conditions. |
||
77 | * |
||
78 | * @return Condition[]|iterable |
||
79 | */ |
||
80 | public function getConditions(): iterable |
||
84 | |||
85 | /** |
||
86 | * Add multiple conditions. |
||
87 | * |
||
88 | * @param iterable $conditions Array of conditions being added. |
||
89 | * |
||
90 | * @return $this |
||
91 | * |
||
92 | * @throws \Assert\InvalidArgumentException If array contains an invalid condition. |
||
93 | */ |
||
94 | public function addConditions(iterable $conditions): self |
||
104 | } |
||
105 |
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.