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