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 |
||
14 | final class FileTest extends TestCase |
||
15 | { |
||
16 | private $topLevelDirPath; |
||
17 | private $topLevelFilePath; |
||
18 | private $subLevelDirPath; |
||
19 | private $subLevelFilePath; |
||
20 | |||
21 | private $oldErrorReporting; |
||
22 | |||
23 | public function setup() |
||
36 | |||
37 | //this is just for convenience, DO NOT RELY ON IT |
||
38 | public function tearDown() |
||
44 | |||
45 | private function deleteTestFiles() |
||
65 | |||
66 | /** |
||
67 | * @test |
||
68 | * @covers ::deleteDirectoryContents |
||
69 | * @expectedException \InvalidArgumentException |
||
70 | * @expectedExceptionMessage $directoryPath is not a string |
||
71 | */ |
||
72 | public function deleteDirectoryContentsNonStringPath() |
||
76 | |||
77 | /** |
||
78 | * @test |
||
79 | * @covers ::deleteDirectoryContents |
||
80 | * @expectedException \Exception |
||
81 | * @expectedExceptionMessage cannot list directory '/some/where/that/doesnt/exist' |
||
82 | */ |
||
83 | public function deleteDirectoryContentsNonExistentPath() |
||
88 | |||
89 | /** |
||
90 | * @test |
||
91 | * @covers ::deleteDirectoryContents |
||
92 | */ |
||
93 | public function deleteDirectoryContentsEmpty() |
||
101 | |||
102 | /** |
||
103 | * @test |
||
104 | * @covers ::deleteDirectoryContents |
||
105 | */ |
||
106 | public function deleteDirectoryContentsWithFiles() |
||
117 | |||
118 | /** |
||
119 | * @test |
||
120 | * @covers ::deleteDirectoryContents |
||
121 | * @expectedException \Exception |
||
122 | * @expectedExceptionCode 2 |
||
123 | */ |
||
124 | View Code Duplication | public function deleteDirectoryContentsWithProtectedFile() |
|
135 | |||
136 | /** |
||
137 | * @test |
||
138 | * @covers ::deleteDirectoryContents |
||
139 | * @expectedException \Exception |
||
140 | * @expectedExceptionCode 1 |
||
141 | */ |
||
142 | public function deleteDirectoryContentsWithProtectedDirectory() |
||
151 | |||
152 | /** |
||
153 | * @test |
||
154 | * @covers ::delete |
||
155 | */ |
||
156 | View Code Duplication | public function deleteBasic() |
|
163 | |||
164 | /** |
||
165 | * @test |
||
166 | * @covers ::delete |
||
167 | */ |
||
168 | public function deleteNonExistent() |
||
173 | |||
174 | /** |
||
175 | * @test |
||
176 | * @covers ::delete |
||
177 | * @expectedException \Exception |
||
178 | */ |
||
179 | public function deleteDirectory() |
||
185 | |||
186 | /** |
||
187 | * @test |
||
188 | * @covers ::delete |
||
189 | * @expectedException \InvalidArgumentException |
||
190 | * @expectedExceptionMessage $path is not a string or is whitespace |
||
191 | */ |
||
192 | public function deleteNonStringPath() |
||
196 | |||
197 | /** |
||
198 | * @test |
||
199 | * @covers ::delete |
||
200 | * @expectedException \InvalidArgumentException |
||
201 | * @expectedExceptionMessage $path is not a string or is whitespace |
||
202 | */ |
||
203 | public function deletePathIsWhitespace() |
||
207 | |||
208 | /** |
||
209 | * Verify behavior of delete() with protected file. |
||
210 | * |
||
211 | * @test |
||
212 | * @covers ::delete |
||
213 | * @expectedException \Exception |
||
214 | */ |
||
215 | View Code Duplication | public function deleteProtectedFile() |
|
226 | |||
227 | /** |
||
228 | * verify basic behavior of deletePathIfEmpty(). |
||
229 | * |
||
230 | * @test |
||
231 | * @covers ::deletePathIfEmpty |
||
232 | * |
||
233 | * @return void |
||
234 | */ |
||
235 | public function deletePathIfEmpty() |
||
243 | |||
244 | /** |
||
245 | * verify behavior of deletePathIfEmpty() when $deletePath does not exist. |
||
246 | * |
||
247 | * @test |
||
248 | * @covers ::deletePathIfEmpty |
||
249 | * |
||
250 | * @return void |
||
251 | */ |
||
252 | public function deletePathIfEmptyNotExists() |
||
258 | |||
259 | /** |
||
260 | * verify behavior of deletePathIfEmpty() when a folder in $deletePath contains a file. |
||
261 | * |
||
262 | * @test |
||
263 | * @covers ::deletePathIfEmpty |
||
264 | * |
||
265 | * @return void |
||
266 | */ |
||
267 | public function deletePathIfEmptyNotEmpty() |
||
284 | } |
||
285 |
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.