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 |
||
13 | final class InvokableFiltererTest extends TestCase |
||
14 | { |
||
15 | /** |
||
16 | * @test |
||
17 | * @covers ::execute |
||
18 | */ |
||
19 | View Code Duplication | public function execute() |
|
30 | |||
31 | /** |
||
32 | * @test |
||
33 | * @covers ::getAliases |
||
34 | */ |
||
35 | View Code Duplication | public function getAliases() |
|
46 | |||
47 | /** |
||
48 | * @test |
||
49 | * @covers ::getSpecification |
||
50 | */ |
||
51 | View Code Duplication | public function getSpecification() |
|
62 | |||
63 | /** |
||
64 | * @test |
||
65 | * @covers ::withAliases |
||
66 | */ |
||
67 | View Code Duplication | public function withAliases() |
|
77 | |||
78 | /** |
||
79 | * @test |
||
80 | * @covers ::withSpecification |
||
81 | */ |
||
82 | View Code Duplication | public function withSpecification() |
|
92 | |||
93 | /** |
||
94 | * @test |
||
95 | * @covers ::__invoke |
||
96 | * @dataProvider provideInvoke |
||
97 | * |
||
98 | * @param array $filter |
||
99 | * @param array $options |
||
100 | * @param array|null $filterAliases |
||
101 | * @param mixed $value |
||
102 | * @param array $expected |
||
103 | */ |
||
104 | public function invoke(array $filter, array $options, $filterAliases, $value, array $expected) |
||
111 | |||
112 | /** |
||
113 | * @returns array |
||
114 | */ |
||
115 | public function provideInvoke() : array |
||
141 | |||
142 | /** |
||
143 | * @test |
||
144 | * @covers ::__invoke |
||
145 | */ |
||
146 | public function invokeThrowsFilterException() |
||
158 | |||
159 | /** |
||
160 | * @return FiltererInterface|\PHPUnit\Framework\MockObject\MockObject |
||
161 | */ |
||
162 | private function getMockFilterer() |
||
166 | } |
||
167 |
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.