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 |
||
| 19 | class VersionInfoTest extends ValueObjectVisitorBaseTest |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var \DateTime |
||
| 23 | */ |
||
| 24 | protected $creationDate; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var \DateTime |
||
| 28 | */ |
||
| 29 | protected $modificationDate; |
||
| 30 | |||
| 31 | public function setUp() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Test the VersionInfo visitor. |
||
| 39 | * |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | public function testVisit() |
||
| 43 | { |
||
| 44 | $visitor = $this->getVisitor(); |
||
| 45 | $generator = $this->getGenerator(); |
||
| 46 | |||
| 47 | $generator->startDocument(null); |
||
| 48 | |||
| 49 | $versionInfo = new Content\VersionInfo( |
||
| 50 | array( |
||
| 51 | 'id' => 23, |
||
| 52 | 'versionNo' => 5, |
||
| 53 | 'status' => Content\VersionInfo::STATUS_PUBLISHED, |
||
| 54 | 'creationDate' => $this->creationDate, |
||
| 55 | 'creatorId' => 14, |
||
| 56 | 'modificationDate' => $this->modificationDate, |
||
| 57 | 'initialLanguageCode' => 'eng-US', |
||
| 58 | 'languageCodes' => array('eng-US', 'ger-DE'), |
||
| 59 | 'names' => array( |
||
| 60 | 'eng-US' => 'Sindelfingen', |
||
| 61 | 'eng-GB' => 'Bielefeld', |
||
| 62 | ), |
||
| 63 | 'contentInfo' => new ContentInfo(array('id' => 42)), |
||
| 64 | ) |
||
| 65 | ); |
||
| 66 | |||
| 67 | $this->setVisitValueObjectExpectations([ |
||
| 68 | new ResourceRouteReference('ezpublish_rest_loadUser', ['userId' => $versionInfo->creatorId]), |
||
|
|
|||
| 69 | new ResourceRouteReference('ezpublish_rest_loadContent', ['contentId' => $versionInfo->contentInfo->id]), |
||
| 70 | ]); |
||
| 71 | |||
| 72 | $visitor->visit( |
||
| 73 | $this->getVisitorMock(), |
||
| 74 | $generator, |
||
| 75 | $versionInfo |
||
| 76 | ); |
||
| 77 | |||
| 78 | $result = $generator->endDocument(null); |
||
| 79 | |||
| 80 | $this->assertNotNull($result); |
||
| 81 | |||
| 82 | return $result; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $result |
||
| 87 | * |
||
| 88 | * @depends testVisit |
||
| 89 | */ |
||
| 90 | public function testResultContainsVersionInfoChildren($result) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $result |
||
| 108 | * |
||
| 109 | * @depends testVisit |
||
| 110 | */ |
||
| 111 | public function testVersionInfoIdElement($result) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $result |
||
| 126 | * |
||
| 127 | * @depends testVisit |
||
| 128 | */ |
||
| 129 | public function testVersionInfoVersionNoElement($result) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $result |
||
| 144 | * |
||
| 145 | * @depends testVisit |
||
| 146 | */ |
||
| 147 | public function testVersionInfoStatusElement($result) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $result |
||
| 162 | * |
||
| 163 | * @depends testVisit |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function testVersionInfoCreationDateElement($result) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param string $result |
||
| 180 | * |
||
| 181 | * @depends testVisit |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function testVersionInfoModificationDateElement($result) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $result |
||
| 198 | * |
||
| 199 | * @depends testVisit |
||
| 200 | */ |
||
| 201 | public function testVersionInfoInitialLanguageCodeElement($result) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $result |
||
| 216 | * |
||
| 217 | * @depends testVisit |
||
| 218 | */ |
||
| 219 | public function testVersionInfoLanguageCodesElement($result) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $result |
||
| 234 | * |
||
| 235 | * @depends testVisit |
||
| 236 | */ |
||
| 237 | public function testVersionInfoNamesElement($result) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $result |
||
| 255 | * |
||
| 256 | * @depends testVisit |
||
| 257 | */ |
||
| 258 | public function testVersionInfoContentElement($result) |
||
| 259 | { |
||
| 260 | $this->assertXMLTag( |
||
| 261 | array( |
||
| 262 | 'tag' => 'Content', |
||
| 263 | 'attributes' => array( |
||
| 264 | 'media-type' => 'application/vnd.ez.api.ContentInfo+xml', |
||
| 265 | ), |
||
| 266 | ), |
||
| 267 | $result, |
||
| 268 | 'Invalid <initialLanguageCode> value.', |
||
| 269 | false |
||
| 270 | ); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get the VersionInfo visitor. |
||
| 275 | * |
||
| 276 | * @return \eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\VersionInfo |
||
| 277 | */ |
||
| 278 | protected function internalGetVisitor() |
||
| 282 | } |
||
| 283 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: