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 |
||
| 9 | class ListCommand extends Command |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The console command name. |
||
| 13 | * |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | protected $name = 'module:list'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The console command description. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $description = 'Show list of all modules.'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Execute the console command. |
||
| 27 | */ |
||
| 28 | 1 | public function handle() |
|
| 29 | { |
||
| 30 | 1 | $this->table(['Name', 'Status', 'Order', 'Path'], $this->getRows()); |
|
| 31 | 1 | } |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Get table rows. |
||
| 35 | * |
||
| 36 | * @return array |
||
| 37 | */ |
||
| 38 | 1 | public function getRows() |
|
| 39 | { |
||
| 40 | 1 | $rows = []; |
|
| 41 | |||
| 42 | /** @var Module $module */ |
||
| 43 | 1 | foreach ($this->getModules() as $module) { |
|
| 44 | 1 | $rows[] = [ |
|
| 45 | 1 | $module->getName(), |
|
| 46 | 1 | $module->isEnabled() ? 'Enabled' : 'Disabled', |
|
| 47 | 1 | $module->get('order'), |
|
| 48 | 1 | $module->getPath(), |
|
| 49 | ]; |
||
| 50 | } |
||
| 51 | |||
| 52 | 1 | return $rows; |
|
| 53 | } |
||
| 54 | |||
| 55 | 1 | public function getModules() |
|
| 56 | { |
||
| 57 | 1 | switch ($this->option('only')) { |
|
| 58 | 1 | case 'enabled': |
|
| 59 | return $this->laravel['modules']->getByStatus(1); |
||
| 60 | break; |
||
|
|
|||
| 61 | |||
| 62 | 1 | case 'disabled': |
|
| 63 | return $this->laravel['modules']->getByStatus(0); |
||
| 64 | break; |
||
| 65 | |||
| 66 | 1 | case 'ordered': |
|
| 67 | return $this->laravel['modules']->getOrdered($this->option('direction')); |
||
| 68 | break; |
||
| 69 | |||
| 70 | default: |
||
| 71 | 1 | return $this->laravel['modules']->all(); |
|
| 72 | break; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get the console command options. |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | 120 | View Code Duplication | protected function getOptions() |
| 88 | } |
||
| 89 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.