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 |
||
10 | class Set implements IteratorAggregate |
||
11 | { |
||
12 | /** |
||
13 | * @var string[]|int[]|float[]|bool[] |
||
14 | */ |
||
15 | private $elements = []; |
||
16 | |||
17 | /** |
||
18 | * @param string[]|int[]|float[]|bool[] $elements |
||
19 | */ |
||
20 | public function __construct(array $elements = []) |
||
24 | |||
25 | /** |
||
26 | * Creates the union of A and B. |
||
27 | */ |
||
28 | public static function union(self $a, self $b): self |
||
32 | |||
33 | /** |
||
34 | * Creates the intersection of A and B. |
||
35 | */ |
||
36 | public static function intersection(self $a, self $b): self |
||
40 | |||
41 | /** |
||
42 | * Creates the difference of A and B. |
||
43 | */ |
||
44 | public static function difference(self $a, self $b): self |
||
48 | |||
49 | /** |
||
50 | * Creates the Cartesian product of A and B. |
||
51 | * |
||
52 | * @return Set[] |
||
53 | */ |
||
54 | View Code Duplication | public static function cartesian(self $a, self $b): array |
|
66 | |||
67 | /** |
||
68 | * Creates the power set of A. |
||
69 | * |
||
70 | * @return Set[] |
||
71 | */ |
||
72 | View Code Duplication | public static function power(self $a): array |
|
84 | |||
85 | /** |
||
86 | * @param string|int|float|bool $element |
||
87 | */ |
||
88 | public function add($element): self |
||
92 | |||
93 | /** |
||
94 | * @param string[]|int[]|float[]|bool[] $elements |
||
95 | */ |
||
96 | public function addAll(array $elements): self |
||
102 | |||
103 | /** |
||
104 | * @param string|int|float $element |
||
105 | */ |
||
106 | public function remove($element): self |
||
110 | |||
111 | /** |
||
112 | * @param string[]|int[]|float[] $elements |
||
113 | */ |
||
114 | public function removeAll(array $elements): self |
||
120 | |||
121 | /** |
||
122 | * @param string|int|float $element |
||
123 | */ |
||
124 | public function contains($element): bool |
||
128 | |||
129 | /** |
||
130 | * @param string[]|int[]|float[] $elements |
||
131 | */ |
||
132 | public function containsAll(array $elements): bool |
||
136 | |||
137 | /** |
||
138 | * @return string[]|int[]|float[]|bool[] |
||
139 | */ |
||
140 | public function toArray(): array |
||
144 | |||
145 | public function getIterator(): ArrayIterator |
||
149 | |||
150 | public function isEmpty(): bool |
||
154 | |||
155 | public function cardinality(): int |
||
159 | |||
160 | /** |
||
161 | * Removes duplicates and rewrites index. |
||
162 | * |
||
163 | * @param string[]|int[]|float[]|bool[] $elements |
||
164 | * |
||
165 | * @return string[]|int[]|float[]|bool[] |
||
166 | */ |
||
167 | private static function sanitize(array $elements): array |
||
173 | } |
||
174 |
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.