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:
| 1 | <?php |
||
| 18 | class LaravelDatabaseRepository extends LaravelFileRepository implements DatabaseRepositoryInterface |
||
| 19 | { |
||
| 20 | protected function createModule(...$args) |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @return ModuleEntity |
||
| 27 | */ |
||
| 28 | public function getModel() |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Scan & get all available modules. |
||
| 35 | */ |
||
| 36 | public function scan() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Determine whether the given module exist. |
||
| 56 | * |
||
| 57 | * @param $name |
||
| 58 | * |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | public function has($name): bool |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @inheritDoc |
||
| 68 | */ |
||
| 69 | public function find(string $name) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Get all modules as laravel collection instance. |
||
| 85 | * |
||
| 86 | * @param $status |
||
| 87 | * |
||
| 88 | * @return Collection |
||
| 89 | */ |
||
| 90 | public function collections($status = 1): Collection |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get module path for a specific module. |
||
| 97 | * |
||
| 98 | * @param $name |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getModulePath($name) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get modules by status. |
||
| 114 | * |
||
| 115 | * @param $status |
||
| 116 | * |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | View Code Duplication | public function getByStatus($status): array |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Format the cached data as array of modules. |
||
| 134 | * |
||
| 135 | * @param array $cached |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | protected function formatCached($cached) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get cached modules from database. |
||
| 154 | * |
||
| 155 | * @return ModuleEntity[] |
||
| 156 | */ |
||
| 157 | public function getCached() |
||
| 163 | |||
| 164 | public function all(): array |
||
| 173 | |||
| 174 | public function create($params, $force = true, $isApi = true, $isPlain = true) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get module type . |
||
| 195 | * |
||
| 196 | * @param bool $isApi |
||
| 197 | * @param bool $isPlain |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function getModuleType($isApi = true, $isPlain = true) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get module used for cli session. |
||
| 217 | * @return string |
||
| 218 | * @throws ModuleNotFoundException|FileNotFoundException |
||
| 219 | */ |
||
| 220 | public function getUsedNow(): string |
||
| 229 | |||
| 230 | public function migrateFileToDatabase() |
||
| 250 | } |
||
| 251 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: