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 | ||
| 22 | class RestExecutedViewTest extends ValueObjectVisitorBaseTest | ||
| 23 | { | ||
| 24 | /** | ||
| 25 | * Test the RestExecutedView visitor. | ||
| 26 | * | ||
| 27 | * @return \DOMDocument | ||
| 28 | */ | ||
| 29 | public function testVisit() | ||
| 30 |     { | ||
| 31 | $visitor = $this->getVisitor(); | ||
| 32 | $generator = $this->getGenerator(); | ||
| 33 | |||
| 34 | $generator->startDocument(null); | ||
| 35 | |||
| 36 | $view = new RestExecutedView( | ||
| 37 | [ | ||
| 38 | 'identifier' => 'test_view', | ||
| 39 | 'searchResults' => new SearchResult([ | ||
| 40 | 'searchHits' => [ | ||
| 41 | $this->buildContentSearchHit(), | ||
| 42 | $this->buildLocationSearchHit(), | ||
| 43 | ], | ||
| 44 | ]), | ||
| 45 | ] | ||
| 46 | ); | ||
| 47 | |||
| 48 | $this->addRouteExpectation( | ||
| 49 | 'ezpublish_rest_views_load', | ||
| 50 | ['viewId' => $view->identifier], | ||
| 51 |             "/content/views/{$view->identifier}" | ||
| 52 | ); | ||
| 53 | $this->addRouteExpectation( | ||
| 54 | 'ezpublish_rest_views_load_results', | ||
| 55 | ['viewId' => $view->identifier], | ||
| 56 |             "/content/views/{$view->identifier}/results" | ||
| 57 | ); | ||
| 58 | |||
| 59 | $visitor->visit( | ||
| 60 | $this->getVisitorMock(), | ||
|  | |||
| 61 | $generator, | ||
| 62 | $view | ||
| 63 | ); | ||
| 64 | |||
| 65 | $result = $generator->endDocument(null); | ||
| 66 | |||
| 67 | $this->assertNotNull($result); | ||
| 68 | |||
| 69 | $dom = new \DOMDocument(); | ||
| 70 | $dom->loadXml($result); | ||
| 71 | |||
| 72 | return $dom; | ||
| 73 | } | ||
| 74 | |||
| 75 | View Code Duplication | public function provideXpathAssertions() | |
| 76 |     { | ||
| 77 | return [ | ||
| 78 | ['/View'], | ||
| 79 | ['/View[@media-type="application/vnd.ez.api.View+xml"]'], | ||
| 80 | ['/View[@href="/content/views/test_view"]'], | ||
| 81 | ['/View/identifier'], | ||
| 82 | ['/View/identifier[text()="test_view"]'], | ||
| 83 | ['/View/Query'], | ||
| 84 | ['/View/Query[@media-type="application/vnd.ez.api.Query+xml"]'], | ||
| 85 | ['/View/Result'], | ||
| 86 | ['/View/Result[@media-type="application/vnd.ez.api.ViewResult+xml"]'], | ||
| 87 | ['/View/Result[@href="/content/views/test_view/results"]'], | ||
| 88 | ['/View/Result/searchHits/searchHit[@score="0.123" and @index="alexandria"]'], | ||
| 89 | ['/View/Result/searchHits/searchHit[@score="0.234" and @index="waze"]'], | ||
| 90 | ]; | ||
| 91 | } | ||
| 92 | |||
| 93 | /** | ||
| 94 | * @param string $xpath | ||
| 95 | * @param \DOMDocument $dom | ||
| 96 | * | ||
| 97 | * @depends testVisit | ||
| 98 | * @dataProvider provideXpathAssertions | ||
| 99 | */ | ||
| 100 | public function testGeneratedXml($xpath, \DOMDocument $dom) | ||
| 101 |     { | ||
| 102 | $this->assertXPath($dom, $xpath); | ||
| 103 | } | ||
| 104 | |||
| 105 | /** | ||
| 106 | * Get the Relation visitor. | ||
| 107 | * | ||
| 108 | * @return \eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\RestExecutedView | ||
| 109 | */ | ||
| 110 | protected function internalGetVisitor() | ||
| 111 |     { | ||
| 112 | return new ValueObjectVisitor\RestExecutedView( | ||
| 113 | $this->getLocationServiceMock(), | ||
| 114 | $this->getContentServiceMock(), | ||
| 115 | $this->getContentTypeServiceMock() | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | |||
| 119 | /** | ||
| 120 | * @return \eZ\Publish\API\Repository\LocationService|\PHPUnit\Framework\MockObject\MockObject | ||
| 121 | */ | ||
| 122 | public function getLocationServiceMock() | ||
| 123 |     { | ||
| 124 | return $this->createMock(LocationService::class); | ||
| 125 | } | ||
| 126 | |||
| 127 | /** | ||
| 128 | * @return \eZ\Publish\API\Repository\ContentService|\PHPUnit\Framework\MockObject\MockObject | ||
| 129 | */ | ||
| 130 | public function getContentServiceMock() | ||
| 131 |     { | ||
| 132 | return $this->createMock(ContentService::class); | ||
| 133 | } | ||
| 134 | |||
| 135 | /** | ||
| 136 | * @return \eZ\Publish\API\Repository\ContentTypeService|\PHPUnit\Framework\MockObject\MockObject | ||
| 137 | */ | ||
| 138 | public function getContentTypeServiceMock() | ||
| 139 |     { | ||
| 140 | return $this->createMock(ContentTypeService::class); | ||
| 141 | } | ||
| 142 | |||
| 143 | /** | ||
| 144 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchHit | ||
| 145 | */ | ||
| 146 | protected function buildContentSearchHit() | ||
| 147 |     { | ||
| 148 | return new SearchHit([ | ||
| 149 | 'score' => 0.123, | ||
| 150 | 'index' => 'alexandria', | ||
| 151 | 'valueObject' => new ApiValues\Content([ | ||
| 152 | 'versionInfo' => new Content\VersionInfo(['contentInfo' => new ContentInfo()]), | ||
| 153 | 'contentType' => new ContentType(), | ||
| 154 | ]), | ||
| 155 | ]); | ||
| 156 | } | ||
| 157 | |||
| 158 | /** | ||
| 159 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchHit | ||
| 160 | */ | ||
| 161 | protected function buildLocationSearchHit() | ||
| 169 | } | ||
| 170 | 
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.