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 Filtered 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 Filtered, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Filtered extends Paged |
||
23 | { |
||
24 | /** @var array Collection for current filtered material identifiers */ |
||
25 | protected $materialIDs = array(); |
||
26 | |||
27 | /** @var array Collection of navigation filters */ |
||
28 | protected $navigation = array(); |
||
29 | |||
30 | /** @var array Collection of field filters */ |
||
31 | protected $field = array(); |
||
32 | |||
33 | /** @var array Search string collection */ |
||
34 | protected $search = array(); |
||
35 | |||
36 | /** @var array Collection of query handlers */ |
||
37 | protected $idHandlers = array(); |
||
38 | |||
39 | /** @var array External material handler and params array */ |
||
40 | protected $entityHandlers = array(); |
||
41 | |||
42 | /** @var array Base material entity handler callbacks array */ |
||
43 | protected $baseEntityHandlers = array(); |
||
44 | |||
45 | /** @var string Collection entities class name */ |
||
46 | protected $entityName = 'samson\cms\CMSMaterial'; |
||
47 | |||
48 | /** @var array Sorter parameters collection */ |
||
49 | protected $sorter = array(); |
||
50 | |||
51 | /** |
||
52 | * Generic collection constructor |
||
53 | * @param RenderInterface $renderer View render object |
||
54 | * @param QueryInterface $query Query object |
||
55 | */ |
||
56 | public function __construct(RenderInterface $renderer, QueryInterface $query, PagerInterface $pager) |
||
61 | |||
62 | /** |
||
63 | * Render products collection block |
||
64 | * @param string $prefix Prefix for view variables |
||
65 | * @param array $restricted Collection of ignored keys |
||
66 | * @return array Collection key => value |
||
67 | */ |
||
68 | public function toView($prefix = null, array $restricted = array()) |
||
76 | |||
77 | /** |
||
78 | * Add external identifier filter handler |
||
79 | * @param callback $handler |
||
80 | * @param array $params |
||
81 | * @return self Chaining |
||
82 | */ |
||
83 | public function handler($handler, array $params = array()) |
||
90 | |||
91 | /** |
||
92 | * Set external entity handler |
||
93 | * @param callback $handler |
||
94 | * @param array $params |
||
95 | * @return self Chaining |
||
96 | */ |
||
97 | public function baseEntityHandler($handler, array $params = array()) |
||
104 | |||
105 | /** |
||
106 | * Set external entity handler |
||
107 | * @param callback $handler |
||
108 | * @param array $params |
||
109 | * @return self Chaining |
||
110 | */ |
||
111 | public function entityHandler($handler, array $params = array()) |
||
118 | |||
119 | /** |
||
120 | * Set collection sorter parameters |
||
121 | * @param string|integer $field Field identifier or name |
||
122 | * @param string $destination ASC|DESC |
||
123 | * @return void |
||
124 | */ |
||
125 | public function sorter($field, $destination = 'ASC') |
||
144 | |||
145 | /** |
||
146 | * Filter collection using navigation entity or collection of them. |
||
147 | * If collection of navigation Url or Ids is passed then this group will be |
||
148 | * applied as single navigation filter to retrieve materials. |
||
149 | * |
||
150 | * @param string|integer|array $navigation Navigation URL or identifier for filtering |
||
151 | * @return self Chaining |
||
152 | */ |
||
153 | public function navigation($navigation) |
||
172 | |||
173 | /** |
||
174 | * Filter collection using additional field entity. |
||
175 | * |
||
176 | * @param string|integer|\samson\cms\Field $field Additional field identifier or name |
||
177 | * @param mixed $value Additional field value for filtering |
||
178 | * @param string $relation Additional field relation for filtering |
||
179 | * @return self Chaining |
||
180 | */ |
||
181 | public function field($field, $value, $relation = dbRelation::EQUAL) |
||
202 | |||
203 | /** |
||
204 | * Filter collection using additional field entity values and LIKE relation. |
||
205 | * If this method is called more then once, it will use materials, previously filtered by this method. |
||
206 | * |
||
207 | * @param string $search Search string |
||
208 | * @return self Chaining |
||
209 | */ |
||
210 | public function search($search) |
||
220 | |||
221 | /** |
||
222 | * Filter collection of numeric field in range from min to max values |
||
223 | * @param string|integer|\samson\cms\Field $field Additional field identifier or name |
||
224 | * @param integer $minValue Min value for range filter |
||
225 | * @param integer $maxValue Max value for range filter |
||
226 | * @return self Chaining |
||
227 | */ |
||
228 | public function ranged($field, $minValue, $maxValue) |
||
252 | |||
253 | /** |
||
254 | * Try to find additional field record |
||
255 | * @param string|integer $field Additional field identifier or name |
||
256 | * @return bool True if field record has been found |
||
257 | */ |
||
258 | protected function isFieldObject(&$field) |
||
273 | |||
274 | /** |
||
275 | * Try to get all material identifiers filtered by navigation |
||
276 | * if no navigation filtering is set - nothing will happen. |
||
277 | * |
||
278 | * @param array $filteredIds Collection of filtered material identifiers |
||
279 | * @return bool True if ALL navigation filtering succeeded or there was no filtering at all otherwise false |
||
280 | */ |
||
281 | View Code Duplication | protected function applyNavigationFilter(& $filteredIds = array()) |
|
307 | |||
308 | /** |
||
309 | * Try to get all material identifiers filtered by additional field |
||
310 | * if no field filtering is set - nothing will happen. |
||
311 | * |
||
312 | * @param array $filteredIds Collection of filtered material identifiers |
||
313 | * @return bool True if ALL field filtering succeeded or there was no filtering at all otherwise false |
||
314 | */ |
||
315 | View Code Duplication | protected function applyFieldFilter(& $filteredIds = array()) |
|
340 | |||
341 | /** |
||
342 | * Try to find all materials which have fields similar to search strings |
||
343 | * |
||
344 | * @param array $filteredIds Collection of filtered material identifiers |
||
345 | * @return bool True if ALL field filtering succeeded or there was no filtering at all otherwise false |
||
346 | */ |
||
347 | protected function applySearchFilter(& $filteredIds = array()) |
||
408 | |||
409 | /** |
||
410 | * Apply all possible material filters |
||
411 | * @param array $filteredIds Collection of material identifiers |
||
412 | * @return bool True if ALL filtering succeeded or there was no filtering at all otherwise false |
||
413 | */ |
||
414 | protected function applyFilter(& $filteredIds = array()) |
||
420 | |||
421 | /** |
||
422 | * Perform material identifiers collection sorting |
||
423 | * @param array $materialIDs Variable to return sorted collection |
||
424 | */ |
||
425 | protected function applyFieldSorter(& $materialIDs = array()) |
||
440 | |||
441 | /** |
||
442 | * Call handlers stack |
||
443 | * @param array $handlers Collection of callbacks with their parameters |
||
444 | * @param array $params External parameters to pass to callback at first |
||
445 | * @return bool True if all handlers succeeded |
||
446 | */ |
||
447 | protected function callHandlers(& $handlers = array(), $params = array()) |
||
463 | |||
464 | /** |
||
465 | * Perform filtering on base material entity |
||
466 | * @param array $materialIDs Variable to return sorted collection |
||
467 | */ |
||
468 | protected function applyBaseEntityFilter(& $materialIDs = array()) |
||
496 | |||
497 | /** |
||
498 | * Perform collection database retrieval using set filters |
||
499 | * |
||
500 | * @return self Chaining |
||
501 | */ |
||
502 | public function fill() |
||
564 | } |
||
565 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.