Complex classes like BaseRepository 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 BaseRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class BaseRepository implements RepositoryContract |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * The IoC container instance. |
||
| 26 | * |
||
| 27 | * @var \Illuminate\Contracts\Container\Container |
||
| 28 | */ |
||
| 29 | protected $container; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The repository model. |
||
| 33 | * |
||
| 34 | * @var object |
||
| 35 | */ |
||
| 36 | protected $model; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The repository identifier. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $repositoryId; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Indicate if the repository cache is enabled. |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $cacheEnabled = true; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Indicate if the repository cache clear is enabled. |
||
| 54 | * |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | protected $cacheClearEnabled = true; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The repository cache lifetime. |
||
| 61 | * |
||
| 62 | * @var float|int |
||
| 63 | */ |
||
| 64 | protected $cacheLifetime; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The repository cache driver. |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $cacheDriver; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Execute given callback and cache result set. |
||
| 75 | * |
||
| 76 | * @param string $class |
||
| 77 | * @param string $method |
||
| 78 | * @param array $args |
||
| 79 | * @param \Closure $closure |
||
| 80 | * |
||
| 81 | * @return mixed |
||
| 82 | */ |
||
| 83 | protected function executeCallback($class, $method, $args, Closure $closure) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Set the IoC container instance. |
||
| 115 | * |
||
| 116 | * @param \Illuminate\Contracts\Container\Container $container |
||
| 117 | * |
||
| 118 | * @return $this |
||
| 119 | */ |
||
| 120 | public function setContainer(Container $container) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the IoC container instance or any of it's services. |
||
| 129 | * |
||
| 130 | * @param string|null $service |
||
| 131 | * |
||
| 132 | * @return mixed |
||
| 133 | */ |
||
| 134 | public function getContainer($service = null) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Set the repository identifier. |
||
| 141 | * |
||
| 142 | * @param string $repositoryId |
||
| 143 | * |
||
| 144 | * @return $this |
||
| 145 | */ |
||
| 146 | public function setRepositoryId($repositoryId) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Get the repository identifier. |
||
| 155 | * |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function getRepositoryId() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Set the repository cache lifetime. |
||
| 165 | * |
||
| 166 | * @param int $cacheLifetime |
||
| 167 | * |
||
| 168 | * @return $this |
||
| 169 | */ |
||
| 170 | public function setCacheLifetime($cacheLifetime) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get the repository cache lifetime. |
||
| 179 | * |
||
| 180 | * @return int |
||
| 181 | */ |
||
| 182 | public function getCacheLifetime() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Convenience method. Alias to setCacheLifetime(0). |
||
| 191 | * |
||
| 192 | * @return $this |
||
| 193 | */ |
||
| 194 | public function skipCache() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Set the repository cache driver. |
||
| 203 | * |
||
| 204 | * @param string $cacheDriver |
||
| 205 | * |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function setCacheDriver($cacheDriver) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get the repository cache driver. |
||
| 217 | * |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | public function getCacheDriver() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Enable repository cache. |
||
| 227 | * |
||
| 228 | * @param bool $status |
||
| 229 | * |
||
| 230 | * @return $this |
||
| 231 | */ |
||
| 232 | public function enableCache($status = true) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Determine if repository cache is enabled. |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | public function isCacheEnabled() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Enable repository cache clear. |
||
| 251 | * |
||
| 252 | * @param bool $status |
||
| 253 | * |
||
| 254 | * @return $this |
||
| 255 | */ |
||
| 256 | public function enableCacheClear($status = true) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Determine if repository cache clear is enabled. |
||
| 265 | * |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | public function isCacheClearEnabled() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Forget the repository cache. |
||
| 275 | * |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | public function forgetCache() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Set the relationships that should be eager loaded. |
||
| 298 | * |
||
| 299 | * @param mixed $relations |
||
| 300 | * |
||
| 301 | * @return $this |
||
| 302 | */ |
||
| 303 | public function with($relations) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Add an "order by" clause to the repository. |
||
| 312 | * |
||
| 313 | * @param string $column |
||
| 314 | * @param string $direction |
||
| 315 | * |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function orderBy($column, $direction = 'asc') |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Register a new global scope. |
||
| 327 | * |
||
| 328 | * @param \Illuminate\Database\Eloquent\Scope|\Closure|string $scope |
||
| 329 | * @param \Closure|null $implementation |
||
| 330 | * |
||
| 331 | * @throws \InvalidArgumentException |
||
| 332 | * |
||
| 333 | * @return mixed |
||
| 334 | */ |
||
| 335 | public function addGlobalScope($scope, Closure $implementation = null) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Remove all or passed registered global scopes. |
||
| 342 | * |
||
| 343 | * @param array|null $scopes |
||
| 344 | * |
||
| 345 | * @return $this |
||
| 346 | */ |
||
| 347 | public function withoutGlobalScopes(array $scopes = null) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Dynamically pass missing static methods to the model. |
||
| 356 | * |
||
| 357 | * @param $method |
||
| 358 | * @param $parameters |
||
| 359 | * |
||
| 360 | * @return mixed |
||
| 361 | */ |
||
| 362 | public static function __callStatic($method, $parameters) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Dynamically pass missing methods to the model. |
||
| 369 | * |
||
| 370 | * @param string $method |
||
| 371 | * @param array $parameters |
||
| 372 | * |
||
| 373 | * @return mixed |
||
| 374 | */ |
||
| 375 | public function __call($method, $parameters) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Store cache keys by mimicking cache tags. |
||
| 384 | * |
||
| 385 | * @param string $class |
||
| 386 | * @param string $method |
||
| 387 | * @param string $hash |
||
| 388 | * |
||
| 389 | * @return void |
||
| 390 | */ |
||
| 391 | protected function storeCacheKeys($class, $method, $hash) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Flush cache keys by mimicking cache tags. |
||
| 404 | * |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | protected function flushCacheKeys() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Get cache keys. |
||
| 428 | * |
||
| 429 | * @param string $file |
||
| 430 | * |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | protected function getCacheKeys($file) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Determine if repository is cacheable. |
||
| 440 | * |
||
| 441 | * @return bool |
||
| 442 | */ |
||
| 443 | protected function isCacheable() |
||
| 450 | } |
||
| 451 |
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.