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 |
||
| 15 | class LocationUpdateTest extends BaseTest |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Tests the LocationUpdate parser. |
||
| 19 | */ |
||
| 20 | public function testParse() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Test LocationUpdate parser with missing sort field. |
||
| 77 | */ |
||
| 78 | View Code Duplication | public function testParseWithMissingSortField() |
|
| 79 | { |
||
| 80 | $inputArray = [ |
||
| 81 | 'priority' => 0, |
||
| 82 | 'remoteId' => 'remote-id', |
||
| 83 | 'sortOrder' => 'ASC', |
||
| 84 | ]; |
||
| 85 | |||
| 86 | $locationUpdate = $this->getParser(); |
||
| 87 | $result = $locationUpdate->parse($inputArray, $this->getParsingDispatcherMock()); |
||
| 88 | |||
| 89 | $this->assertInstanceOf( |
||
| 90 | RestLocationUpdateStruct::class, |
||
| 91 | $result |
||
| 92 | ); |
||
| 93 | |||
| 94 | $this->assertInstanceOf( |
||
| 95 | LocationUpdateStruct::class, |
||
| 96 | $result->locationUpdateStruct |
||
| 97 | ); |
||
| 98 | |||
| 99 | $this->assertNull( |
||
| 100 | $result->locationUpdateStruct->sortField |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Test LocationUpdate parser with missing sort order. |
||
| 106 | */ |
||
| 107 | View Code Duplication | public function testParseWithMissingSortOrder() |
|
| 108 | { |
||
| 109 | $inputArray = [ |
||
| 110 | 'priority' => 0, |
||
| 111 | 'remoteId' => 'remote-id', |
||
| 112 | 'sortField' => 'PATH', |
||
| 113 | ]; |
||
| 114 | |||
| 115 | $locationUpdate = $this->getParser(); |
||
| 116 | $result = $locationUpdate->parse($inputArray, $this->getParsingDispatcherMock()); |
||
| 117 | |||
| 118 | $this->assertInstanceOf( |
||
| 119 | RestLocationUpdateStruct::class, |
||
| 120 | $result |
||
| 121 | ); |
||
| 122 | |||
| 123 | $this->assertInstanceOf( |
||
| 124 | LocationUpdateStruct::class, |
||
| 125 | $result->locationUpdateStruct |
||
| 126 | ); |
||
| 127 | |||
| 128 | $this->assertNull( |
||
| 129 | $result->locationUpdateStruct->sortOrder |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns the LocationUpdateStruct parser. |
||
| 135 | * |
||
| 136 | * @return \eZ\Publish\Core\REST\Server\Input\Parser\LocationUpdate |
||
| 137 | */ |
||
| 138 | protected function internalGetParser() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the location service mock object. |
||
| 148 | * |
||
| 149 | * @return \eZ\Publish\API\Repository\LocationService |
||
| 150 | */ |
||
| 151 | protected function getLocationServiceMock() |
||
| 163 | } |
||
| 164 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.