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 |
||
14 | trait MigrationCommandTrait |
||
15 | { |
||
16 | /** |
||
17 | * Errors check parameters |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $errors = []; |
||
22 | |||
23 | /** |
||
24 | * Information block sites |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $sites = []; |
||
29 | |||
30 | /** |
||
31 | * Information block type |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $type = ''; |
||
36 | |||
37 | /** |
||
38 | * Information blocks |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $iblocks = []; |
||
43 | |||
44 | /** |
||
45 | * Directory with file(s) |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $dir; |
||
50 | |||
51 | /** |
||
52 | * Extension file(s) |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $extension = '.xml'; |
||
57 | |||
58 | /** |
||
59 | * Check argument directory and set $this->dir |
||
60 | * |
||
61 | * @param InputInterface $input |
||
62 | */ |
||
63 | protected function setDir(InputInterface $input) |
||
79 | |||
80 | /** |
||
81 | * Check arguments type and code, set $this->iblocks |
||
82 | * |
||
83 | * @param InputInterface $input |
||
84 | */ |
||
85 | protected function setIblocks(InputInterface $input) |
||
101 | |||
102 | /** |
||
103 | * Check argument type and set $this->type |
||
104 | * |
||
105 | * @param InputInterface $input |
||
106 | */ |
||
107 | View Code Duplication | protected function setType(InputInterface $input) |
|
122 | |||
123 | /** |
||
124 | * Check argument sites and set $this->sites |
||
125 | * |
||
126 | * @param InputInterface $input |
||
127 | */ |
||
128 | View Code Duplication | protected function setSites(InputInterface $input) |
|
143 | } |
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.