Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DatabaseRepository 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 DatabaseRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | abstract class DatabaseRepository implements RepositoryInterface, Countable |
||
| 17 | { |
||
| 18 | use Macroable; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Application instance. |
||
| 22 | * |
||
| 23 | * @var \Illuminate\Contracts\Foundation\Application |
||
| 24 | */ |
||
| 25 | protected $app; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The module path. |
||
| 29 | * |
||
| 30 | * @var string|null |
||
| 31 | */ |
||
| 32 | protected $path; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The scanned paths. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $paths = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $stubPath; |
||
| 45 | /** |
||
| 46 | * @var UrlGenerator |
||
| 47 | */ |
||
| 48 | private $url; |
||
| 49 | /** |
||
| 50 | * @var ConfigRepository |
||
| 51 | */ |
||
| 52 | private $config; |
||
| 53 | /** |
||
| 54 | * @var Filesystem |
||
| 55 | */ |
||
| 56 | private $files; |
||
| 57 | /** |
||
| 58 | * @var CacheManager |
||
| 59 | */ |
||
| 60 | private $cache; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The constructor. |
||
| 64 | * |
||
| 65 | * @param Container $app |
||
| 66 | * @param string|null $path |
||
| 67 | */ |
||
| 68 | View Code Duplication | public function __construct(Container $app, $path = null) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Get all modules. |
||
| 80 | * |
||
| 81 | * @return mixed |
||
| 82 | */ |
||
| 83 | public function all() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get cached modules. |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | public function getCached() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Scan & get all available modules. |
||
| 106 | * |
||
| 107 | * @return array |
||
| 108 | */ |
||
| 109 | public function scan() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get modules as modules collection instance. |
||
| 118 | * |
||
| 119 | * @return \Nwidart\Modules\Collection |
||
| 120 | */ |
||
| 121 | public function toCollection() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get scanned paths. |
||
| 128 | * |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | public function getScanPaths() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Determine whether the given module exist. |
||
| 138 | * |
||
| 139 | * @param $name |
||
| 140 | * |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | public function has($name) : bool |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get list of enabled modules. |
||
| 150 | * |
||
| 151 | * @return mixed |
||
| 152 | */ |
||
| 153 | public function allEnabled() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get list of disabled modules. |
||
| 160 | * |
||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | public function allDisabled() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get count from all modules. |
||
| 170 | * |
||
| 171 | * @return int |
||
| 172 | */ |
||
| 173 | public function count() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get all ordered modules. |
||
| 180 | * |
||
| 181 | * @param string $direction |
||
| 182 | * @return mixed |
||
| 183 | */ |
||
| 184 | public function getOrdered($direction = 'asc') |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get modules by the given status. |
||
| 193 | * |
||
| 194 | * @param int $status |
||
| 195 | * |
||
| 196 | * @return mixed |
||
| 197 | */ |
||
| 198 | public function getByStatus($status) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Find a specific module. |
||
| 207 | * |
||
| 208 | * @param $name |
||
| 209 | * @return Module|null |
||
| 210 | */ |
||
| 211 | public function find(string $name) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Find all modules that are required by a module. If the module cannot be found, throw an exception. |
||
| 224 | * |
||
| 225 | * @param $name |
||
| 226 | * @return array |
||
| 227 | * @throws ModuleNotFoundException |
||
| 228 | */ |
||
| 229 | View Code Duplication | public function findRequirements($name): array |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Find a specific module. If there return that, otherwise throw exception. |
||
| 244 | * |
||
| 245 | * @param $name |
||
| 246 | * |
||
| 247 | * @return mixed |
||
| 248 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
| 249 | */ |
||
| 250 | View Code Duplication | public function findOrFail(string $name) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Find a specific module. If there return that, otherwise create it. |
||
| 263 | * |
||
| 264 | * @param $name |
||
| 265 | * |
||
| 266 | * @return Module |
||
| 267 | */ |
||
| 268 | public function findByNameOrCreate(string $name) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param $name |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | public function getModulePath($name) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return \Illuminate\Filesystem\Filesystem |
||
| 286 | */ |
||
| 287 | public function getFiles() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get a specific config data from a configuration file. |
||
| 294 | * |
||
| 295 | * @param string $key |
||
| 296 | * |
||
| 297 | * @param string|null $default |
||
| 298 | * @return mixed |
||
| 299 | */ |
||
| 300 | public function config(string $key, $default = null) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Get a module path. |
||
| 307 | * |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | public function getPath(): string |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Find a specific module by its alias. |
||
| 317 | * |
||
| 318 | * @param string $alias |
||
| 319 | * @return Module|void |
||
| 320 | */ |
||
| 321 | public function findByAlias(string $alias) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Boot the modules. |
||
| 334 | */ |
||
| 335 | public function boot(): void |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Register the modules. |
||
| 344 | */ |
||
| 345 | public function register(): void |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get asset path for a specific module. |
||
| 354 | * |
||
| 355 | * @param string $module |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public function assetPath(string $module): string |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Delete a specific module. |
||
| 365 | * |
||
| 366 | * @param string $module |
||
| 367 | * @return bool |
||
| 368 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
| 369 | */ |
||
| 370 | public function delete(string $module): bool |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Determine whether the given module is activated. |
||
| 377 | * |
||
| 378 | * @param string $name |
||
| 379 | * @return bool |
||
| 380 | * @throws ModuleNotFoundException |
||
| 381 | */ |
||
| 382 | public function isEnabled(string $name): bool |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Determine whether the given module is not activated. |
||
| 389 | * |
||
| 390 | * @param string $name |
||
| 391 | * @return bool |
||
| 392 | * @throws ModuleNotFoundException |
||
| 393 | */ |
||
| 394 | public function isDisabled(string $name): bool |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Format the cached data as array of modules. |
||
| 401 | * |
||
| 402 | * @param array $cached |
||
| 403 | * |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | View Code Duplication | protected function formatCached($cached) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Creates a new Module instance |
||
| 419 | * |
||
| 420 | * @param Container $app |
||
| 421 | * @param string $args |
||
| 422 | * @param string $path |
||
| 423 | * @return \Nwidart\Modules\Module |
||
| 424 | */ |
||
| 425 | abstract protected function createModule(...$args); |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Parse from ModuleEntityCollection to Module array |
||
| 429 | * |
||
| 430 | * @param \Illuminate\Database\Eloquent\Collection $entities |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | protected function parse(\Illuminate\Database\Eloquent\Collection $entities) |
||
| 443 | } |
||
| 444 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..