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 |
||
22 | class LinuxPlatformTest extends TestCase { |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $oldPath; |
||
27 | |||
28 | /** |
||
29 | * @var LinuxPlatform |
||
30 | */ |
||
31 | protected $platform; |
||
32 | |||
33 | /** |
||
34 | * @inheritdoc TestCase |
||
35 | */ |
||
36 | View Code Duplication | public function setUp() { |
|
|
|||
37 | $this->oldPath = getenv('PATH'); |
||
38 | putenv("PATH=/bin:{$this->oldPath}"); |
||
39 | |||
40 | $filesystem = $this->createMock(Filesystem::class); |
||
41 | $this->platform = new LinuxPlatform($filesystem); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @inheritdoc TestCase |
||
46 | */ |
||
47 | public function tearDown() { |
||
50 | |||
51 | /** |
||
52 | * @covers ::expandPath |
||
53 | * @covers \ComponentManager\Platform\AbstractPlatform::__construct |
||
54 | * @covers \ComponentManager\Platform\AbstractPlatform::getDirectorySeparator |
||
55 | * @covers \ComponentManager\Platform\LinuxPlatform::getHomeDirectory |
||
56 | */ |
||
57 | public function testExpandPath() { |
||
63 | |||
64 | /** |
||
65 | * @covers ::getExecutablePath |
||
66 | * @covers \ComponentManager\Platform\AbstractPlatform::__construct |
||
67 | * @covers \ComponentManager\Platform\AbstractPlatform::getDirectorySeparator |
||
68 | * @covers \ComponentManager\Platform\AbstractPlatform::joinPaths |
||
69 | */ |
||
70 | public function testGetExecutablePath() { |
||
74 | |||
75 | /** |
||
76 | * @covers ::getExecutablePath |
||
77 | * @covers \ComponentManager\Platform\AbstractPlatform::__construct |
||
78 | * @covers \ComponentManager\Platform\AbstractPlatform::getDirectorySeparator |
||
79 | * @covers \ComponentManager\Platform\AbstractPlatform::joinPaths |
||
80 | * @expectedException \ComponentManager\Exception\PlatformException |
||
81 | * @expectedExceptionCode 2 |
||
82 | */ |
||
83 | public function testGetExecutablePathThrows() { |
||
86 | |||
87 | /** |
||
88 | * @covers ::getHomeDirectory |
||
89 | * @covers \ComponentManager\Platform\AbstractPlatform::__construct |
||
90 | */ |
||
91 | public function testGetHomeDirectory() { |
||
94 | |||
95 | /** |
||
96 | * @covers ::getHomeDirectory |
||
97 | * @covers ::getLocalSharedDirectory |
||
98 | * @covers \ComponentManager\Platform\AbstractPlatform::__construct |
||
99 | * @covers \ComponentManager\Platform\AbstractPlatform::getDirectorySeparator |
||
100 | * @covers \ComponentManager\Platform\AbstractPlatform::joinPaths |
||
101 | */ |
||
102 | public function testGetLocalSharedDirectory() { |
||
105 | } |
||
106 |
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.