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 | 12 | public function handle() |
|
123 | |||
124 | /** |
||
125 | * Determine if the given email address already exists. |
||
126 | * |
||
127 | * @param string $email |
||
128 | * @return void |
||
129 | * |
||
130 | * @throws \Dyrynda\Artisan\Exceptions\MakeUserException |
||
131 | */ |
||
132 | 9 | private function validateEmail($email) |
|
142 | |||
143 | /** |
||
144 | * Return any additional database fields passed by the --fields option. |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | 7 | private function additionalFields() |
|
160 | |||
161 | /** |
||
162 | * Normalise the given (database) field input value. |
||
163 | * |
||
164 | * @param mixed $value |
||
165 | * @return mixed |
||
166 | */ |
||
167 | 3 | private function normaliseValue($value) |
|
183 | |||
184 | /** |
||
185 | * Create file handler objects. |
||
186 | * |
||
187 | * @param string $path |
||
188 | * @return BulkImportFileHandler |
||
189 | * |
||
190 | * @throws \Dyrynda\Artisan\Exceptions\ImportFileException |
||
191 | */ |
||
192 | 3 | private function fileHandlerFactory($path) : BulkImportFileHandler |
|
206 | |||
207 | /** |
||
208 | * Add default password to data. |
||
209 | * |
||
210 | * @param array $data |
||
211 | * @return array |
||
212 | * |
||
213 | */ |
||
214 | private function setPasswords($data) |
||
223 | } |
||
224 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: