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 |
||
29 | class AcceptedOrderItem extends OrderItem implements IAcceptedOrderItem |
||
30 | { |
||
31 | use TProductPrice, TItemShipping, TAmount { |
||
32 | TAmount::serializeAmount insteadof TProductPrice; |
||
33 | TAmount::sanitizeAmount insteadof TProductPrice; |
||
34 | } |
||
35 | |||
36 | public function __construct( |
||
37 | IValidatorIterator $validators, |
||
38 | ISchemaValidator $schemaValidator, |
||
39 | IPayloadMap $payloadMap, |
||
40 | LoggerInterface $logger, |
||
41 | IPayload $parentPayload = null |
||
42 | ) { |
||
43 | parent::__construct($validators, $schemaValidator, $payloadMap, $logger, $parentPayload); |
||
44 | |||
45 | $this->logger = $logger; |
||
|
|||
46 | $this->payloadMap = $payloadMap; |
||
47 | $this->payloadFactory = new PayloadFactory(); |
||
48 | |||
49 | $this->extractionPaths = array_merge( |
||
50 | $this->extractionPaths, |
||
51 | [ |
||
52 | 'shipmentMethod' => 'string(x:Shipping/x:ShipmentMethod)', |
||
53 | 'shipmentMethodDisplayText' => 'string(x:Shipping/x:ShipmentMethod/@displayText)', |
||
54 | ] |
||
55 | ); |
||
56 | $this->optionalExtractionPaths = array_merge( |
||
57 | $this->optionalExtractionPaths, |
||
58 | [ |
||
59 | 'amount' => 'x:Pricing/x:Amount', |
||
60 | 'unitPrice' => 'x:Pricing/x:UnitPrice', |
||
61 | 'remainder' => 'x:Pricing/x:Amount/@remainder', |
||
62 | ] |
||
63 | ); |
||
64 | } |
||
65 | |||
66 | protected function deserializeExtra($serializedPayload) |
||
67 | { |
||
68 | $xpath = $this->getPayloadAsXPath($serializedPayload); |
||
69 | return $this->deserializeDestination($xpath)->deserializesShipDate($xpath); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Shipping destination may be one of two payload types. If the message |
||
74 | * includes a ShippedAddress node, use the mailing address destination type. |
||
75 | * If the message includes a StoreFrontAddress node, use a store front |
||
76 | * details destination type. |
||
77 | * |
||
78 | * @return self |
||
79 | */ |
||
80 | View Code Duplication | protected function deserializeDestination(DOMXPath $xpath) |
|
81 | { |
||
82 | $destinationNode = $xpath->query('x:Shipping/x:ShippedAddress|x:Shipping/x:StoreFrontAddress')->item(0); |
||
83 | if ($destinationNode) { |
||
84 | $mailingAddress = static::MAILING_ADDRESS_INTERFACE; |
||
85 | $storeFront = static::STORE_FRONT_DETAILS_INTERFACE; |
||
86 | $destination = null; |
||
87 | switch ($destinationNode->nodeName) { |
||
88 | case $mailingAddress::ROOT_NODE: |
||
89 | $destination = $this->buildPayloadForInterface($mailingAddress); |
||
90 | break; |
||
91 | case $storeFront::ROOT_NODE: |
||
92 | $destination = $this->buildPayloadForInterface($storeFront); |
||
93 | break; |
||
94 | } |
||
95 | if ($destination) { |
||
96 | $destination->deserialize($destinationNode->C14N()); |
||
97 | $this->setDestination($destination); |
||
98 | } |
||
99 | } |
||
100 | return $this; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Deserialize the EstimatedShipDate to a DateTime object. |
||
105 | * |
||
106 | * @param DOMXPath |
||
107 | * @return self |
||
108 | */ |
||
109 | protected function deserializesShipDate(DOMXPath $xpath) |
||
110 | { |
||
111 | $estimatedShipDate = $xpath->evaluate('string(x:Shipping/x:EstimatedShipDate)'); |
||
112 | if ($estimatedShipDate) { |
||
113 | $shipDate = new DateTime($estimatedShipDate); |
||
114 | $this->setEstimatedShipDate($shipDate); |
||
115 | } |
||
116 | return $this; |
||
117 | } |
||
118 | |||
119 | protected function serializeContents() |
||
120 | { |
||
121 | return parent::serializeContents() |
||
122 | . ($this->hasPricingData() ? $this->serializeProductPrice() : '') |
||
123 | . ($this->hasShippingData() ? $this->serializeItemShipping() : ''); |
||
124 | } |
||
125 | |||
126 | protected function getXmlNamespace() |
||
130 | |||
131 | /** |
||
132 | * Check if the payload has shipping data to include in the serialization. |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | protected function hasShippingData() |
||
140 | |||
141 | /** |
||
142 | * Check if the payload has pricing data to include in the serialization. |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | protected function hasPricingData() |
||
150 | } |
||
151 |
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..