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) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Serialize the criteria making sure the Closures are taken care of. |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | 1 | protected function serializeCriteria() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Serialize single criterion with customized serialization of Closures. |
||
| 167 | * |
||
| 168 | * @param \Prettus\Repository\Contracts\CriteriaInterface $criterion |
||
| 169 | * |
||
| 170 | * @return \Prettus\Repository\Contracts\CriteriaInterface|array |
||
| 171 | * |
||
| 172 | * @throws \Exception |
||
| 173 | */ |
||
| 174 | protected function serializeCriterion($criterion) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get cache minutes |
||
| 198 | * |
||
| 199 | * @return int |
||
| 200 | */ |
||
| 201 | 1 | public function getCacheMinutes() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * call Cache |
||
| 210 | * |
||
| 211 | * @param string $method |
||
| 212 | * @param array $args |
||
| 213 | * |
||
| 214 | * @return mixed |
||
| 215 | */ |
||
| 216 | 1 | public function callCache($method, array $args) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Retrieve all data of repository |
||
| 233 | * |
||
| 234 | * @param array $columns |
||
| 235 | * |
||
| 236 | * @return mixed |
||
| 237 | */ |
||
| 238 | 1 | public function all($columns = ['*']) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Retrieve all data of repository, paginated |
||
| 245 | * |
||
| 246 | * @param null $limit |
||
| 247 | * @param array $columns |
||
| 248 | * |
||
| 249 | * @return mixed |
||
| 250 | */ |
||
| 251 | 1 | public function paginate($limit = null, $columns = ['*']) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Find data by id |
||
| 258 | * |
||
| 259 | * @param int $id |
||
| 260 | * @param array $columns |
||
| 261 | * |
||
| 262 | * @return mixed |
||
| 263 | */ |
||
| 264 | 1 | public function find($id, $columns = ['*']) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Find data by field and value |
||
| 271 | * |
||
| 272 | * @param string $field |
||
| 273 | * @param mixed $value |
||
| 274 | * @param array $columns |
||
| 275 | * |
||
| 276 | * @return mixed |
||
| 277 | */ |
||
| 278 | 1 | public function findByField($field, $value = null, $columns = ['*']) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Find data by multiple fields |
||
| 285 | * |
||
| 286 | * @param array $where |
||
| 287 | * @param array $columns |
||
| 288 | * |
||
| 289 | * @return mixed |
||
| 290 | */ |
||
| 291 | 1 | public function findWhere(array $where, $columns = ['*']) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Find data by Criteria |
||
| 298 | * |
||
| 299 | * @param CriteriaInterface $criteria |
||
| 300 | * |
||
| 301 | * @return mixed |
||
| 302 | */ |
||
| 303 | 1 | public function getByCriteria(CriteriaInterface $criteria) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Pluck |
||
| 310 | * |
||
| 311 | * @param string $column |
||
| 312 | * @param array $key |
||
| 313 | * |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | 1 | public function pluck($column, $key = null) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Count |
||
| 323 | * |
||
| 324 | * @param array $input Array Input |
||
| 325 | * |
||
| 326 | * @return int |
||
| 327 | */ |
||
| 328 | 1 | public function count(array $input = array()) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Max |
||
| 335 | * |
||
| 336 | * @param mixed $field Mixed Field |
||
| 337 | * @param array $input Array Input |
||
| 338 | * |
||
| 339 | * @return mixed |
||
| 340 | */ |
||
| 341 | 1 | public function max($field, array $input = array()) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Min |
||
| 348 | * |
||
| 349 | * @param mixed $field Mixed Field |
||
| 350 | * @param array $input Array Input |
||
| 351 | * |
||
| 352 | * @return mixed |
||
| 353 | */ |
||
| 354 | 1 | public function min($field, array $input = array()) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Sum |
||
| 361 | * |
||
| 362 | * @param mixed $field Mixed Field |
||
| 363 | * @param array $input Array Input |
||
| 364 | * |
||
| 365 | * @return float |
||
| 366 | */ |
||
| 367 | 1 | public function sum($field, array $input = array()) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Average |
||
| 374 | * |
||
| 375 | * @param mixed $field Mixed Field |
||
| 376 | * @param array $input Array Input |
||
| 377 | * |
||
| 378 | * @return float |
||
| 379 | */ |
||
| 380 | 1 | public function avg($field, array $input = array()) |
|
| 384 | } |
||
| 385 |
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: