Complex classes like Criteriable 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 Criteriable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | trait Criteriable |
||
15 | { |
||
16 | /** |
||
17 | * List of repository criteria. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $criteria = []; |
||
22 | |||
23 | /** |
||
24 | * List of default repository criteria. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $defaultCriteria = []; |
||
29 | |||
30 | /** |
||
31 | * Skip criteria flag. |
||
32 | * If setted to true criteria will not be apply to the query. |
||
33 | * |
||
34 | * @var bool |
||
35 | */ |
||
36 | protected $skipCriteria = false; |
||
37 | |||
38 | /** |
||
39 | * Skip default criteria flag. |
||
40 | * If setted to true default criteria will not be added to the criteria list. |
||
41 | * |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected $skipDefaultCriteria = false; |
||
45 | |||
46 | /** |
||
47 | * Return name for the criterion. |
||
48 | * If as criterion in parameter passed string we assume that is criterion class name. |
||
49 | * |
||
50 | * @param CriterionContract|Closure|string $criteria |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | public function getCriterionName($criteria): string |
||
62 | |||
63 | /** |
||
64 | * Try to instantiate given criterion class name with this arguments. |
||
65 | * |
||
66 | * @param $class |
||
67 | * @param $arguments |
||
68 | * |
||
69 | * @throws CriterionException |
||
70 | * |
||
71 | * @return mixed |
||
|
|||
72 | */ |
||
73 | protected function instantiateCriterion($class, $arguments) |
||
92 | |||
93 | /** |
||
94 | * Return class and arguments from passed array criterion. |
||
95 | * Extracting class and arguments from array. |
||
96 | * |
||
97 | * @param array $criterion |
||
98 | * |
||
99 | * @throws CriterionException |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | protected function extractCriterionClassAndArgs(array $criterion): array |
||
120 | |||
121 | /** |
||
122 | * Add criterion to the specific list. |
||
123 | * low-level implementation of adding criterion to the list. |
||
124 | * |
||
125 | * @param Closure|CriterionContract|array|string $criterion |
||
126 | * @param string $list |
||
127 | * |
||
128 | * @throws CriterionException |
||
129 | * @throws RepositoryException |
||
130 | * |
||
131 | * @return $this |
||
132 | */ |
||
133 | protected function addCriterion($criterion, $list) |
||
163 | |||
164 | /** |
||
165 | * Add criteria to the specific list |
||
166 | * low-level implementation of adding criteria to the list. |
||
167 | * |
||
168 | * @param array $criteria |
||
169 | * @param $list |
||
170 | */ |
||
171 | protected function addCriteria(array $criteria, $list) |
||
178 | |||
179 | /** |
||
180 | * Push criterion to the criteria list. |
||
181 | * |
||
182 | * @param CriterionContract|Closure|array|string $criterion |
||
183 | * |
||
184 | * @return $this |
||
185 | */ |
||
186 | public function pushCriterion($criterion) |
||
192 | |||
193 | /** |
||
194 | * Remove provided criterion from criteria list. |
||
195 | * |
||
196 | * @param CriterionContract|Closure|string $criterion |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function removeCriterion($criterion) |
||
206 | |||
207 | /** |
||
208 | * Remove provided criteria from criteria list. |
||
209 | * |
||
210 | * @param array $criteria |
||
211 | * |
||
212 | * @return RepositoryContract |
||
213 | */ |
||
214 | public function removeCriteria(array $criteria) |
||
222 | |||
223 | /** |
||
224 | * Push array of criteria to the criteria list. |
||
225 | * |
||
226 | * @param array $criteria |
||
227 | * |
||
228 | * @return $this |
||
229 | */ |
||
230 | public function pushCriteria(array $criteria) |
||
236 | |||
237 | /** |
||
238 | * Flush criteria list. |
||
239 | * We can flush criteria only when they is not skipped. |
||
240 | * |
||
241 | * @return $this |
||
242 | */ |
||
243 | public function flushCriteria() |
||
251 | |||
252 | /** |
||
253 | * Set default criteria list. |
||
254 | * |
||
255 | * @param array $criteria |
||
256 | * |
||
257 | * @return $this |
||
258 | */ |
||
259 | public function setDefaultCriteria(array $criteria) |
||
265 | |||
266 | /** |
||
267 | * Return default criteria list. |
||
268 | * |
||
269 | * @return array |
||
270 | */ |
||
271 | public function getDefaultCriteria(): array |
||
275 | |||
276 | /** |
||
277 | * Return current list of criteria. |
||
278 | * |
||
279 | * @return array |
||
280 | */ |
||
281 | public function getCriteria(): array |
||
289 | |||
290 | /** |
||
291 | * Set skipCriteria flag. |
||
292 | * |
||
293 | * @param bool|true $flag |
||
294 | * |
||
295 | * @return $this |
||
296 | */ |
||
297 | public function skipCriteria($flag = true) |
||
303 | |||
304 | /** |
||
305 | * Set skipDefaultCriteria flag. |
||
306 | * |
||
307 | * @param bool|true $flag |
||
308 | * |
||
309 | * @return $this |
||
310 | */ |
||
311 | public function skipDefaultCriteria($flag = true) |
||
317 | |||
318 | /** |
||
319 | * Check if a given criterion name now in the criteria list. |
||
320 | * |
||
321 | * @param CriterionContract|Closure|string $criterion |
||
322 | * |
||
323 | * @return bool |
||
324 | */ |
||
325 | public function hasCriterion($criterion): bool |
||
329 | |||
330 | /** |
||
331 | * Return criterion object or closure from criteria list by name. |
||
332 | * |
||
333 | * @param $criterion |
||
334 | * |
||
335 | * @return CriterionContract|Closure|null |
||
336 | */ |
||
337 | public function getCriterion($criterion) |
||
343 | |||
344 | /** |
||
345 | * Apply criteria list to the given query. |
||
346 | * |
||
347 | * @param $query |
||
348 | * @param $repository |
||
349 | * |
||
350 | * @return mixed |
||
351 | */ |
||
352 | public function applyCriteria($query, $repository) |
||
364 | } |
||
365 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.