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 RecordOriginConstraintTest extends RestTestCase |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * load fixtures |
||
| 21 | * |
||
| 22 | * @return void |
||
| 23 | */ |
||
| 24 | public function setUp() |
||
| 34 | |||
| 35 | /** |
||
| 36 | * test the validation of the RecordOriginConstraint |
||
| 37 | * |
||
| 38 | * @dataProvider createDataProvider |
||
| 39 | * |
||
| 40 | * @param object $entity The object to create |
||
| 41 | * @param integer $expectedStatus Header status code |
||
| 42 | * @param string $expectedResponse Post data result of post |
||
| 43 | * |
||
| 44 | * @return void |
||
| 45 | */ |
||
| 46 | View Code Duplication | public function testRecordOriginHandlingOnCreate($entity, $expectedStatus, $expectedResponse) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * tests for the case if user doesn't provide an id in payload.. constraint |
||
| 58 | * must take the id from the request in that case. |
||
| 59 | * |
||
| 60 | * @return void |
||
| 61 | */ |
||
| 62 | public function testRecordOriginHandlingWithNoIdInPayload() |
||
| 63 | { |
||
| 64 | $record = (object) [ |
||
| 65 | //'id' => '' - no, no id.. that's the point ;-) |
||
| 66 | 'customerNumber' => 555, |
||
| 67 | 'name' => 'Muster Hans' |
||
| 68 | ]; |
||
| 69 | |||
| 70 | $client = static::createRestClient(); |
||
| 71 | $client->put('/person/customer/100', $record); |
||
| 72 | |||
| 73 | $this->assertEquals( |
||
| 74 | (object) [ |
||
| 75 | 'propertyPath' => 'recordOrigin', |
||
| 76 | 'message' => 'Prohibited modification attempt on record with recordOrigin of core.' |
||
| 77 | .' You tried to change (customerNumber, name, someObject), but You can only change' |
||
| 78 | .' by recordCoreException: (addedField, someObject.twoField).' |
||
| 79 | ], |
||
| 80 | $client->getResults()[0] |
||
| 81 | ); |
||
| 82 | |||
| 83 | $this->assertEquals(1, count($client->getResults())); |
||
| 84 | $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode()); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Test the validation of the RecordOriginConstraint |
||
| 89 | * |
||
| 90 | * @param array $fieldsToSet Fields to be modified |
||
| 91 | * @param integer $expectedStatus Header status code |
||
| 92 | * @param string $expectedResponse Result to be returned |
||
| 93 | * @param boolean $checkSavedEntry To check db for correct result |
||
| 94 | * |
||
| 95 | * @dataProvider updateDataProvider |
||
| 96 | * |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | public function testRecordOriginHandlingOnUpdate( |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Test the validation of the RecordOriginConstraint |
||
| 133 | * |
||
| 134 | * @param array $ops PATCH operations |
||
| 135 | * @param integer $expectedStatus Header status code |
||
| 136 | * @param string $expectedResponse Result to be returned |
||
| 137 | * |
||
| 138 | * @dataProvider patchDataProvider |
||
| 139 | * |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | View Code Duplication | public function testRecordOriginHandlingOnPatch( |
|
| 143 | $ops, |
||
| 144 | $expectedStatus, |
||
| 145 | $expectedResponse |
||
| 146 | ) { |
||
| 147 | $client = static::createRestClient(); |
||
| 148 | |||
| 149 | $client->request('PATCH', '/person/customer/100', [], [], [], json_encode($ops)); |
||
| 150 | |||
| 151 | $response = $client->getResponse(); |
||
| 152 | $this->assertEquals($expectedStatus, $response->getStatusCode()); |
||
| 153 | $this->assertEquals($expectedResponse, $client->getResults()); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * test to see if DELETE on a recordorigin: core is denied |
||
| 158 | * |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | public function testDeleteHandling() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Data provider for POST related stuff |
||
| 179 | * |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | public function createDataProvider() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Data provider for PUT related stuff |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | public function updateDataProvider() |
||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * providing the conditional errorMessage Object |
||
| 281 | * |
||
| 282 | * @param string $changedFields the Field the user wants to change |
||
| 283 | * |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | private function getExpectedErrorMessage($changedFields) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Data provider for PATCH related stuff |
||
| 301 | * |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | public function patchDataProvider() |
||
| 362 | } |
||
| 363 |
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.