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 |
||
| 19 | trait PortServiceProviderTrait |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Write the DB queries in the Log and Display them in the |
||
| 24 | * terminal (in case you want to see them while executing the tests). |
||
| 25 | * |
||
| 26 | * @param bool|false $terminal |
||
| 27 | */ |
||
| 28 | public function debugDatabaseQueries($log = true, $terminal = false) |
||
| 29 | { |
||
| 30 | if (Config::get('database.query_debugging')) { |
||
| 31 | DB::listen(function ($event) use ($terminal, $log) { |
||
| 32 | $fullQuery = vsprintf(str_replace(['%', '?'], ['%%', '%s'], $event->sql), $event->bindings); |
||
| 33 | |||
| 34 | $text = $event->connectionName . ' (' . $event->time . '): ' . $fullQuery; |
||
| 35 | |||
| 36 | if ($terminal) { |
||
| 37 | dump($text); |
||
| 38 | } |
||
| 39 | |||
| 40 | if ($log) { |
||
| 41 | Log::info($text); |
||
| 42 | } |
||
| 43 | }); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * By default Laravel takes (server/database/factories) as the |
||
| 49 | * path to the factories, this function changes the path to load |
||
| 50 | * the factories from the infrastructure directory. |
||
| 51 | */ |
||
| 52 | public function changeTheDefaultDatabaseModelsFactoriesPath($customPath) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get the containers Service Providers full classes names. |
||
| 63 | * |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public function getMainServiceProviders() |
||
| 67 | { |
||
| 68 | $containersNamespace = PortButler::getContainersNamespace(); |
||
| 69 | |||
| 70 | $allServiceProviders = []; |
||
| 71 | |||
| 72 | foreach (PortButler::getContainersNames() as $containerName) { |
||
| 73 | // append the Module main service provider |
||
| 74 | $allServiceProviders[] = PortButler::buildMainServiceProvider($containersNamespace, $containerName); |
||
| 75 | } |
||
| 76 | |||
| 77 | return array_unique($allServiceProviders) ? : []; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Load views from inside the Containers |
||
| 82 | */ |
||
| 83 | public function autoLoadViewsFromContainers() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Load migrations files from Containers to Laravel |
||
| 97 | */ |
||
| 98 | public function autoMigrationsFromContainers() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * TODO: needs refactoring, was created in 5 min |
||
| 117 | * |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | public function getAllContainersConsoleCommandsForAutoLoading() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Auto load Containers and Port config files into Laravel |
||
| 141 | */ |
||
| 142 | public function autoLoadConfigFiles() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * |
||
| 150 | */ |
||
| 151 | protected function autoLoadContainersConfigFiles() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * |
||
| 160 | */ |
||
| 161 | protected function autoLoadPortConfigFiles() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param $directory |
||
| 168 | */ |
||
| 169 | private function loadConfigs($directory) |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * By default the Dingo API package (in the config file) creates an instance of the |
||
| 188 | * fractal manager which takes the default serializer (specified by the fractal |
||
| 189 | * package itself, and there's no way to override change it from the configurations of |
||
| 190 | * the Dingo package). |
||
| 191 | * |
||
| 192 | * Here I am replacing the current default serializer (DataArraySerializer) by the |
||
| 193 | * (JsonApiSerializer). |
||
| 194 | * |
||
| 195 | * "Serializers are what build the final response after taking the transformers data". |
||
| 196 | */ |
||
| 197 | public function overrideDefaultFractalSerializer() |
||
| 222 | } |
||
| 223 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.