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:
Complex classes like ResourceCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ResourceCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class ResourceCommand extends Command |
||
17 | { |
||
18 | use MicroboardPathResolver; |
||
19 | |||
20 | /** |
||
21 | * The console command name. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $name = 'microboard:resource'; |
||
26 | |||
27 | /** |
||
28 | * The console command description. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $description = 'Create an admin resource for given model'; |
||
33 | |||
34 | /** |
||
35 | * @var Factory |
||
36 | */ |
||
37 | protected $microboard; |
||
38 | |||
39 | /** |
||
40 | * @var Filesystem |
||
41 | */ |
||
42 | protected $files; |
||
43 | |||
44 | /** |
||
45 | * ResourceCommand constructor. |
||
46 | * |
||
47 | * @param Factory $microboard |
||
48 | * @param Filesystem $files |
||
49 | */ |
||
50 | public function __construct(Factory $microboard, Filesystem $files) |
||
57 | |||
58 | /** |
||
59 | * Execute the console command. |
||
60 | * |
||
61 | * @throws Exception |
||
62 | */ |
||
63 | public function handle() |
||
145 | |||
146 | /** |
||
147 | * @param $modelName |
||
148 | * @return \Illuminate\Contracts\Foundation\Application|mixed |
||
149 | */ |
||
150 | protected function getNamespacedModel($modelName) |
||
162 | |||
163 | /** |
||
164 | * @return array |
||
165 | */ |
||
166 | protected function getAvailableOptions() |
||
172 | |||
173 | /** |
||
174 | * @return array |
||
175 | */ |
||
176 | protected function getAvailableAbilities() |
||
188 | |||
189 | /** |
||
190 | * |
||
191 | * |
||
192 | * @param string $model |
||
193 | * @param bool $filter |
||
194 | * @return array |
||
195 | */ |
||
196 | protected function getModelFillableColumns(string $model, $filter = false) |
||
206 | |||
207 | /** |
||
208 | * Create table.blade.php file |
||
209 | * |
||
210 | * @return $this |
||
211 | * @throws FileNotFoundException |
||
212 | */ |
||
213 | protected function makeTableBladeFile() |
||
219 | |||
220 | /** |
||
221 | * Create a new blade file with given name. |
||
222 | * |
||
223 | * @param $file |
||
224 | * @return $this |
||
225 | * @throws FileNotFoundException |
||
226 | */ |
||
227 | protected function makeBladeFile($file) |
||
239 | |||
240 | /** |
||
241 | * @param $view |
||
242 | * @param $name |
||
243 | * @return string |
||
244 | */ |
||
245 | protected function getViewsPath($view, $name): string |
||
253 | |||
254 | /** |
||
255 | * Build the directory for the class if necessary. |
||
256 | * |
||
257 | * @param string $path |
||
258 | * @return string |
||
259 | */ |
||
260 | protected function makeDirectory($path) |
||
268 | |||
269 | /** |
||
270 | * Replace views variables. |
||
271 | * |
||
272 | * @param $file |
||
273 | * @param string $stub |
||
274 | * @param $name |
||
275 | * @return string |
||
276 | * @throws FileNotFoundException |
||
277 | */ |
||
278 | protected function replaceVariablesForm($file, string $stub, $name) |
||
328 | |||
329 | /** |
||
330 | * Create form.blade.php |
||
331 | * |
||
332 | * @return $this |
||
333 | * @throws FileNotFoundException |
||
334 | */ |
||
335 | protected function makeFormBladeFile() |
||
341 | |||
342 | /** |
||
343 | * It makes lang files to this model |
||
344 | * |
||
345 | * @param $code |
||
346 | * @return $this |
||
347 | * @throws FileNotFoundException |
||
348 | */ |
||
349 | protected function makeLangFileTo($code) |
||
375 | |||
376 | /** |
||
377 | * @throws FileNotFoundException |
||
378 | */ |
||
379 | protected function updateRoutesAndNavLinks() |
||
417 | |||
418 | /** |
||
419 | * Get the console command arguments. |
||
420 | * |
||
421 | * @return array |
||
422 | */ |
||
423 | protected function getArguments() |
||
429 | |||
430 | /** |
||
431 | * Get the console command arguments. |
||
432 | * |
||
433 | * @return array |
||
434 | */ |
||
435 | protected function getOptions() |
||
446 | } |
||
447 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.