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 |
||
13 | class MakeUser extends Command |
||
14 | { |
||
15 | /** |
||
16 | * The name and signature of the console command. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $signature = 'make:user |
||
21 | {--email= : Set the email for the new user} |
||
22 | {--name= : Set the name for the new user} |
||
23 | {--password= : The password to set for the new user} |
||
24 | {--send-reset : Send a password reset email for the new user} |
||
25 | {--fields= : Additional database fields to set on the user} |
||
26 | {--force : Create the user model circumventing guarded fields} |
||
27 | {--import-file= : Relative path and filename for a file to import users from. File name MUST contain the extension representing the type of file (Ex: ./path/to/file.csv)} |
||
28 | '; |
||
29 | |||
30 | /** |
||
31 | * The console command description. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $description = 'Create new application users'; |
||
36 | |||
37 | /** |
||
38 | * Execute the console command. |
||
39 | * |
||
40 | * Handle creation of the new application user. |
||
41 | * |
||
42 | * @return void |
||
43 | */ |
||
44 | public function handle() |
||
116 | |||
117 | /** |
||
118 | * Determine if the given email address already exists. |
||
119 | * |
||
120 | * @param string $email |
||
121 | * @return void |
||
122 | * |
||
123 | * @throws \Dyrynda\Artisan\Exceptions\MakeUserException |
||
124 | */ |
||
125 | private function validateEmail($email) |
||
135 | |||
136 | /** |
||
137 | * Return any additional database fields passed by the --fields option. |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | private function additionalFields() |
||
153 | |||
154 | /** |
||
155 | * Normalise the given (database) field input value. |
||
156 | * |
||
157 | * @param mixed $value |
||
158 | * @return mixed |
||
159 | */ |
||
160 | private function normaliseValue($value) |
||
176 | |||
177 | /** |
||
178 | * Create file handler objects |
||
179 | * |
||
180 | * @param string $path |
||
181 | * @return BulkImportFileHandler |
||
182 | * |
||
183 | * @throws \Dyrynda\Artisan\Exceptions\ImportFileException |
||
184 | */ |
||
185 | private function fileHandlerFactory($path) : BulkImportFileHandler |
||
199 | |||
200 | /** |
||
201 | * Add default password to data |
||
202 | * |
||
203 | * @param array $data |
||
204 | * @return array |
||
205 | * |
||
206 | */ |
||
207 | private function setDefaultPassword($data) |
||
213 | } |
||
214 |
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.