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 |
||
17 | class DefaultControllerTest extends RestTestCase |
||
18 | { |
||
19 | /** |
||
20 | * Initial setup |
||
21 | * @return void |
||
22 | */ |
||
23 | public function setUp() |
||
34 | |||
35 | /** |
||
36 | * test options request |
||
37 | * @return void |
||
38 | */ |
||
39 | View Code Duplication | public function testOptions() |
|
47 | |||
48 | /** |
||
49 | * Testing basic functionality |
||
50 | * @return void |
||
51 | */ |
||
52 | View Code Duplication | public function testIndex() |
|
53 | { |
||
54 | $client = static::createClient(); |
||
55 | |||
56 | // Let's get information from the schema |
||
57 | $client->request('GET', '/analytics/schema/app'); |
||
58 | $content = $client->getResponse()->getContent(); |
||
59 | $schema = json_decode($content); |
||
60 | |||
61 | // Check schema |
||
62 | $sampleSchema = json_decode( |
||
63 | '{ |
||
64 | "title": "Application usage", |
||
65 | "description": "Data use for application access", |
||
66 | "type": "object", |
||
67 | "properties": { |
||
68 | "id": { |
||
69 | "title": "ID", |
||
70 | "description": "Unique identifier", |
||
71 | "type": "string" |
||
72 | }, |
||
73 | "count": { |
||
74 | "title": "count", |
||
75 | "description": "Sum of result", |
||
76 | "type": "integer" |
||
77 | } |
||
78 | }, |
||
79 | "x-params": [] |
||
80 | }' |
||
81 | ); |
||
82 | $this->assertEquals($sampleSchema, $schema); |
||
83 | |||
84 | // Let's get information from the count |
||
85 | $client->request('GET', '/analytics/app'); |
||
86 | $content = $client->getResponse()->getContent(); |
||
87 | $data = json_decode($content); |
||
88 | |||
89 | // Counter data result of aggregate |
||
90 | $sampleData = json_decode('{"_id":"app-count","count":2}'); |
||
91 | $this->assertEquals($sampleData, $data); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Testing basic functionality |
||
96 | * @return void |
||
97 | */ |
||
98 | public function testApp2Index() |
||
111 | |||
112 | /** |
||
113 | * Testing basic functionality |
||
114 | * @return void |
||
115 | */ |
||
116 | View Code Duplication | public function testCustomerCreateDateFilteringIndex() |
|
158 | |||
159 | /** |
||
160 | * test to see if required params are required |
||
161 | * |
||
162 | * @return void |
||
163 | */ |
||
164 | public function testMissingParamExceptions() |
||
171 | |||
172 | /** |
||
173 | * test to see application of param |
||
174 | * |
||
175 | * @dataProvider paramHandlingWithIntDataProvider |
||
176 | * |
||
177 | * @param int $yearFrom year from |
||
178 | * @param int $yearTo year to |
||
179 | * @param int $numRecords number of records |
||
180 | * |
||
181 | * @return void |
||
182 | */ |
||
183 | public function testParamHandlingWithInt($yearFrom, $yearTo, $numRecords) |
||
190 | |||
191 | /** |
||
192 | * data provider |
||
193 | * |
||
194 | * @return array data |
||
|
|||
195 | */ |
||
196 | View Code Duplication | public function paramHandlingWithIntDataProvider() |
|
221 | |||
222 | /** |
||
223 | * test to see application of param with int on string field, if the correct records are returned |
||
224 | * |
||
225 | * @dataProvider paramHandlingWithIntOnStringFieldDataProvider |
||
226 | * |
||
227 | * @param string $groupId group id |
||
228 | * @param int $numRecords number of records |
||
229 | * @param array $idList list of ids |
||
230 | * |
||
231 | * @return void |
||
232 | */ |
||
233 | View Code Duplication | public function testParamHandlingWithIntOnStringField($groupId, $numRecords, $idList) |
|
234 | { |
||
235 | $client = static::createRestClient(); |
||
236 | $client->request('GET', '/analytics/customer-with-int-param-string-field?groupId='.$groupId); |
||
237 | |||
238 | $this->assertEquals($numRecords, count($client->getResults())); |
||
239 | |||
240 | foreach ($client->getResults() as $result) { |
||
241 | $this->assertContains($result->{'_id'}, $idList); |
||
242 | } |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * data provider |
||
247 | * |
||
248 | * @return array data |
||
249 | */ |
||
250 | public function paramHandlingWithIntOnStringFieldDataProvider() |
||
265 | |||
266 | /** |
||
267 | * see if array properties are properly handled |
||
268 | * |
||
269 | * @dataProvider paramHandlingArrayWithIntOnStringFieldDataProvider |
||
270 | * |
||
271 | * @param string $groupId group id |
||
272 | * @param array $idList list of ids |
||
273 | * |
||
274 | * @return void |
||
275 | */ |
||
276 | View Code Duplication | public function testParamArrayHandlingWithIntOnStringField($groupId, $idList) |
|
290 | |||
291 | /** |
||
292 | * data provider |
||
293 | * |
||
294 | * @return array data |
||
295 | */ |
||
296 | public function paramHandlingArrayWithIntOnStringFieldDataProvider() |
||
313 | |||
314 | /** |
||
315 | * test to see if the params are mentioned in the schema |
||
316 | * |
||
317 | * @return void |
||
318 | */ |
||
319 | public function testParamsInSchema() |
||
328 | } |
||
329 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.