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 |
||
27 | class Customization implements ICustomization |
||
28 | { |
||
29 | use TPayload, TCustomizationInstructionContainer; |
||
30 | |||
31 | const ROOT_NODE = 'Customization'; |
||
32 | |||
33 | /** @var int */ |
||
34 | protected $customizationId; |
||
35 | /** @var IPriceGroup */ |
||
36 | protected $extendedPrice; |
||
37 | /** @var string */ |
||
38 | protected $itemId; |
||
39 | /** @var string */ |
||
40 | protected $customizedItemId; |
||
41 | /** @var IOrderItem */ |
||
42 | protected $customizedItem; |
||
43 | |||
44 | /** |
||
45 | * @param IValidatorIterator |
||
46 | * @param ISchemaValidator |
||
47 | * @param IPayloadMap |
||
48 | * @param LoggerInterface |
||
49 | * @param IPayload |
||
50 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
51 | */ |
||
52 | public function __construct( |
||
53 | IValidatorIterator $validators, |
||
54 | ISchemaValidator $schemaValidator, |
||
|
|||
55 | IPayloadMap $payloadMap, |
||
56 | LoggerInterface $logger, |
||
57 | IPayload $parentPayload = null |
||
58 | ) { |
||
59 | $this->logger = $logger; |
||
60 | $this->validators = $validators; |
||
61 | $this->payloadMap = $payloadMap; |
||
62 | $this->parentPayload = $parentPayload; |
||
63 | $this->payloadFactory = new PayloadFactory; |
||
64 | |||
65 | $this->extractionPaths = [ |
||
66 | 'itemId' => 'string(x:ItemId)', |
||
67 | ]; |
||
68 | $this->optionalExtractionPaths = [ |
||
69 | 'customizationId' => 'x:CustomizationId', |
||
70 | 'customizedItemId' => 'x:Item/@ref', |
||
71 | ]; |
||
72 | $this->subpayloadExtractionPaths = [ |
||
73 | 'customizationInstructions' => 'x:Instructions', |
||
74 | ]; |
||
75 | |||
76 | $this->customizationInstructions = $this->buildPayloadForInterface( |
||
77 | self::CUSTOMIZATION_INSTRUCTION_ITERABLE_INTERFACE |
||
78 | ); |
||
79 | } |
||
80 | |||
81 | public function getEmptyExtendedPrice() |
||
85 | |||
86 | public function getCustomizationId() |
||
90 | |||
91 | public function setCustomizationId($customizationId) |
||
92 | { |
||
93 | $this->customizationId = $customizationId; |
||
94 | return $this; |
||
95 | } |
||
96 | |||
97 | public function getExtendedPrice() |
||
101 | |||
102 | public function setExtendedPrice(IPriceGroup $extendedPrice) |
||
103 | { |
||
104 | $this->extendedPrice = $extendedPrice; |
||
105 | return $this; |
||
106 | } |
||
107 | |||
108 | public function getItemId() |
||
112 | |||
113 | public function setItemId($itemId) |
||
114 | { |
||
115 | $this->itemId = $this->cleanString($itemId, 20); |
||
116 | return $this; |
||
117 | } |
||
118 | |||
119 | public function getCustomizedItem() |
||
120 | { |
||
121 | if (!$this->customizedItem) { |
||
122 | $this->customizedItem = $this->findCustomizedItem(); |
||
123 | } |
||
124 | return $this->customizedItem; |
||
125 | } |
||
126 | |||
127 | public function setCustomizedItem(IOrderItem $customizedItem) |
||
128 | { |
||
129 | $this->customizedItem = $customizedItem; |
||
130 | $orderItemContainer = $this->getOrderItemContainer(); |
||
131 | if ($orderItemContainer) { |
||
132 | $orderItemContainer->getOrderItems()->offsetSet($customizedItem); |
||
133 | } |
||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | public function getCustomizedItemId() |
||
138 | { |
||
139 | $item = $this->getCustomizedItem(); |
||
140 | return $item ? $item->getId() : $this->customizedItemId; |
||
141 | } |
||
142 | |||
143 | protected function getParentOrderItem() |
||
147 | |||
148 | protected function getOrderItemContainer() |
||
152 | |||
153 | /** |
||
154 | * Search through related payloads for the item the customization applies |
||
155 | * to. Will attempt to dereference an item id if one exists. If there is |
||
156 | * not an id to dereference, will default to the first order item ancestor. |
||
157 | * If no suitable item can be found, will return null. |
||
158 | * |
||
159 | * This will perform a fresh search on every invocation. As payloads related |
||
160 | * to this one may mutate around this payload, it is not guaranteed to be |
||
161 | * idempotent so results are not cached. |
||
162 | * |
||
163 | * @return IOrderItem|null |
||
164 | */ |
||
165 | protected function findCustomizedItem() |
||
166 | { |
||
167 | // Get the parent item if there is one, may be null if there isn't. |
||
168 | $parentItem = $this->getParentOrderItem(); |
||
169 | // If there isn't an id to dereference, accept the parent, whether |
||
170 | // there is one or not, as the item being referenced. If there is |
||
171 | // an id to dereference and it matches the parent, no need to search |
||
172 | // further, so it can be returned. |
||
173 | if (is_null($this->customizedItemId) || |
||
174 | ($parentItem && $parentItem->getId() === $this->customizedItemId) |
||
175 | ) { |
||
176 | return $parentItem; |
||
177 | } |
||
178 | |||
179 | // If an item hasn't been found yet, look though all items that could |
||
180 | // be referenced (items in a related IOrderItemContainer) by the item id |
||
181 | // for a match - know there is an id as if there weren't, the parent |
||
182 | // item would have already been returned. |
||
183 | $itemContainer = $this->getOrderItemContainer(); |
||
184 | if ($itemContainer) { |
||
185 | foreach ($itemContainer->getOrderItems() as $orderItem) { |
||
186 | if ($orderItem->getId() === $this->customizedItemId) { |
||
187 | $this->customizedItem = $orderItem; |
||
188 | break; |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | // If still no match, one can't be found/doesn't exist so return null. |
||
193 | return null; |
||
194 | } |
||
195 | |||
196 | View Code Duplication | protected function serializeContents() |
|
197 | { |
||
198 | return $this->serializeOptionalXmlEncodedValue('CustomizationId', $this->getCustomizationId()) |
||
199 | . $this->getCustomizationInstructions()->serialize() |
||
200 | . (!is_null($this->getExtendedPrice()) ? $this->getExtendedPrice()->setRootNodeName('ExtendedPrice')->serialize() : '') |
||
201 | . "<ItemId>{$this->xmlEncode($this->getItemId())}</ItemId>" |
||
202 | . $this->serializeCustomizedItem(); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * When a customized item id has been provided, return a serialization |
||
207 | * of the reference - "Item" node with "ref" attribute set to the item id. |
||
208 | * When no customized item id is set, returns an empty string. |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | protected function serializeCustomizedItem() |
||
213 | { |
||
214 | $itemId = $this->getCustomizedItemId(); |
||
215 | return $itemId ? "<Item ref='{$this->xmlEncode($itemId)}'/>" : ''; |
||
216 | } |
||
217 | |||
218 | View Code Duplication | protected function deserializeExtra($serializedPayload) |
|
219 | { |
||
220 | $xpath = $this->getPayloadAsXPath($serializedPayload); |
||
221 | $priceNode = $xpath->query('x:ExtendedPrice')->item(0); |
||
222 | if ($priceNode) { |
||
223 | $this->extendedPrice = $this->getEmptyExtendedPrice()->deserialize($priceNode->C14N()); |
||
224 | } |
||
225 | } |
||
226 | |||
227 | protected function getRootNodeName() |
||
231 | |||
232 | protected function getXmlNamespace() |
||
236 | } |
||
237 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.