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 |
||
26 | class SetupGenerator implements SetupGeneratorInterface |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * @var SetupInterface |
||
31 | */ |
||
32 | protected $setup; |
||
33 | |||
34 | /** |
||
35 | * @var Filesystem |
||
36 | */ |
||
37 | protected $disk; |
||
38 | |||
39 | /** |
||
40 | * @var GlobalConfigurationInterface |
||
41 | */ |
||
42 | protected $globalConfiguration; |
||
43 | |||
44 | /** |
||
45 | * Generator constructor. |
||
46 | * |
||
47 | * @param SetupInterface $setup |
||
48 | */ |
||
49 | public function __construct(SetupInterface $setup) |
||
54 | |||
55 | /** |
||
56 | * @param string $name |
||
57 | * |
||
58 | * @return mixed |
||
59 | */ |
||
60 | public function __get($name) |
||
68 | |||
69 | /** |
||
70 | * @param string[] $suites |
||
71 | * |
||
72 | * @return void |
||
73 | */ |
||
74 | public function generate($suites = []) |
||
105 | |||
106 | /** |
||
107 | * @param string $suite |
||
108 | */ |
||
109 | public function createSuiteAndBuild(string $suite) |
||
114 | |||
115 | /** |
||
116 | * @param SuiteInfoInterface $suiteInfo |
||
117 | * |
||
118 | * @return void |
||
119 | */ |
||
120 | protected function generateSuite(SuiteInfoInterface $suiteInfo) |
||
127 | |||
128 | /** |
||
129 | * Build Actor classes |
||
130 | * |
||
131 | * @return void |
||
132 | */ |
||
133 | protected function build() |
||
137 | |||
138 | /** |
||
139 | * @param string $command |
||
140 | */ |
||
141 | protected function executeCodeceptionCommand(string $command) |
||
158 | |||
159 | /** |
||
160 | * @param string $command |
||
161 | * @param string[] ...$arguments |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | protected function parseCommand(string $command, string ...$arguments) : string |
||
169 | |||
170 | protected function createGlobalConfiguration() |
||
176 | |||
177 | protected function createTestsDir() |
||
181 | |||
182 | protected function createSupportingDirs() |
||
188 | |||
189 | /** |
||
190 | * @param string[] $suites |
||
191 | */ |
||
192 | protected function createSuites($suites) |
||
203 | |||
204 | /** |
||
205 | * @param string $path |
||
206 | * @param string|resource $contents |
||
207 | * @param bool $inBasePath |
||
208 | * |
||
209 | * @return bool |
||
210 | */ |
||
211 | View Code Duplication | protected function createFile(string $path, $contents, bool $inBasePath = false) : bool |
|
221 | |||
222 | /** |
||
223 | * @param string $path |
||
224 | * @param bool $inBasePath |
||
225 | * |
||
226 | * @return bool |
||
227 | */ |
||
228 | View Code Duplication | protected function createDir(string $path, bool $inBasePath = false) : bool |
|
238 | |||
239 | /** |
||
240 | * @param string $path |
||
241 | * @param bool $inBasePath |
||
242 | * |
||
243 | * @return string |
||
244 | */ |
||
245 | protected function getPath(string $path, bool $inBasePath = false) : string |
||
253 | |||
254 | /** |
||
255 | * @param string $path |
||
256 | * @param bool $inBasePath |
||
257 | * |
||
258 | * @return bool |
||
259 | */ |
||
260 | protected function createEmptyDir(string $path, bool $inBasePath = false) : bool |
||
268 | |||
269 | /** |
||
270 | * @param string $dir |
||
271 | * @param bool $inBasePath |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | protected function createGitKeep(string $dir, bool $inBasePath = false) : bool |
||
279 | |||
280 | } |
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.