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