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 string |
||
| 35 | */ |
||
| 36 | protected $model; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The repository identifier. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $repositoryId; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The repository cache status. |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $cacheEnabled = true; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The repository cache clear status. |
||
| 54 | * |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | protected $cacheClear = true; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Execute given callback and cache result set. |
||
| 61 | * |
||
| 62 | * @param string $class |
||
| 63 | * @param string $method |
||
| 64 | * @param string $cacheKey |
||
|
|
|||
| 65 | * @param \Closure $closure |
||
| 66 | * |
||
| 67 | * @return mixed |
||
| 68 | */ |
||
| 69 | protected function executeCallback($class, $method, $hash, Closure $closure) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Set the IoC container instance. |
||
| 90 | * |
||
| 91 | * @param \Illuminate\Contracts\Container\Container $container |
||
| 92 | * |
||
| 93 | * @return $this |
||
| 94 | */ |
||
| 95 | public function setContainer(Container $container) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Return the IoC container instance or any of it's services. |
||
| 104 | * |
||
| 105 | * @param string $service |
||
| 106 | * |
||
| 107 | * @return mixed |
||
| 108 | */ |
||
| 109 | public function getContainer($service = null) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Set the repository identifier. |
||
| 116 | * |
||
| 117 | * @param string $repositoryId |
||
| 118 | * |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | public function setRepositoryId($repositoryId) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get the repository identifier. |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function getRepositoryId() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Set the repository cache status. |
||
| 140 | * |
||
| 141 | * @param bool $status |
||
| 142 | * |
||
| 143 | * @return $this |
||
| 144 | */ |
||
| 145 | public function setCacheStatus($status) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get the repository cache status. |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | public function getCacheStatus() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Set the repository cache clear status. |
||
| 164 | * |
||
| 165 | * @param bool $status |
||
| 166 | * |
||
| 167 | * @return $this |
||
| 168 | */ |
||
| 169 | public function setCacheClearStatus($status) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get the repository cache clear status. |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | public function getCacheClearStatus() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Forget the repository cache. |
||
| 188 | * |
||
| 189 | * @return $this |
||
| 190 | */ |
||
| 191 | public function forgetCache() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set the relationships that should be eager loaded. |
||
| 213 | * |
||
| 214 | * @param mixed $relations |
||
| 215 | * |
||
| 216 | * @return $this |
||
| 217 | */ |
||
| 218 | public function with($relations) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Add an "order by" clause to the repository. |
||
| 227 | * |
||
| 228 | * @param string $column |
||
| 229 | * @param string $direction |
||
| 230 | * |
||
| 231 | * @return $this |
||
| 232 | */ |
||
| 233 | public function orderBy($column, $direction = 'asc') |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Register a new global scope. |
||
| 242 | * |
||
| 243 | * @param \Illuminate\Database\Eloquent\Scope|\Closure|string $scope |
||
| 244 | * @param \Closure|null $implementation |
||
| 245 | * |
||
| 246 | * @throws \InvalidArgumentException |
||
| 247 | * |
||
| 248 | * @return mixed |
||
| 249 | */ |
||
| 250 | public function addGlobalScope($scope, Closure $implementation = null) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Remove all or passed registered global scopes. |
||
| 257 | * |
||
| 258 | * @param array|null $scopes |
||
| 259 | * |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | public function withoutGlobalScopes(array $scopes = null) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Dynamically pass missing static methods to the model. |
||
| 271 | * |
||
| 272 | * @param $method |
||
| 273 | * @param $parameters |
||
| 274 | * |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | public static function __callStatic($method, $parameters) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Dynamically pass missing methods to the model. |
||
| 284 | * |
||
| 285 | * @param string $method |
||
| 286 | * @param array $parameters |
||
| 287 | * |
||
| 288 | * @return mixed |
||
| 289 | */ |
||
| 290 | public function __call($method, $parameters) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Store cache keys by mimicking cache tags. |
||
| 299 | * |
||
| 300 | * @param $class |
||
| 301 | * @param $method |
||
| 302 | * @param $hash |
||
| 303 | * @param $config |
||
| 304 | * |
||
| 305 | * @return void |
||
| 306 | */ |
||
| 307 | protected function storeCacheKeys($class, $method, $hash, $config) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Flush cache keys by mimicking cache tags. |
||
| 319 | * |
||
| 320 | * @return array |
||
| 321 | */ |
||
| 322 | protected function flushCacheKeys() |
||
| 340 | } |
||
| 341 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.