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 |
||
26 | class Criteria |
||
27 | { |
||
28 | /** |
||
29 | * @var Selector |
||
30 | */ |
||
31 | protected static $false = null; |
||
32 | |||
33 | /** |
||
34 | * @var Selector |
||
35 | */ |
||
36 | protected static $true = null; |
||
37 | |||
38 | /** |
||
39 | * @var Selector |
||
40 | */ |
||
41 | protected static $null = null; |
||
42 | |||
43 | /** |
||
44 | * @var Selector |
||
45 | */ |
||
46 | protected static $self = null; |
||
47 | |||
48 | /** |
||
49 | * @return \Cubiche\Core\Specification\Selector |
||
50 | */ |
||
51 | View Code Duplication | public static function false() |
|
59 | |||
60 | /** |
||
61 | * @return \Cubiche\Core\Specification\Selector |
||
62 | */ |
||
63 | View Code Duplication | public static function true() |
|
71 | |||
72 | /** |
||
73 | * @return \Cubiche\Core\Specification\Selector |
||
74 | */ |
||
75 | View Code Duplication | public static function null() |
|
83 | |||
84 | /** |
||
85 | * @param string $key |
||
86 | * |
||
87 | * @return \Cubiche\Core\Specification\Selector |
||
88 | */ |
||
89 | public static function key($key) |
||
93 | |||
94 | /** |
||
95 | * @param string $property |
||
96 | * |
||
97 | * @return \Cubiche\Core\Specification\Selector |
||
98 | */ |
||
99 | public static function property($property) |
||
103 | |||
104 | /** |
||
105 | * @param string $method |
||
106 | * |
||
107 | * @return \Cubiche\Core\Specification\Selector |
||
108 | */ |
||
109 | public static function method($method) |
||
113 | |||
114 | /** |
||
115 | * @return \Cubiche\Core\Specification\Selector |
||
116 | */ |
||
117 | public static function this() |
||
125 | |||
126 | /** |
||
127 | * @param callable $callable |
||
128 | * |
||
129 | * @return \Cubiche\Core\Specification\Selector |
||
130 | */ |
||
131 | public static function callback(callable $callable) |
||
135 | |||
136 | /** |
||
137 | * @return \Cubiche\Core\Specification\Selector |
||
138 | */ |
||
139 | public static function count() |
||
143 | |||
144 | /** |
||
145 | * @param SelectorInterface|mixed $value |
||
146 | * |
||
147 | * @return \Cubiche\Core\Specification\Constraint\GreaterThan |
||
148 | */ |
||
149 | public static function gt($value) |
||
153 | |||
154 | /** |
||
155 | * @param SelectorInterface|mixed $value |
||
156 | * |
||
157 | * @return \Cubiche\Core\Specification\Constraint\GreaterThanEqual |
||
158 | */ |
||
159 | public static function gte($value) |
||
163 | |||
164 | /** |
||
165 | * @param SelectorInterface|mixed $value |
||
166 | * |
||
167 | * @return \Cubiche\Core\Specification\Constraint\LessThan |
||
168 | */ |
||
169 | public static function lt($value) |
||
173 | |||
174 | /** |
||
175 | * @param SelectorInterface|mixed $value |
||
176 | * |
||
177 | * @return \Cubiche\Core\Specification\Constraint\LessThanEqual |
||
178 | */ |
||
179 | public static function lte($value) |
||
183 | |||
184 | /** |
||
185 | * @param SelectorInterface|mixed $value |
||
186 | * |
||
187 | * @return \Cubiche\Core\Specification\Constraint\Equal |
||
188 | */ |
||
189 | public static function eq($value) |
||
193 | |||
194 | /** |
||
195 | * @param SelectorInterface|mixed $value |
||
196 | * |
||
197 | * @return \Cubiche\Core\Specification\Constraint\NotEqual |
||
198 | */ |
||
199 | public static function neq($value) |
||
203 | |||
204 | /** |
||
205 | * @param SelectorInterface|mixed $value |
||
206 | * |
||
207 | * @return \Cubiche\Core\Specification\Constraint\Same |
||
208 | */ |
||
209 | public static function same($value) |
||
213 | |||
214 | /** |
||
215 | * @param SelectorInterface|mixed $value |
||
216 | * |
||
217 | * @return \Cubiche\Core\Specification\Constraint\NotSame |
||
218 | */ |
||
219 | public static function notSame($value) |
||
223 | |||
224 | /** |
||
225 | * @return \Cubiche\Core\Specification\Constraint\Same |
||
226 | */ |
||
227 | public static function isNull() |
||
231 | |||
232 | /** |
||
233 | * @return \Cubiche\Core\Specification\Constraint\NotSame |
||
234 | */ |
||
235 | public static function notNull() |
||
239 | |||
240 | /** |
||
241 | * @return \Cubiche\Core\Specification\Constraint\Same |
||
242 | */ |
||
243 | public static function isTrue() |
||
247 | |||
248 | /** |
||
249 | * @return \Cubiche\Core\Specification\Constraint\Same |
||
250 | */ |
||
251 | public static function isFalse() |
||
255 | |||
256 | /** |
||
257 | * @param SpecificationInterface $specification |
||
258 | * |
||
259 | * @return \Cubiche\Core\Specification\Quantifier\All |
||
260 | */ |
||
261 | public static function all(SpecificationInterface $specification) |
||
265 | |||
266 | /** |
||
267 | * @param int $count |
||
268 | * @param SpecificationInterface $specification |
||
269 | * |
||
270 | * @return \Cubiche\Core\Specification\Quantifier\AtLeast |
||
271 | */ |
||
272 | public static function atLeast($count, SpecificationInterface $specification) |
||
276 | |||
277 | /** |
||
278 | * @param SpecificationInterface $specification |
||
279 | * |
||
280 | * @return \Cubiche\Core\Specification\Quantifier\AtLeast |
||
281 | */ |
||
282 | public static function any(SpecificationInterface $specification) |
||
286 | |||
287 | /** |
||
288 | * @param SpecificationInterface $specification |
||
289 | * |
||
290 | * @return \Cubiche\Core\Specification\SpecificationInterface |
||
291 | */ |
||
292 | public static function not(SpecificationInterface $specification) |
||
296 | } |
||
297 |
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.