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 LaravelDatabaseRepository 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 LaravelDatabaseRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class LaravelDatabaseRepository extends LaravelFileRepository implements DatabaseRepositoryInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Creates a new Module instance. |
||
| 26 | * |
||
| 27 | * @param mixed ...$args |
||
| 28 | * |
||
| 29 | * @return DatabaseModule |
||
| 30 | */ |
||
| 31 | protected function createModule(...$args) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @return ModuleEntity |
||
| 38 | */ |
||
| 39 | public function getModel() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Scan & get all available modules. |
||
| 46 | */ |
||
| 47 | public function scan() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get all modules as laravel collection instance. |
||
| 69 | * |
||
| 70 | * @param $status |
||
| 71 | * |
||
| 72 | * @return Collection |
||
| 73 | */ |
||
| 74 | public function collections($status = 1): Collection |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get module path for a specific module. |
||
| 81 | * |
||
| 82 | * @param $name |
||
| 83 | * |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public function getModulePath($name) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get modules by status. |
||
| 98 | * |
||
| 99 | * @param $status |
||
| 100 | * |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | View Code Duplication | public function getByStatus($status): array |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Format the cached data as array of modules. |
||
| 118 | * |
||
| 119 | * @param array $cached |
||
| 120 | * |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | protected function formatCached($cached) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get cached modules from database. |
||
| 138 | * |
||
| 139 | * @return ModuleEntity[] |
||
| 140 | */ |
||
| 141 | public function getCached() |
||
| 147 | |||
| 148 | public function all(): array |
||
| 157 | |||
| 158 | public function create($params, $force = true, $isApi = true, $isPlain = true) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get module type . |
||
| 179 | * |
||
| 180 | * @param bool $isApi |
||
| 181 | * @param bool $isPlain |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function getModuleType($isApi = true, $isPlain = true) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get module used for cli session. |
||
| 201 | * @return string |
||
| 202 | * @throws ModuleNotFoundException|FileNotFoundException |
||
| 203 | */ |
||
| 204 | public function getUsedNow(): string |
||
| 213 | |||
| 214 | public function migrateFileToDatabase($forceUpdate = false) |
||
| 250 | |||
| 251 | public function update($name) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Validate array attributes before insert/update into database. |
||
| 258 | * |
||
| 259 | * @param array $attributes |
||
| 260 | * @param array $allows |
||
| 261 | * |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | protected function validateAttributes(array $attributes, array $allows = []) |
||
| 274 | |||
| 275 | View Code Duplication | public function getOrdered($direction = 'asc'): array |
|
| 293 | } |
||
| 294 |