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 | * Set Cache Repository |
||
| 21 | * |
||
| 22 | * @param CacheRepository $repository |
||
| 23 | * |
||
| 24 | * @return $this |
||
| 25 | */ |
||
| 26 | public function setCacheRepository(CacheRepository $repository) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Return instance of Cache Repository |
||
| 35 | * |
||
| 36 | * @return CacheRepository |
||
| 37 | */ |
||
| 38 | 1 | public function getCacheRepository() |
|
| 46 | |||
| 47 | /** |
||
| 48 | * CacheTags |
||
| 49 | * |
||
| 50 | * @param mixed $tags |
||
| 51 | * |
||
| 52 | * @return \Illuminate\Cache\TaggedCache |
||
| 53 | */ |
||
| 54 | 1 | public function cacheTags($tags = null) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Skip Cache |
||
| 63 | * |
||
| 64 | * @param bool $status |
||
| 65 | * |
||
| 66 | * @return $this |
||
| 67 | */ |
||
| 68 | public function skipCache($status = true) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Is Skipped Cache |
||
| 77 | * |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | 1 | public function isSkippedCache() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Allowed Cache |
||
| 95 | * |
||
| 96 | * @param string $method |
||
| 97 | * |
||
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | 1 | protected function allowedCache($method) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Get Cache key for the method |
||
| 128 | * |
||
| 129 | * @param string $method |
||
| 130 | * @param array $args |
||
| 131 | * |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | 1 | public function getCacheKey($method, $args = null) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Serialize the criteria making sure the Closures are taken care of. |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | 1 | protected function serializeCriteria() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Serialize single criterion with customized serialization of Closures. |
||
| 162 | * |
||
| 163 | * @param \Prettus\Repository\Contracts\CriteriaInterface $criterion |
||
| 164 | * |
||
| 165 | * @return \Prettus\Repository\Contracts\CriteriaInterface|array |
||
| 166 | * |
||
| 167 | * @throws \Exception |
||
| 168 | */ |
||
| 169 | protected function serializeCriterion($criterion) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get cache minutes |
||
| 193 | * |
||
| 194 | * @return int |
||
| 195 | */ |
||
| 196 | 1 | public function getCacheMinutes() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * call Cache |
||
| 205 | * |
||
| 206 | * @param string $method |
||
| 207 | * @param array $args |
||
| 208 | * |
||
| 209 | * @return mixed |
||
| 210 | */ |
||
| 211 | 1 | public function callCache($method, array $args) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Retrieve all data of repository |
||
| 228 | * |
||
| 229 | * @param array $columns |
||
| 230 | * |
||
| 231 | * @return mixed |
||
| 232 | */ |
||
| 233 | 1 | public function all($columns = ['*']) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Retrieve all data of repository, paginated |
||
| 240 | * |
||
| 241 | * @param null $limit |
||
| 242 | * @param array $columns |
||
| 243 | * |
||
| 244 | * @return mixed |
||
| 245 | */ |
||
| 246 | 1 | public function paginate($limit = null, $columns = ['*']) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Find data by id |
||
| 253 | * |
||
| 254 | * @param int $id |
||
| 255 | * @param array $columns |
||
| 256 | * |
||
| 257 | * @return mixed |
||
| 258 | */ |
||
| 259 | 1 | public function find($id, $columns = ['*']) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Find data by field and value |
||
| 266 | * |
||
| 267 | * @param string $field |
||
| 268 | * @param mixed $value |
||
| 269 | * @param array $columns |
||
| 270 | * |
||
| 271 | * @return mixed |
||
| 272 | */ |
||
| 273 | 1 | public function findByField($field, $value = null, $columns = ['*']) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Find data by multiple fields |
||
| 280 | * |
||
| 281 | * @param array $where |
||
| 282 | * @param array $columns |
||
| 283 | * |
||
| 284 | * @return mixed |
||
| 285 | */ |
||
| 286 | 1 | public function findWhere(array $where, $columns = ['*']) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Find data by Criteria |
||
| 293 | * |
||
| 294 | * @param CriteriaInterface $criteria |
||
| 295 | * |
||
| 296 | * @return mixed |
||
| 297 | */ |
||
| 298 | 1 | public function getByCriteria(CriteriaInterface $criteria) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Pluck |
||
| 305 | * |
||
| 306 | * @param string $column |
||
| 307 | * @param array $key |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | 1 | public function pluck($column, $key = null) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Count |
||
| 318 | * |
||
| 319 | * @param array $input Array Input |
||
| 320 | * |
||
| 321 | * @return int |
||
| 322 | */ |
||
| 323 | 1 | public function count(array $input = array()) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Max |
||
| 330 | * |
||
| 331 | * @param mixed $field Mixed Field |
||
| 332 | * @param array $input Array Input |
||
| 333 | * |
||
| 334 | * @return mixed |
||
| 335 | */ |
||
| 336 | 1 | public function max($field, array $input = array()) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Min |
||
| 343 | * |
||
| 344 | * @param mixed $field Mixed Field |
||
| 345 | * @param array $input Array Input |
||
| 346 | * |
||
| 347 | * @return mixed |
||
| 348 | */ |
||
| 349 | 1 | public function min($field, array $input = array()) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Sum |
||
| 356 | * |
||
| 357 | * @param mixed $field Mixed Field |
||
| 358 | * @param array $input Array Input |
||
| 359 | * |
||
| 360 | * @return float |
||
| 361 | */ |
||
| 362 | 1 | public function sum($field, array $input = array()) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Average |
||
| 369 | * |
||
| 370 | * @param mixed $field Mixed Field |
||
| 371 | * @param array $input Array Input |
||
| 372 | * |
||
| 373 | * @return float |
||
| 374 | */ |
||
| 375 | 1 | public function avg($field, array $input = array()) |
|
| 379 | } |
||
| 380 |
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: