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 |
||
| 13 | class ContentTest extends RESTFunctionalTestCase |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Covers POST /content/objects. |
||
| 17 | * |
||
| 18 | * @return string REST content ID |
||
| 19 | */ |
||
| 20 | View Code Duplication | public function testCreateContent() |
|
| 21 | { |
||
| 22 | $request = $this->createHttpRequest('POST', '/api/ezp/v2/content/objects', 'ContentCreate+xml', 'ContentInfo+json'); |
||
| 23 | $string = $this->addTestSuffix(__FUNCTION__); |
||
| 24 | $body = <<< XML |
||
| 25 | <?xml version="1.0" encoding="UTF-8"?> |
||
| 26 | <ContentCreate> |
||
| 27 | <ContentType href="/api/ezp/v2/content/types/1" /> |
||
| 28 | <mainLanguageCode>eng-GB</mainLanguageCode> |
||
| 29 | <LocationCreate> |
||
| 30 | <ParentLocation href="/api/ezp/v2/content/locations/1/2" /> |
||
| 31 | <priority>0</priority> |
||
| 32 | <hidden>false</hidden> |
||
| 33 | <sortField>PATH</sortField> |
||
| 34 | <sortOrder>ASC</sortOrder> |
||
| 35 | </LocationCreate> |
||
| 36 | <Section href="/api/ezp/v2/content/sections/1" /> |
||
| 37 | <alwaysAvailable>true</alwaysAvailable> |
||
| 38 | <remoteId>{$string}</remoteId> |
||
| 39 | <User href="/api/ezp/v2/user/users/14" /> |
||
| 40 | <modificationDate>2012-09-30T12:30:00</modificationDate> |
||
| 41 | <fields> |
||
| 42 | <field> |
||
| 43 | <fieldDefinitionIdentifier>name</fieldDefinitionIdentifier> |
||
| 44 | <languageCode>eng-GB</languageCode> |
||
| 45 | <fieldValue>{$string}</fieldValue> |
||
| 46 | </field> |
||
| 47 | </fields> |
||
| 48 | </ContentCreate> |
||
| 49 | XML; |
||
| 50 | $request->setContent($body); |
||
| 51 | |||
| 52 | $response = $this->sendHttpRequest($request); |
||
| 53 | |||
| 54 | self::assertHttpResponseCodeEquals($response, 201); |
||
| 55 | self::assertHttpResponseHasHeader($response, 'Location'); |
||
| 56 | |||
| 57 | $href = $response->getHeader('Location'); |
||
| 58 | $this->addCreatedElement($href); |
||
| 59 | |||
| 60 | return $href; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @depends testCreateContent |
||
| 65 | * Covers PUBLISH /content/objects/<contentId>/versions/<versionNumber> |
||
| 66 | * |
||
| 67 | * @return string REST content ID |
||
| 68 | */ |
||
| 69 | public function testPublishContent($restContentHref) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @depends testPublishContent |
||
| 81 | * Covers GET /content/objects?remoteId=<remoteId> |
||
| 82 | */ |
||
| 83 | public function testRedirectContent($restContentHref) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @depends testPublishContent |
||
| 95 | */ |
||
| 96 | public function testLoadContent($restContentHref) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @depends testPublishContent |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function testUpdateContentMetadata($restContentHref) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @depends testPublishContent |
||
| 129 | * |
||
| 130 | * @return string ContentVersion REST ID |
||
| 131 | */ |
||
| 132 | View Code Duplication | public function testCreateDraftFromVersion($restContentHref) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @depends testPublishContent |
||
| 146 | * Covers GET /content/objects/<contentId>/currentversion |
||
| 147 | * @covers \eZ\Publish\Core\REST\Server\Controller\Content::redirectCurrentVersion |
||
| 148 | */ |
||
| 149 | public function testRedirectCurrentVersion($restContentHref) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @depends testCreateDraftFromVersion |
||
| 162 | * Covers GET /content/objects/<contentId>/versions/<versionNumber> |
||
| 163 | * |
||
| 164 | * @param string $restContentVersionHref |
||
| 165 | */ |
||
| 166 | public function testLoadContentVersion($restContentVersionHref) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Covers COPY /content/objects/<contentId>. |
||
| 179 | * @depends testPublishContent |
||
| 180 | * |
||
| 181 | * @return string the copied content href |
||
| 182 | */ |
||
| 183 | public function testCopyContent($restContentHref) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Covers DELETE /content/objects/<versionNumber>. |
||
| 202 | * @depends testCopyContent |
||
| 203 | */ |
||
| 204 | public function testDeleteContent($restContentHref) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @depends testPublishContent |
||
| 216 | * Covers GET /content/objects/<contentId>/versions |
||
| 217 | */ |
||
| 218 | public function testLoadContentVersions($restContentHref) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @depends testPublishContent |
||
| 229 | * |
||
| 230 | * @param string $restContentHref /content/objects/<contentId> |
||
| 231 | * Covers COPY /content/objects/<contentId>/currentversion |
||
| 232 | * |
||
| 233 | * @return string the ID of the created version (/content/objects/<contentId>/versions/<versionNumber> |
||
| 234 | */ |
||
| 235 | View Code Duplication | public function testCreateDraftFromCurrentVersion($restContentHref) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @depends testCreateDraftFromCurrentVersion |
||
| 249 | * |
||
| 250 | * @param string $restContentVersionHref /api/ezp/v2/content/objects/<contentId>/versions>/<versionNumber> |
||
| 251 | * Covers DELETE /api/ezp/v2/content/objects/<contentId>/versions>/<versionNumber> |
||
| 252 | */ |
||
| 253 | public function testDeleteContentVersion($restContentVersionHref) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @depends testCreateDraftFromVersion |
||
| 264 | * Covers PATCH /content/objects/<contentId>/versions>/<versionNumber> |
||
| 265 | * |
||
| 266 | * @param string $restContentVersionHref /content/objects/<contentId>/versions>/<versionNumber> |
||
| 267 | */ |
||
| 268 | View Code Duplication | public function testUpdateVersion($restContentVersionHref) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @depends testPublishContent |
||
| 293 | * Covers GET /content/objects/<contentId>/relations |
||
| 294 | */ |
||
| 295 | public function testRedirectCurrentVersionRelations($restContentHref) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @depends testCreateDraftFromVersion |
||
| 311 | * Covers GET /content/objects/<contentId>/versions/<versionNumber>/relations |
||
| 312 | */ |
||
| 313 | public function testLoadVersionRelations($restContentVersionHref) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @depends testCreateDraftFromVersion |
||
| 324 | * Covers POST /content/objects/<contentId>/versions/<versionNumber>/relations/<relationId> |
||
| 325 | * |
||
| 326 | * @return string created relation HREF (/content/objects/<contentId>/versions/<versionNumber>/relations/<relationId> |
||
| 327 | */ |
||
| 328 | View Code Duplication | public function testCreateRelation($restContentVersionHref) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @depends testCreateRelation |
||
| 351 | * Covers GET /content/objects/<contentId>/versions/<versionNo>/relations/<relationId> |
||
| 352 | */ |
||
| 353 | public function testLoadVersionRelation($restContentRelationHref) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Returns the Content key from the decoded JSON of $restContentId's contentInfo. |
||
| 366 | * |
||
| 367 | * |
||
| 368 | * @throws \InvalidArgumentException |
||
| 369 | * |
||
| 370 | * @param string $restContentHref /api/ezp/v2/content/objects/<contentId> |
||
| 371 | * |
||
| 372 | * @return array |
||
| 373 | */ |
||
| 374 | private function loadContent($restContentHref) |
||
| 391 | |||
| 392 | View Code Duplication | public function testCreateView() |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Covers DELETE /content/objects/<contentId>/currentversion/translations/<languageCode>. |
||
| 420 | * |
||
| 421 | * @depends testCreateDraftFromVersion |
||
| 422 | * |
||
| 423 | * @param string $restContentVersionHref |
||
| 424 | */ |
||
| 425 | public function testDeletePublishedVersionTranslation($restContentVersionHref) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Publish another Version with new Translation. |
||
| 478 | * |
||
| 479 | * @param string $restContentVersionHref |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | View Code Duplication | private function createVersionTranslation($restContentVersionHref) |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Make REST API calls to check if the given Language exists and create it if it doesn't. |
||
| 515 | * |
||
| 516 | * @param string $languageCode |
||
| 517 | * @param string $languageName |
||
| 518 | */ |
||
| 519 | private function ensureLanguageExists($languageCode, $languageName) |
||
| 523 | } |
||
| 524 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.