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 |
||
| 11 | class RouteTranslationsCacheCommand extends Command |
||
| 12 | { |
||
| 13 | use TranslatedRouteCommandContext; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $name = 'route:trans:cache'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $description = 'Create a route cache file for faster route registration for all locales'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The filesystem instance. |
||
| 27 | * |
||
| 28 | * @var Filesystem |
||
| 29 | */ |
||
| 30 | protected $files; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Create a new route command instance. |
||
| 34 | * |
||
| 35 | * @param Filesystem $files |
||
| 36 | */ |
||
| 37 | public function __construct(Filesystem $files) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Execute the console command. |
||
| 46 | */ |
||
| 47 | public function handle() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Cache the routes separately for each locale. |
||
| 58 | */ |
||
| 59 | View Code Duplication | protected function cacheRoutesPerLocale() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Boot a fresh copy of the application and get the routes. |
||
| 88 | * |
||
| 89 | * @param string|null $locale |
||
| 90 | * @return \Illuminate\Routing\RouteCollection |
||
| 91 | */ |
||
| 92 | protected function getFreshApplicationRoutes($locale = null) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Build the route cache file. |
||
| 116 | * |
||
| 117 | * @param \Illuminate\Routing\RouteCollection $routes |
||
| 118 | * @return string |
||
| 119 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 120 | */ |
||
| 121 | View Code Duplication | protected function buildRouteCacheFile(RouteCollection $routes) |
|
| 144 | } |
||
| 145 |
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.