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 |
||
18 | final class ConfigProviderTest extends TestCase |
||
19 | { |
||
20 | private $configBuilder; |
||
21 | |||
22 | protected function setUp(): void |
||
26 | |||
27 | View Code Duplication | public function testGet(): void |
|
|
|||
28 | { |
||
29 | $menu = $this->prophesize(ItemInterface::class); |
||
30 | |||
31 | $this->configBuilder->buildMenu(['name' => 'foo'], ['a' => 'b'])->willReturn($menu); |
||
32 | |||
33 | $configProvider = new ConfigProvider($this->configBuilder->reveal(), [ |
||
34 | 'foo' => ['name' => 'foo'], |
||
35 | 'bar' => ['name' => 'bar'], |
||
36 | ]); |
||
37 | static::assertSame($menu->reveal(), $configProvider->get('foo', ['a' => 'b'])); |
||
38 | } |
||
39 | |||
40 | View Code Duplication | public function testGetDoesNotExist(): void |
|
50 | |||
51 | View Code Duplication | public function testHas(): void |
|
60 | |||
61 | View Code Duplication | public function testHasNot(): void |
|
70 | } |
||
71 |
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.