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 |
||
| 28 | class Shipment implements IShipment |
||
| 29 | { |
||
| 30 | use TPayload, TShippedItemContainer; |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | protected $id; |
||
| 34 | /** @var string */ |
||
| 35 | protected $destinationRef; |
||
| 36 | /** @var string */ |
||
| 37 | protected $warehouse; |
||
| 38 | /** @var string */ |
||
| 39 | protected $carrier; |
||
| 40 | /** @var string */ |
||
| 41 | protected $mode; |
||
| 42 | /** @var string */ |
||
| 43 | protected $displayText; |
||
| 44 | /** @var string */ |
||
| 45 | protected $totalWeight; |
||
| 46 | /** @var DateTime */ |
||
| 47 | protected $shippedDate; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param IValidatorIterator |
||
| 51 | * @param ISchemaValidator |
||
| 52 | * @param IPayloadMap |
||
| 53 | * @param LoggerInterface |
||
| 54 | * @param IPayload |
||
| 55 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 56 | */ |
||
| 57 | public function __construct( |
||
| 58 | IValidatorIterator $validators, |
||
| 59 | ISchemaValidator $schemaValidator, |
||
| 60 | IPayloadMap $payloadMap, |
||
| 61 | LoggerInterface $logger, |
||
| 62 | IPayload $parentPayload = null |
||
| 63 | ) { |
||
| 64 | $this->logger = $logger; |
||
|
|
|||
| 65 | $this->validators = $validators; |
||
| 66 | $this->schemaValidator = $schemaValidator; |
||
| 67 | $this->parentPayload = $parentPayload; |
||
| 68 | $this->payloadMap = $payloadMap; |
||
| 69 | $this->payloadFactory = $this->getNewPayloadFactory(); |
||
| 70 | |||
| 71 | $this->initExtractPaths() |
||
| 72 | ->initOptionalExtractPaths() |
||
| 73 | ->initDatetimeExtractPaths() |
||
| 74 | ->initSubPayloadExtractPaths() |
||
| 75 | ->initSubPayloadProperties(); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Initialize the protected class property array self::extractionPaths with xpath |
||
| 80 | * key/value pairs. |
||
| 81 | * |
||
| 82 | * @return self |
||
| 83 | */ |
||
| 84 | protected function initExtractPaths() |
||
| 85 | { |
||
| 86 | $this->extractionPaths = [ |
||
| 87 | 'id' => 'string(@id)', |
||
| 88 | 'destinationRef' => 'string(@destinationRef)', |
||
| 89 | 'warehouse' => 'string(x:Warehouse)', |
||
| 90 | 'totalWeight' => 'string(x:TotalWeight)', |
||
| 91 | ]; |
||
| 92 | return $this; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Initialize the protected class property array self::optionalExtractionPaths with xpath |
||
| 97 | * key/value pairs. |
||
| 98 | * |
||
| 99 | * @return self |
||
| 100 | */ |
||
| 101 | protected function initOptionalExtractPaths() |
||
| 102 | { |
||
| 103 | $this->optionalExtractionPaths = [ |
||
| 104 | 'carrier' => 'x:Carrier', |
||
| 105 | 'mode' => 'x:Carrier/@mode', |
||
| 106 | 'displayText' => 'x:Carrier/@displayText', |
||
| 107 | ]; |
||
| 108 | return $this; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Initialize the protected class property array self::subpayloadExtractionPaths with xpath |
||
| 113 | * key/value pairs. |
||
| 114 | * |
||
| 115 | * @return self |
||
| 116 | */ |
||
| 117 | protected function initSubPayloadExtractPaths() |
||
| 118 | { |
||
| 119 | $this->subpayloadExtractionPaths = [ |
||
| 120 | 'shippedItems' => 'x:ShippedItems', |
||
| 121 | ]; |
||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Initialize the protected class property array self::datetimeExtractionPaths with xpath |
||
| 127 | * key/value pairs. |
||
| 128 | * |
||
| 129 | * @return self |
||
| 130 | */ |
||
| 131 | protected function initDatetimeExtractPaths() |
||
| 132 | { |
||
| 133 | $this->datetimeExtractionPaths = [ |
||
| 134 | 'shippedDate' => 'string(x:ShippedDate)', |
||
| 135 | ]; |
||
| 136 | return $this; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Initialize any sub-payload class properties with their concrete instance. |
||
| 141 | * |
||
| 142 | * @return self |
||
| 143 | */ |
||
| 144 | protected function initSubPayloadProperties() |
||
| 145 | { |
||
| 146 | $this->setShippedItems($this->buildPayloadForInterface( |
||
| 147 | static::SHIPPED_ITEM_ITERABLE_INTERFACE |
||
| 148 | )); |
||
| 149 | return $this; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @see IShipment::getId() |
||
| 154 | */ |
||
| 155 | public function getId() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @see IShipment::setId() |
||
| 162 | * @codeCoverageIgnore |
||
| 163 | */ |
||
| 164 | public function setId($id) |
||
| 165 | { |
||
| 166 | $this->id = $id; |
||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @see IShipment::getDestinationRef() |
||
| 172 | */ |
||
| 173 | public function getDestinationRef() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @see IShipment::setDestinationRef() |
||
| 180 | * @codeCoverageIgnore |
||
| 181 | */ |
||
| 182 | public function setDestinationRef($destinationRef) |
||
| 183 | { |
||
| 184 | $this->destinationRef = $destinationRef; |
||
| 185 | return $this; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @see IShipment::getWarehouse() |
||
| 190 | */ |
||
| 191 | public function getWarehouse() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @see IShipment::setWarehouse() |
||
| 198 | * @codeCoverageIgnore |
||
| 199 | */ |
||
| 200 | public function setWarehouse($warehouse) |
||
| 201 | { |
||
| 202 | $this->warehouse = $warehouse; |
||
| 203 | return $this; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @see IShipment::getCarrier() |
||
| 208 | */ |
||
| 209 | public function getCarrier() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @see IShipment::setCarrier() |
||
| 216 | * @codeCoverageIgnore |
||
| 217 | */ |
||
| 218 | public function setCarrier($carrier) |
||
| 219 | { |
||
| 220 | $this->carrier = $carrier; |
||
| 221 | return $this; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @see IShipment::getMode() |
||
| 226 | */ |
||
| 227 | public function getMode() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @see IShipment::setMode() |
||
| 234 | * @codeCoverageIgnore |
||
| 235 | */ |
||
| 236 | public function setMode($mode) |
||
| 237 | { |
||
| 238 | $this->mode = $mode; |
||
| 239 | return $this; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @see IShipment::getDisplayText() |
||
| 244 | */ |
||
| 245 | public function getDisplayText() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @see IShipment::setDisplayText() |
||
| 252 | * @codeCoverageIgnore |
||
| 253 | */ |
||
| 254 | public function setDisplayText($displayText) |
||
| 255 | { |
||
| 256 | $this->displayText = $displayText; |
||
| 257 | return $this; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @see IShipment::getTotalWeight() |
||
| 262 | */ |
||
| 263 | public function getTotalWeight() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @see IShipment::setTotalWeight() |
||
| 270 | * @codeCoverageIgnore |
||
| 271 | */ |
||
| 272 | public function setTotalWeight($totalWeight) |
||
| 273 | { |
||
| 274 | $this->totalWeight = $totalWeight; |
||
| 275 | return $this; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @see IShipment::getShippedDate() |
||
| 280 | */ |
||
| 281 | public function getShippedDate() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @see IShipment::setShippedDate() |
||
| 288 | * @codeCoverageIgnore |
||
| 289 | */ |
||
| 290 | public function setShippedDate(DateTime $shippedDate) |
||
| 291 | { |
||
| 292 | $this->shippedDate = $shippedDate; |
||
| 293 | return $this; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @see TPayload::serializeContents() |
||
| 298 | */ |
||
| 299 | protected function serializeContents() |
||
| 300 | { |
||
| 301 | return $this->getShippedItems()->serialize() |
||
| 302 | . $this->serializeRequiredValue('Warehouse', $this->xmlEncode($this->getWarehouse())) |
||
| 303 | . $this->serializeCarrierValue('Carrier', $this->getCarrier()) |
||
| 304 | . $this->serializeRequiredValue('TotalWeight', $this->xmlEncode($this->getTotalWeight())) |
||
| 305 | . $this->serializeOptionalDateValue('ShippedDate', 'c', $this->getShippedDate()); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @see TPayload::getRootNodeName() |
||
| 310 | */ |
||
| 311 | protected function getRootNodeName() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @see TPayload::getXmlNamespace() |
||
| 318 | */ |
||
| 319 | protected function getXmlNamespace() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Serializing the carrier XML node. |
||
| 326 | * |
||
| 327 | * @param string |
||
| 328 | * @param string |
||
| 329 | * @return string | null |
||
| 330 | */ |
||
| 331 | View Code Duplication | protected function serializeCarrierValue($nodeName, $value) |
|
| 332 | { |
||
| 333 | $modeAttribute = $this->serializeOptionalAttribute('mode', $this->xmlEncode($this->getMode())); |
||
| 334 | $displayTextAttribute = $this->serializeOptionalAttribute('displayText', $this->xmlEncode($this->getDisplayText())); |
||
| 335 | return $value |
||
| 336 | ? sprintf('<%s %s %s>%s</%1$s>', $nodeName, $modeAttribute, $displayTextAttribute, $this->xmlEncode($value)) : null; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @see TPayload::getRootAttributes() |
||
| 341 | */ |
||
| 342 | protected function getRootAttributes() |
||
| 349 | } |
||
| 350 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..