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 |
||
19 | View Code Duplication | class GenerateCest extends BaseCommand |
|
|
|||
20 | { |
||
21 | |||
22 | /** |
||
23 | * The console command name. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $name = 'make-cest'; |
||
28 | |||
29 | /** |
||
30 | * The console command description. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $description = 'Generate a Codeception Cest for the given module and suite'; |
||
35 | |||
36 | /** |
||
37 | * Execute the console command. |
||
38 | * |
||
39 | * @return void |
||
40 | * |
||
41 | * @throws Exception |
||
42 | */ |
||
43 | public function handle() |
||
44 | { |
||
45 | /** |
||
46 | * @var string $moduleName |
||
47 | * @var string $suite |
||
48 | * @var string $name |
||
49 | */ |
||
50 | $moduleName = $this->argument('moduleName'); |
||
51 | $suite = $this->argument('suite'); |
||
52 | $name = $this->argument('name'); |
||
53 | |||
54 | $this->info("Creating '{$name}' for module '{$moduleName}' in suite '{$suite}'."); |
||
55 | |||
56 | $setup = new Setup($moduleName); |
||
57 | |||
58 | try { |
||
59 | $generator = new CestGenerator($setup, $suite, $name); |
||
60 | $generator->generate(); |
||
61 | $this->info("Created test!"); |
||
62 | } catch (Exception $e) { |
||
63 | $this->error("Failed to create test!"); |
||
64 | throw $e; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return CommandArguments |
||
70 | */ |
||
71 | protected function provideArguments() : CommandArguments |
||
79 | |||
80 | } |
||
81 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.