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 | public function testParamHandlingWithIntOnStringField($groupId, $numRecords, $idList) |
||
| 234 | { |
||
| 235 | $client = static::createRestClient(); |
||
| 236 | |||
| 237 | $url = '/analytics/customer-with-int-param-string-field'; |
||
| 238 | if (!is_null($groupId)) { |
||
| 239 | $url .= '?groupId='.$groupId; |
||
| 240 | } |
||
| 241 | |||
| 242 | $client->request('GET', $url); |
||
| 243 | |||
| 244 | $this->assertEquals($numRecords, count($client->getResults())); |
||
| 245 | |||
| 246 | foreach ($client->getResults() as $result) { |
||
| 247 | $this->assertContains($result->{'_id'}, $idList); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * data provider |
||
| 253 | * |
||
| 254 | * @return array data |
||
| 255 | */ |
||
| 256 | public function paramHandlingWithIntOnStringFieldDataProvider() |
||
| 257 | { |
||
| 258 | return [ |
||
| 259 | [ |
||
| 260 | 100, |
||
| 261 | 3, |
||
| 262 | ['100', '101', '102'] |
||
| 263 | ], |
||
| 264 | [ |
||
| 265 | null, // testing default value of 100 as defined in params! |
||
| 266 | 3, |
||
| 267 | ['100', '101', '102'] |
||
| 268 | ], |
||
| 269 | [ |
||
| 270 | 200, |
||
| 271 | 3, |
||
| 272 | ['100', '101', '103'] |
||
| 273 | ] |
||
| 274 | ]; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * see if array properties are properly handled |
||
| 279 | * |
||
| 280 | * @dataProvider paramHandlingArrayWithIntOnStringFieldDataProvider |
||
| 281 | * |
||
| 282 | * @param string $groupId group id |
||
| 283 | * @param array $idList list of ids |
||
| 284 | * |
||
| 285 | * @return void |
||
| 286 | */ |
||
| 287 | public function testParamArrayHandlingWithIntOnStringField($groupId, $idList) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * data provider |
||
| 304 | * |
||
| 305 | * @return array data |
||
| 306 | */ |
||
| 307 | public function paramHandlingArrayWithIntOnStringFieldDataProvider() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * test to see if the params are mentioned in the schema |
||
| 327 | * |
||
| 328 | * @return void |
||
| 329 | */ |
||
| 330 | public function testParamsInSchema() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * tests conversion of MongoDates in output as well as replacement of Date instances (and maybe others) |
||
| 342 | * in the pipeline script |
||
| 343 | * |
||
| 344 | * @return void |
||
| 345 | */ |
||
| 346 | public function testDateHandlingInOutput() |
||
| 394 | } |
||
| 395 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.