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); |
||
50 | trait Comparisons |
||
51 | { |
||
52 | /** |
||
53 | * @param mixed $value |
||
54 | * @param RuleInterface|null $next |
||
55 | * |
||
56 | * @return RuleInterface |
||
57 | * |
||
58 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
59 | */ |
||
60 | 5 | View Code Duplication | protected static function equals($value, RuleInterface $next = null): RuleInterface |
72 | |||
73 | /** |
||
74 | * @param mixed $value |
||
75 | * @param RuleInterface|null $next |
||
76 | * |
||
77 | * @return RuleInterface |
||
78 | * |
||
79 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
80 | */ |
||
81 | 2 | View Code Duplication | protected static function notEquals($value, RuleInterface $next = null): RuleInterface |
93 | |||
94 | /** |
||
95 | * @param array $scalars |
||
96 | * @param RuleInterface|null $next |
||
97 | * |
||
98 | * @return RuleInterface |
||
99 | */ |
||
100 | 1 | protected static function inValues(array $scalars, RuleInterface $next = null): RuleInterface |
|
104 | |||
105 | /** |
||
106 | * @param mixed $value |
||
107 | * @param RuleInterface|null $next |
||
108 | * |
||
109 | * @return RuleInterface |
||
110 | */ |
||
111 | 2 | protected static function lessThan($value, RuleInterface $next = null): RuleInterface |
|
117 | |||
118 | /** |
||
119 | * @param mixed $value |
||
120 | * @param RuleInterface|null $next |
||
121 | * |
||
122 | * @return RuleInterface |
||
123 | */ |
||
124 | 2 | protected static function lessOrEquals($value, RuleInterface $next = null): RuleInterface |
|
130 | |||
131 | /** |
||
132 | * @param mixed $value |
||
133 | * @param RuleInterface|null $next |
||
134 | * |
||
135 | * @return RuleInterface |
||
136 | */ |
||
137 | 2 | protected static function moreThan($value, RuleInterface $next = null): RuleInterface |
|
143 | |||
144 | /** |
||
145 | * @param mixed $value |
||
146 | * @param RuleInterface|null $next |
||
147 | * |
||
148 | * @return RuleInterface |
||
149 | */ |
||
150 | 2 | protected static function moreOrEquals($value, RuleInterface $next = null): RuleInterface |
|
157 | |||
158 | /** |
||
159 | * @param mixed $lowerLimit |
||
160 | * @param mixed $upperLimit |
||
161 | * @param RuleInterface|null $next |
||
162 | * |
||
163 | * @return RuleInterface |
||
164 | */ |
||
165 | 3 | protected static function between($lowerLimit, $upperLimit, RuleInterface $next = null): RuleInterface |
|
174 | |||
175 | /** |
||
176 | * @param int $min |
||
177 | * @param int $max |
||
178 | * @param RuleInterface|null $next |
||
179 | * |
||
180 | * @return RuleInterface |
||
181 | */ |
||
182 | 2 | protected static function stringLengthBetween(int $min, int $max, RuleInterface $next = null): RuleInterface |
|
188 | |||
189 | /** |
||
190 | * @param int $min |
||
191 | * @param RuleInterface|null $next |
||
192 | * |
||
193 | * @return RuleInterface |
||
194 | */ |
||
195 | 1 | protected static function stringLengthMin(int $min, RuleInterface $next = null): RuleInterface |
|
201 | |||
202 | /** |
||
203 | * @param int $max |
||
204 | * @param RuleInterface|null $next |
||
205 | * |
||
206 | * @return RuleInterface |
||
207 | */ |
||
208 | 1 | protected static function stringLengthMax(int $max, RuleInterface $next = null): RuleInterface |
|
214 | |||
215 | /** |
||
216 | * @param string $pattern |
||
217 | * @param RuleInterface|null $next |
||
218 | * |
||
219 | * @return RuleInterface |
||
220 | */ |
||
221 | 1 | protected static function regexp(string $pattern, RuleInterface $next = null): RuleInterface |
|
227 | |||
228 | /** |
||
229 | * @param RuleInterface $rule |
||
230 | * |
||
231 | * @return RuleInterface |
||
232 | */ |
||
233 | 3 | protected static function nullable(RuleInterface $rule): RuleInterface |
|
237 | } |
||
238 |
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.