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 |
||
| 16 | class RequiredHashControllerTest extends RestTestCase |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * load fixtures |
||
| 20 | * |
||
| 21 | * @return void |
||
| 22 | */ |
||
| 23 | View Code Duplication | public function setUp() |
|
|
|
|||
| 24 | { |
||
| 25 | if (!class_exists('GravitonDyn\TestCaseRequiredHashBundle\DataFixtures\MongoDB\LoadTestCaseRequiredHashData')) { |
||
| 26 | $this->markTestSkipped('TestCaseRequiredHashData definition is not loaded'); |
||
| 27 | } |
||
| 28 | |||
| 29 | $this->loadFixtures( |
||
| 30 | ['GravitonDyn\TestCaseRequiredHashBundle\DataFixtures\MongoDB\LoadTestCaseRequiredHashData'], |
||
| 31 | null, |
||
| 32 | 'doctrine_mongodb' |
||
| 33 | ); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Test POST method with optional hash |
||
| 38 | * |
||
| 39 | * @return void |
||
| 40 | */ |
||
| 41 | public function testPostWithOptionalHash() |
||
| 42 | { |
||
| 43 | $data = [ |
||
| 44 | 'name' => __METHOD__, |
||
| 45 | 'optionalHash' => [ |
||
| 46 | 'name' => 'abc', |
||
| 47 | 'value' => 123, |
||
| 48 | 'optional' => '2015-09-03T12:00:00+0000', |
||
| 49 | |||
| 50 | 'optionalSubHash' => [ |
||
| 51 | 'name' => 'abc', |
||
| 52 | 'value' => 123, |
||
| 53 | 'optional' => '2015-09-03T12:00:00+0000', |
||
| 54 | ], |
||
| 55 | 'requiredSubHash' => [ |
||
| 56 | 'name' => 'abc', |
||
| 57 | 'value' => 123, |
||
| 58 | 'optional' => '2015-09-03T12:00:00+0000', |
||
| 59 | ], |
||
| 60 | ], |
||
| 61 | 'requiredHash' => [ |
||
| 62 | 'name' => 'abc', |
||
| 63 | 'value' => 123, |
||
| 64 | 'optional' => '2015-09-03T12:00:00+0000', |
||
| 65 | |||
| 66 | 'optionalSubHash' => [ |
||
| 67 | 'name' => 'abc', |
||
| 68 | 'value' => 123, |
||
| 69 | 'optional' => '2015-09-03T12:00:00+0000', |
||
| 70 | ], |
||
| 71 | 'requiredSubHash' => [ |
||
| 72 | 'name' => 'abc', |
||
| 73 | 'value' => 123, |
||
| 74 | 'optional' => '2015-09-03T12:00:00+0000', |
||
| 75 | ], |
||
| 76 | ], |
||
| 77 | ]; |
||
| 78 | |||
| 79 | $client = static::createRestClient(); |
||
| 80 | $client->post('/testcase/requiredhash/', $data); |
||
| 81 | $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode()); |
||
| 82 | $this->assertEmpty($client->getResults()); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Test POST method without optional hash |
||
| 87 | * |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | public function testPostWithoutOptionalHash() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Test POST method without field in optional hash |
||
| 115 | * |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | public function testPostWithoutFieldInOptionalHash() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Test POST method without required hash |
||
| 169 | * |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | public function testPostWithoutRequiredHash() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Test POST method with empty optional hash |
||
| 206 | * |
||
| 207 | * @return void |
||
| 208 | */ |
||
| 209 | public function testPostWithEmptyOptionalHash() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Test POST method with empty required hash |
||
| 261 | * |
||
| 262 | * @return void |
||
| 263 | */ |
||
| 264 | public function testPostWithEmptyRequiredHash() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * check that schema does not contain realId artefacts |
||
| 303 | * |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | View Code Duplication | public function testCollectionHasNoRealId() |
|
| 316 | } |
||
| 317 |
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.