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 |
||
11 | final class CollectionTest extends BaseUnitTestCase |
||
12 | { |
||
13 | private $objects; |
||
14 | private $collection; |
||
15 | |||
16 | protected function setUp() |
||
28 | |||
29 | protected function tearDown() |
||
35 | |||
36 | /** |
||
37 | * @test |
||
38 | */ |
||
39 | public function itShouldThrowInvalidCollectionObjectException(): void |
||
45 | |||
46 | /** |
||
47 | * @test |
||
48 | */ |
||
49 | public function itShouldReturnCollection(): void |
||
53 | |||
54 | /** |
||
55 | * @test |
||
56 | */ |
||
57 | public function itShouldReturnEmptyCollection(): void |
||
63 | |||
64 | /** |
||
65 | * @test |
||
66 | */ |
||
67 | public function itShouldReturnFirst(): void |
||
73 | |||
74 | /** |
||
75 | * @test |
||
76 | */ |
||
77 | public function itShouldReturnLast(): void |
||
83 | |||
84 | /** |
||
85 | * @test |
||
86 | */ |
||
87 | public function itShouldReturnKey(): void |
||
93 | |||
94 | /** |
||
95 | * @test |
||
96 | */ |
||
97 | View Code Duplication | public function itShouldReturnNext(): void |
|
104 | |||
105 | /** |
||
106 | * @test |
||
107 | */ |
||
108 | View Code Duplication | public function itShouldReturnCurrent(): void |
|
115 | |||
116 | /** |
||
117 | * @test |
||
118 | */ |
||
119 | public function itShouldRemoveKey(): void |
||
124 | |||
125 | /** |
||
126 | * @test |
||
127 | */ |
||
128 | public function itShouldRemoveElement(): void |
||
134 | |||
135 | /** |
||
136 | * @test |
||
137 | */ |
||
138 | public function itShouldReturnContains(): void |
||
143 | |||
144 | /** |
||
145 | * @test |
||
146 | */ |
||
147 | public function itShouldReturnElementByKey(): void |
||
152 | |||
153 | /** |
||
154 | * @test |
||
155 | */ |
||
156 | public function itShouldReturnKeys(): void |
||
161 | |||
162 | /** |
||
163 | * @test |
||
164 | */ |
||
165 | public function itShouldReturnValues(): void |
||
170 | |||
171 | /** |
||
172 | * @test |
||
173 | */ |
||
174 | public function itShouldReturnCount(): void |
||
178 | |||
179 | /** |
||
180 | * @test |
||
181 | */ |
||
182 | public function itShouldSetObject(): void |
||
188 | |||
189 | /** |
||
190 | * @test |
||
191 | */ |
||
192 | public function itShouldAddObject(): void |
||
198 | |||
199 | /** |
||
200 | * @test |
||
201 | */ |
||
202 | public function itShouldClearObjects(): void |
||
208 | } |
||
209 |
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.