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 |
||
33 | class ComposerProcessService implements ComposerServiceInterface |
||
34 | { |
||
35 | /** |
||
36 | * @Inject("config") |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $appConfig; |
||
40 | |||
41 | private $workingDir; |
||
42 | private $composerFile; |
||
43 | private $composerSetup; |
||
44 | |||
45 | /** |
||
46 | * This function to install a plugin by composer require |
||
47 | * |
||
48 | * @param string $packageName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0" |
||
49 | * @return bool |
||
50 | */ |
||
51 | View Code Duplication | public function execRequire($packageName) |
|
64 | |||
65 | /** |
||
66 | * This function to remove a plugin by composer remove |
||
67 | * Note: Remove with dependency, if not, please add " --no-update-with-dependencies" |
||
68 | * |
||
69 | * @param string $packageName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0" |
||
70 | * @return bool |
||
71 | */ |
||
72 | View Code Duplication | public function execRemove($packageName) |
|
87 | |||
88 | /** |
||
89 | * Run command |
||
90 | * |
||
91 | * @param string $command |
||
92 | * @return void |
||
93 | */ |
||
94 | public function runCommand($command) |
||
101 | |||
102 | /** |
||
103 | * Set working dir |
||
104 | * @param string $workingDir |
||
105 | */ |
||
106 | public function setWorkingDir($workingDir) |
||
110 | |||
111 | /** |
||
112 | * Get environment php command |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | private function getPHP() |
||
120 | |||
121 | /** |
||
122 | * Set init |
||
123 | */ |
||
124 | private function init() |
||
132 | |||
133 | /** |
||
134 | * Check composer file and setup it |
||
135 | */ |
||
136 | private function setupComposer() |
||
151 | } |
||
152 |
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.