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:
Complex classes like InMemoryCriteria often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use InMemoryCriteria, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class InMemoryCriteria implements CriteriaInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | protected $entityName; |
||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $callbacks; |
||
18 | |||
19 | /** |
||
20 | * @param string $entityName |
||
21 | */ |
||
22 | public function __construct($entityName) |
||
27 | |||
28 | /** |
||
29 | * @return mixed |
||
30 | */ |
||
31 | public function &getQuery() |
||
35 | |||
36 | /** |
||
37 | * @return string |
||
38 | */ |
||
39 | public function getEntityName() |
||
43 | |||
44 | /** |
||
45 | * @param string $entityName |
||
46 | * @return CriteriaInterface |
||
47 | */ |
||
48 | public function relation($entityName) |
||
53 | |||
54 | /** |
||
55 | * @param string $attribute |
||
56 | * @param bool|int|float|string $value |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function equalTo($attribute, $value) |
||
75 | |||
76 | /** |
||
77 | * @param string $attribute |
||
78 | * @param bool|int|float|string $value |
||
79 | * @return $this |
||
80 | */ |
||
81 | public function notEqualTo($attribute, $value) |
||
97 | |||
98 | /** |
||
99 | * @param string $attribute |
||
100 | * @param int|float $value |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function lessThan($attribute, $value) |
||
119 | |||
120 | /** |
||
121 | * @param string $attribute |
||
122 | * @param int|float $value |
||
123 | * @return $this |
||
124 | */ |
||
125 | public function greaterThan($attribute, $value) |
||
141 | |||
142 | /** |
||
143 | * @param string $attribute |
||
144 | * @param int|float $value |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function greaterThanOrEqualTo($attribute, $value) |
||
163 | |||
164 | /** |
||
165 | * @param string $attribute |
||
166 | * @param int|float $value |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function lessThanOrEqualTo($attribute, $value) |
||
185 | |||
186 | /** |
||
187 | * @param string $attribute |
||
188 | * @param int|float $value |
||
189 | * @return $this |
||
190 | */ |
||
191 | View Code Duplication | public function like($attribute, $value) |
|
207 | |||
208 | /** |
||
209 | * @param string $attribute |
||
210 | * @return $this |
||
211 | */ |
||
212 | public function isNull($attribute) |
||
228 | |||
229 | /** |
||
230 | * @param string $attribute |
||
231 | * @return $this |
||
232 | */ |
||
233 | public function isNotNull($attribute) |
||
249 | |||
250 | /** |
||
251 | * @param string $attribute |
||
252 | * @param array $values |
||
253 | * @return $this |
||
254 | */ |
||
255 | View Code Duplication | public function in($attribute, array $values) |
|
271 | |||
272 | /** |
||
273 | * @param string $attribute |
||
274 | * @param array $values |
||
275 | * @return $this |
||
276 | */ |
||
277 | public function notIn($attribute, array $values) |
||
293 | |||
294 | /** |
||
295 | * @param string $attribute |
||
296 | * @param int|float|string $minValue |
||
297 | * @param int|float|string $maxValue |
||
298 | * @return $this |
||
299 | */ |
||
300 | public function between($attribute, $minValue, $maxValue) |
||
316 | |||
317 | /** |
||
318 | * @param string $attribute |
||
319 | * @return $this |
||
320 | */ |
||
321 | public function order($attribute) |
||
326 | |||
327 | /** |
||
328 | * @param int $limit |
||
329 | * @return $this |
||
330 | */ |
||
331 | public function limit($limit) |
||
336 | |||
337 | /** |
||
338 | * @param int $offset |
||
339 | * @return $this |
||
340 | */ |
||
341 | public function offset($offset) |
||
346 | |||
347 | /** |
||
348 | * @param $attribute |
||
349 | * @return string |
||
350 | */ |
||
351 | public function getField($attribute) |
||
355 | } |
||
356 |
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.