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 |
||
| 12 | class ModelMakeCommand extends GeneratorCommand |
||
| 13 | { |
||
| 14 | use ModuleCommandTrait; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * The name of argument name. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $argumentName = 'model'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The console command name. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $name = 'module:make-model'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The console command description. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $description = 'Create a new model for the specified module.'; |
||
| 36 | |||
| 37 | public function handle() : int |
||
| 38 | { |
||
| 39 | if (parent::handle() === E_ERROR) { |
||
| 40 | return E_ERROR; |
||
| 41 | } |
||
| 42 | |||
| 43 | $this->handleOptionalMigrationOption(); |
||
| 44 | |||
| 45 | return 0; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Create a proper migration name: |
||
| 50 | * ProductDetail: product_details |
||
| 51 | * Product: products |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | private function createMigrationName() |
||
| 55 | { |
||
| 56 | $pieces = preg_split('/(?=[A-Z])/', $this->argument('model'), -1, PREG_SPLIT_NO_EMPTY); |
||
| 57 | |||
| 58 | $string = ''; |
||
| 59 | foreach ($pieces as $i => $piece) { |
||
| 60 | if ($i+1 < count($pieces)) { |
||
| 61 | $string .= strtolower($piece) . '_'; |
||
| 62 | } else { |
||
| 63 | $string .= Str::plural(strtolower($piece)); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | return $string; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get the console command arguments. |
||
| 72 | * |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | 4 | protected function getArguments() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Get the console command options. |
||
| 85 | * |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | 4 | protected function getOptions() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Create the migration file with the given model if migration flag was used |
||
| 98 | */ |
||
| 99 | private function handleOptionalMigrationOption() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return mixed |
||
| 109 | */ |
||
| 110 | View Code Duplication | protected function getTemplateContents() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @return mixed |
||
| 128 | */ |
||
| 129 | protected function getDestinationFilePath() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return mixed|string |
||
| 140 | */ |
||
| 141 | private function getModelName() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | private function getFillable() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get default namespace. |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function getDefaultNamespace() : string |
||
| 173 | } |
||
| 174 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.