Complex classes like CacheableRepository 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 CacheableRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 12 | trait CacheableRepository | ||
| 13 | { | ||
| 14 | /** | ||
| 15 | * @var CacheRepository | ||
| 16 | */ | ||
| 17 | protected $cacheRepository = null; | ||
| 18 | |||
| 19 | /** | ||
| 20 | * @var boolean | ||
| 21 | */ | ||
| 22 | protected $cacheSkip = false; | ||
| 23 | |||
| 24 | /** | ||
| 25 | * Set Cache Repository | ||
| 26 | * | ||
| 27 | * @param CacheRepository $repository | ||
| 28 | * | ||
| 29 | * @return $this | ||
| 30 | */ | ||
| 31 | 1 | public function setCacheRepository(CacheRepository $repository) | |
| 37 | |||
| 38 | /** | ||
| 39 | * Return instance of Cache Repository | ||
| 40 | * | ||
| 41 | * @return CacheRepository | ||
| 42 | */ | ||
| 43 | 1 | public function getCacheRepository() | |
| 51 | |||
| 52 | /** | ||
| 53 | * CacheTags | ||
| 54 | * | ||
| 55 | * @param mixed $tags | ||
| 56 | * | ||
| 57 | * @return \Illuminate\Cache\TaggedCache | ||
| 58 | */ | ||
| 59 | 1 | public function cacheTags($tags = null) | |
| 65 | |||
| 66 | /** | ||
| 67 | * Skip Cache | ||
| 68 | * | ||
| 69 | * @param bool $status | ||
| 70 | * | ||
| 71 | * @return $this | ||
| 72 | */ | ||
| 73 | 1 | public function skipCache($status = true) | |
| 79 | |||
| 80 | /** | ||
| 81 | * Is Skipped Cache | ||
| 82 | * | ||
| 83 | * @return bool | ||
| 84 | */ | ||
| 85 | 2 | public function isSkippedCache() | |
| 97 | |||
| 98 | /** | ||
| 99 | * Allowed Cache | ||
| 100 | * | ||
| 101 | * @param string $method | ||
| 102 | * | ||
| 103 | * @return bool | ||
| 104 | */ | ||
| 105 | 1 | protected function allowedCache($method) | |
| 130 | |||
| 131 | /** | ||
| 132 | * Get Cache key for the method | ||
| 133 | * | ||
| 134 | * @param string $method | ||
| 135 | * @param array $args | ||
| 136 | * | ||
| 137 | * @return string | ||
| 138 | */ | ||
| 139 | 1 | public function getCacheKey($method, $args = null) | |
| 154 | |||
| 155 | /** | ||
| 156 | 1 | * Serialize the criteria making sure the Closures are taken care of. | |
| 157 | * | ||
| 158 | * @return string | ||
| 159 | 1 | */ | |
| 160 | protected function serializeCriteria() | ||
| 170 | |||
| 171 | /** | ||
| 172 | * Serialize single criterion with customized serialization of Closures. | ||
| 173 | * | ||
| 174 | * @param \Prettus\Repository\Contracts\CriteriaInterface $criterion | ||
| 175 | * | ||
| 176 | * @return \Prettus\Repository\Contracts\CriteriaInterface|array | ||
| 177 | * | ||
| 178 | * @throws \Exception | ||
| 179 | */ | ||
| 180 | protected function serializeCriterion($criterion) | ||
| 201 | |||
| 202 | /** | ||
| 203 | 1 | * Get cache minutes | |
| 204 | * | ||
| 205 | 1 | * @return int | |
| 206 | */ | ||
| 207 | 1 | public function getCacheMinutes() | |
| 213 | |||
| 214 | /** | ||
| 215 | * call Cache | ||
| 216 | * | ||
| 217 | * @param string $method | ||
| 218 | 1 | * @param array $args | |
| 219 | * | ||
| 220 | 1 | * @return mixed | |
| 221 | */ | ||
| 222 | public function callCache($method, array $args) | ||
| 236 | |||
| 237 | /** | ||
| 238 | * Retrieve all data of repository | ||
| 239 | * | ||
| 240 | 1 | * @param array $columns | |
| 241 | * | ||
| 242 | 1 | * @return mixed | |
| 243 | */ | ||
| 244 | public function all($columns = ['*']) | ||
| 248 | |||
| 249 | /** | ||
| 250 | * Retrieve all data of repository, paginated | ||
| 251 | * | ||
| 252 | * @param null $limit | ||
| 253 | * @param array $columns | ||
| 254 | 1 | * @param string $method | |
| 255 | * | ||
| 256 | 1 | * @return mixed | |
| 257 | */ | ||
| 258 | public function paginate($limit = null, $columns = ['*'], $method = "paginate") | ||
| 262 | |||
| 263 | /** | ||
| 264 | * Find data by id | ||
| 265 | * | ||
| 266 | * @param int $id | ||
| 267 | 1 | * @param array $columns | |
| 268 | * | ||
| 269 | 1 | * @return mixed | |
| 270 | */ | ||
| 271 | public function find($id, $columns = ['*']) | ||
| 275 | |||
| 276 | /** | ||
| 277 | * Find data by field and value | ||
| 278 | * | ||
| 279 | * @param string $field | ||
| 280 | * @param mixed $value | ||
| 281 | 1 | * @param array $columns | |
| 282 | * | ||
| 283 | 1 | * @return mixed | |
| 284 | */ | ||
| 285 | public function findByField($field, $value = null, $columns = ['*']) | ||
| 289 | |||
| 290 | /** | ||
| 291 | * Find data by multiple fields | ||
| 292 | * | ||
| 293 | * @param array $where | ||
| 294 | 1 | * @param array $columns | |
| 295 | * | ||
| 296 | 1 | * @return mixed | |
| 297 | */ | ||
| 298 | public function findWhere(array $where, $columns = ['*']) | ||
| 302 | |||
| 303 | /** | ||
| 304 | * Find data by Criteria | ||
| 305 | * | ||
| 306 | 1 | * @param CriteriaInterface $criteria | |
| 307 | * | ||
| 308 | 1 | * @return mixed | |
| 309 | */ | ||
| 310 | public function getByCriteria(CriteriaInterface $criteria) | ||
| 314 | |||
| 315 | /** | ||
| 316 | * Pluck | ||
| 317 | * | ||
| 318 | * @param string $column | ||
| 319 | 1 | * @param array $key | |
| 320 | * | ||
| 321 | 1 | * @return array | |
| 322 | */ | ||
| 323 | public function pluck($column, $key = null) | ||
| 327 | |||
| 328 | /** | ||
| 329 | * Count results of repository | ||
| 330 | * | ||
| 331 | * @param array $where | ||
| 332 | 1 | * @param string $columns | |
| 333 | * | ||
| 334 | 1 | * @return int | |
| 335 | */ | ||
| 336 | public function count(array $where = [], $columns = '*') | ||
| 340 | |||
| 341 | /** | ||
| 342 | * Max | ||
| 343 | * | ||
| 344 | * @param mixed $field Mixed Field | ||
| 345 | 1 | * @param array $input Array Input | |
| 346 | * | ||
| 347 | 1 | * @return mixed | |
| 348 | */ | ||
| 349 | public function max($field, array $input = array()) | ||
| 353 | |||
| 354 | /** | ||
| 355 | * Min | ||
| 356 | * | ||
| 357 | * @param mixed $field Mixed Field | ||
| 358 | 1 | * @param array $input Array Input | |
| 359 | * | ||
| 360 | 1 | * @return mixed | |
| 361 | */ | ||
| 362 | public function min($field, array $input = array()) | ||
| 366 | |||
| 367 | /** | ||
| 368 | * Sum | ||
| 369 | * | ||
| 370 | * @param mixed $field Mixed Field | ||
| 371 | 1 | * @param array $input Array Input | |
| 372 | * | ||
| 373 | 1 | * @return float | |
| 374 | */ | ||
| 375 | public function sum($field, array $input = array()) | ||
| 379 | |||
| 380 | /** | ||
| 381 | * Average | ||
| 382 | * | ||
| 383 | * @param mixed $field Mixed Field | ||
| 384 | 1 | * @param array $input Array Input | |
| 385 | * | ||
| 386 | 1 | * @return float | |
| 387 | */ | ||
| 388 | public function avg($field, array $input = array()) | ||
| 392 | } | ||
| 393 | 
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: