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 ClientTest extends \PHPUnit_Framework_TestCase |
||
15 | { |
||
16 | /** |
||
17 | * Set up each test. |
||
18 | * |
||
19 | * @return void |
||
20 | */ |
||
21 | public function setUp() |
||
25 | |||
26 | /** |
||
27 | * Verify basic behavior of search(). |
||
28 | * |
||
29 | * @test |
||
30 | * @covers ::__construct |
||
31 | * @covers ::search |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | View Code Duplication | public function search() |
|
55 | |||
56 | /** |
||
57 | * Verify proper exceptions thrown when Client is constructed with bad data. |
||
58 | * |
||
59 | * @param string $privateApiKey The private api key issued by Marvel. |
||
60 | * @param string $publicApiKey The public api key issued by Marvel. |
||
61 | * @param AdapterInterface $adapter Implementation of a client adapter. |
||
62 | * |
||
63 | * @test |
||
64 | * @covers ::__construct |
||
65 | * @dataProvider badConstructorData |
||
66 | * @expectedException \InvalidArgumentException |
||
67 | * |
||
68 | * @return void |
||
69 | */ |
||
70 | public function constructWithBadData($privateApiKey, $publicApiKey, AdapterInterface $adapter) |
||
74 | |||
75 | /** |
||
76 | * Data adapter for constructWithBadData test. |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | public function badConstructorData() |
||
95 | |||
96 | /** |
||
97 | * Verify proper exceptions thrown when Client is constructed with bad data. |
||
98 | * |
||
99 | * @param string $resource The API resource to search for. |
||
100 | * @param array $filters Array of search criteria to use in request. |
||
101 | * |
||
102 | * @test |
||
103 | * @covers ::search |
||
104 | * @dataProvider badSearchData |
||
105 | * @expectedException \InvalidArgumentException |
||
106 | * |
||
107 | * @return void |
||
108 | */ |
||
109 | public function searchtWithBadData($resource, array $filters) |
||
113 | |||
114 | /** |
||
115 | * Data adapter for searchWithBadData test. |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | View Code Duplication | public function badSearchData() |
|
129 | |||
130 | /** |
||
131 | * Verify basic behavior of get(). |
||
132 | * |
||
133 | * @test |
||
134 | * @covers ::__construct |
||
135 | * @covers ::get |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | View Code Duplication | public function get() |
|
159 | |||
160 | /** |
||
161 | * Verify proper exceptions thrown when Client is constructed with bad data. |
||
162 | * |
||
163 | * @param string $resource The API resource to search for. |
||
164 | * @param integer $id The id of the API resource. |
||
165 | * |
||
166 | * @test |
||
167 | * @covers ::get |
||
168 | * @dataProvider badGetData |
||
169 | * @expectedException \InvalidArgumentException |
||
170 | * |
||
171 | * @return void |
||
172 | */ |
||
173 | public function gettWithBadData($resource, $id) |
||
177 | |||
178 | /** |
||
179 | * Data adapter for getWithBadData test. |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | public function badGetData() |
||
196 | |||
197 | /** |
||
198 | * Verfiy response is return from cache. |
||
199 | * |
||
200 | * @test |
||
201 | * @covers ::get |
||
202 | * |
||
203 | * @return void |
||
204 | */ |
||
205 | public function getFromCache() |
||
231 | |||
232 | /** |
||
233 | * Verfiy response is return from cache. |
||
234 | * |
||
235 | * @test |
||
236 | * @covers ::get |
||
237 | * |
||
238 | * @return void |
||
239 | */ |
||
240 | public function getSetsCache() |
||
263 | |||
264 | /** |
||
265 | * Verify bahvior of __call() for single entity. |
||
266 | * |
||
267 | * @test |
||
268 | * @covers ::__call |
||
269 | * |
||
270 | * @return void |
||
271 | */ |
||
272 | public function callEntity() |
||
279 | |||
280 | /** |
||
281 | * Verify bahvior of __call() for entity that is not found. |
||
282 | * |
||
283 | * @test |
||
284 | * @covers ::__call |
||
285 | * |
||
286 | * @return void |
||
287 | */ |
||
288 | public function callEntityNotFound() |
||
294 | |||
295 | /** |
||
296 | * Verify bahvior of __call() for entity that is invalid. |
||
297 | * |
||
298 | * @test |
||
299 | * @covers ::__call |
||
300 | * |
||
301 | * @return void |
||
302 | */ |
||
303 | public function callInvalidEntity() |
||
309 | |||
310 | /** |
||
311 | * Verify basic bahvior of __call() for entity collection. |
||
312 | * |
||
313 | * @test |
||
314 | * @covers ::__call |
||
315 | * |
||
316 | * @return void |
||
317 | */ |
||
318 | public function callCollection() |
||
328 | } |
||
329 |
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.