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 declare(strict_types=1); |
||
16 | abstract class TestCase extends PHPUnitTestCase |
||
17 | { |
||
18 | const DEFAULT_AWAIT_TIMEOUT = 60; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $baseTmpDir; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $tmpDir; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $tmpNamespace; |
||
34 | |||
35 | 84 | protected function setUp(): void |
|
51 | |||
52 | 84 | protected function tearDown(): void |
|
60 | |||
61 | /** |
||
62 | * @return array |
||
63 | */ |
||
64 | public function provideTrueFalse(): array |
||
75 | |||
76 | /** |
||
77 | * @return string |
||
78 | */ |
||
79 | 84 | protected function getSysTempDir(): string |
|
87 | |||
88 | /** |
||
89 | * @param string $dir |
||
90 | */ |
||
91 | 68 | protected function rmdir(string $dir): void |
|
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | 68 | protected function getTmpDir(): string |
|
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | 67 | protected function getRandomNameSpace(): string |
|
129 | |||
130 | /** |
||
131 | * @param string $path |
||
132 | * @return array |
||
133 | */ |
||
134 | 67 | protected function getFilesInDirectory(string $path): array |
|
151 | |||
152 | /** |
||
153 | * @param PromiseInterface $promise |
||
154 | * @param LoopInterface|null $loop |
||
155 | * @return mixed |
||
156 | */ |
||
157 | 6 | protected function await(PromiseInterface $promise, LoopInterface $loop = null, float $timeout = self::DEFAULT_AWAIT_TIMEOUT) |
|
165 | |||
166 | /** |
||
167 | * @param array $promises |
||
168 | * @param LoopInterface|null $loop |
||
169 | * @return array |
||
170 | */ |
||
171 | 3 | View Code Duplication | protected function awaitAll(array $promises, LoopInterface $loop = null, float $timeout = self::DEFAULT_AWAIT_TIMEOUT) |
179 | |||
180 | /** |
||
181 | * @param array $promises |
||
182 | * @param LoopInterface|null $loop |
||
183 | * @return mixed |
||
184 | */ |
||
185 | 3 | View Code Duplication | protected function awaitAny(array $promises, LoopInterface $loop = null, float $timeout = self::DEFAULT_AWAIT_TIMEOUT) |
193 | } |
||
194 |
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.