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:
Complex classes like OrderCreateRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OrderCreateRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class OrderCreateRequest implements IOrderCreateRequest |
||
32 | { |
||
33 | use TTopLevelPayload, TOrderCustomer, TOrderItemContainer, TShipGroupContainer, |
||
34 | TDestinationContainer, TPaymentContainer, TItemRelationshipContainer, TOrderHoldContainer, |
||
35 | TCustomAttributeContainer, TTemplateContainer, TOrderContext, TAmount; |
||
36 | |||
37 | /** @var string */ |
||
38 | protected $orderType; |
||
39 | /** @var string */ |
||
40 | protected $requestId; |
||
41 | /** @var string */ |
||
42 | protected $testType; |
||
43 | /** @var string */ |
||
44 | protected $orderId; |
||
45 | /** @var string */ |
||
46 | protected $levelOfService; |
||
47 | /** @var DateTime */ |
||
48 | protected $createTime; |
||
49 | /** @var IMailingAddress */ |
||
50 | protected $billingAddress; |
||
51 | /** @var string */ |
||
52 | protected $billingAddressIdRef; |
||
53 | /** @var string */ |
||
54 | protected $shopRunnerMessage; |
||
55 | /** @var string */ |
||
56 | protected $currency; |
||
57 | /** @var string */ |
||
58 | protected $associateName; |
||
59 | /** @var string */ |
||
60 | protected $associateNumber; |
||
61 | /** @var string */ |
||
62 | protected $associateStore; |
||
63 | /** @var bool */ |
||
64 | protected $taxHeader; |
||
65 | /** @var string */ |
||
66 | protected $printedCatalogCode; |
||
67 | /** @var string */ |
||
68 | protected $locale; |
||
69 | /** @var string */ |
||
70 | protected $dashboardRepId; |
||
71 | /** @var string */ |
||
72 | protected $orderSource; |
||
73 | /** @var string */ |
||
74 | protected $orderSourceType; |
||
75 | /** @var string */ |
||
76 | protected $orderHistoryUrl; |
||
77 | /** @var bool */ |
||
78 | protected $vatInclusivePricing; |
||
79 | /** @var string */ |
||
80 | protected $orderTotal; |
||
81 | |||
82 | /** |
||
83 | * @param IValidatorIterator |
||
84 | * @param ISchemaValidator |
||
85 | * @param IPayloadMap |
||
86 | * @param LoggerInterface |
||
87 | * @param IPayload |
||
88 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
89 | */ |
||
90 | public function __construct( |
||
91 | IValidatorIterator $validators, |
||
92 | ISchemaValidator $schemaValidator, |
||
93 | IPayloadMap $payloadMap, |
||
94 | LoggerInterface $logger, |
||
95 | IPayload $parentPayload = null |
||
96 | ) { |
||
97 | $this->logger = $logger; |
||
|
|||
98 | $this->validators = $validators; |
||
99 | $this->schemaValidator = $schemaValidator; |
||
100 | $this->payloadMap = $payloadMap; |
||
101 | $this->parentPayload = $parentPayload; |
||
102 | $this->payloadFactory = new PayloadFactory; |
||
103 | |||
104 | $this->setupSubPayloads(); |
||
105 | |||
106 | $this->extractionPaths = array_merge( |
||
107 | [ |
||
108 | 'requestId' => 'string(@requestId)', |
||
109 | 'orderId' => 'string(x:Order/@customerOrderId)', |
||
110 | 'billingAddressIdRef' => 'string(x:Order/x:Payment/x:BillingAddress/@ref)', |
||
111 | 'currency' => 'string(x:Order/x:Currency)', |
||
112 | 'locale' => 'string(x:Order/x:Locale)', |
||
113 | 'orderTotal' => 'number(x:Order/x:OrderTotal)', |
||
114 | 'firstName' => 'string(x:Order/x:Customer/x:Name/x:FirstName)', |
||
115 | 'lastName' => 'string(x:Order/x:Customer/x:Name/x:LastName)', |
||
116 | ], |
||
117 | $this->getOrderContextExtractionPaths() |
||
118 | ); |
||
119 | $this->optionalExtractionPaths = array_merge( |
||
120 | [ |
||
121 | 'orderType' => '@orderType', |
||
122 | 'testType' => '@testType', |
||
123 | 'levelOfService' => 'x:Order/@levelOfService', |
||
124 | 'shopRunnerMessage' => 'x:Order/x:ShopRunnerMessage', |
||
125 | 'associateName' => 'x:Order/x:Associate/x:Name', |
||
126 | 'associateNumber' => 'x:Order/x:Associate/x:Number', |
||
127 | 'associateStore' => 'x:Order/x:Associate/x:Store', |
||
128 | 'printedCatalogCode' => 'x:Order/x:PrintedCatalogCode', |
||
129 | 'dashboardRepId' => 'x:Order/x:DashboardRepId', |
||
130 | 'orderSource' => 'x:Order/x:OrderSource', |
||
131 | 'orderSourceType' => 'x:Order/x:OrderSource/@type', |
||
132 | 'orderHistoryUrl' => 'x:Order/x:OrderHistoryUrl', |
||
133 | 'customerId' => 'x:Order/x:Customer/@customerId', |
||
134 | 'gender' => 'x:Order/x:Customer/x:Gender', |
||
135 | 'emailAddress' => 'x:Order/x:Customer/x:EmailAddress', |
||
136 | 'taxId' => 'x:Order/x:Customer/x:CustomerTaxId', |
||
137 | 'middleName' => 'x:Order/x:Customer/x:Name/x:MiddleName', |
||
138 | 'honorificName' => 'x:Order/x:Customer/x:Name/x:Honorific', |
||
139 | ], |
||
140 | $this->getOrderContextOptionalExtractionPaths() |
||
141 | ); |
||
142 | $this->booleanExtractionPaths = [ |
||
143 | 'taxHeader' => 'string(x:Order/x:TaxHeader/x:Error)', |
||
144 | 'vatInclusivePricing' => 'string(x:Order/x:VATInclusivePricing)', |
||
145 | 'taxExempt' => 'string(x:Order/x:Customer/x:TaxExemptFlag)', |
||
146 | ]; |
||
147 | $this->subpayloadExtractionPaths = array_merge( |
||
148 | [ |
||
149 | 'loyaltyPrograms' => 'x:Order/x:Customer/x:LoyaltyPrograms', |
||
150 | 'orderItems' => 'x:Order/x:OrderItems', |
||
151 | 'payments' => 'x:Order/x:Payment/x:Payments', |
||
152 | 'shipGroups' => 'x:Order/x:Shipping/x:ShipGroups', |
||
153 | 'destinations' => 'x:Order/x:Shipping/x:Destinations', |
||
154 | 'itemRelationships' => 'x:Order/x:Relationships', |
||
155 | 'holds' => 'x:Order/x:Holds', |
||
156 | 'customAttributes' => 'x:Order/x:CustomAttributes', |
||
157 | 'templates' => 'x:Order/x:Templates', |
||
158 | ], |
||
159 | $this->getOrderContextSubpayloadExtractionPaths() |
||
160 | ); |
||
161 | } |
||
162 | |||
163 | public function getOrderType() |
||
167 | |||
168 | public function setOrderType($orderType) |
||
169 | { |
||
170 | if ($orderType === self::ORDER_TYPE_SALES || |
||
171 | $orderType === self::ORDER_TYPE_RETURN || |
||
172 | $orderType === self::ORDER_TYPE_PURCHASE || |
||
173 | $orderType === self::ORDER_TYPE_TRANSFER |
||
174 | ) { |
||
175 | $this->orderType = $orderType; |
||
176 | } |
||
177 | return $this; |
||
178 | } |
||
179 | |||
180 | public function getRequestId() |
||
184 | |||
185 | public function setRequestId($requestId) |
||
186 | { |
||
187 | $this->requestId = $this->cleanString($requestId, 40); |
||
188 | return $this; |
||
189 | } |
||
190 | |||
191 | public function getTestType() |
||
195 | |||
196 | public function setTestType($testType) |
||
197 | { |
||
198 | if ($testType === self::TEST_TYPE_WEBONLY || |
||
199 | $testType === self::TEST_TYPE_AUTOCANCEL || |
||
200 | $testType === self::TEST_TYPE_NORELEASE || |
||
201 | $testType === self::TEST_TYPE_AUTOSHIP |
||
202 | ) { |
||
203 | $this->testType = $testType; |
||
204 | } |
||
205 | return $this; |
||
206 | } |
||
207 | |||
208 | public function getOrderId() |
||
212 | |||
213 | public function setOrderId($orderId) |
||
214 | { |
||
215 | $this->orderId = $this->cleanString($orderId, 24); |
||
216 | return $this; |
||
217 | } |
||
218 | |||
219 | public function getLevelOfService() |
||
223 | |||
224 | public function setLevelOfService($levelOfService) |
||
225 | { |
||
226 | if ($levelOfService === self::LEVEL_OF_SERVICE_REGULAR || |
||
227 | $levelOfService === self::LEVEL_OF_SERVICE_RUSH |
||
228 | ) { |
||
229 | $this->levelOfService = $levelOfService; |
||
230 | } |
||
231 | return $this; |
||
232 | } |
||
233 | |||
234 | public function getCreateTime() |
||
238 | |||
239 | public function setCreateTime(DateTime $createTime) |
||
240 | { |
||
241 | $this->createTime = $createTime; |
||
242 | return $this; |
||
243 | } |
||
244 | |||
245 | public function getBillingAddress() |
||
246 | { |
||
247 | if (!$this->billingAddress && $this->billingAddressIdRef) { |
||
248 | foreach ($this->getDestinations() as $destination) { |
||
249 | if ($destination->getId() === $this->billingAddressIdRef) { |
||
250 | $this->billingAddress = $destination; |
||
251 | break; |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | return $this->billingAddress; |
||
256 | } |
||
257 | |||
258 | public function setBillingAddress(IMailingAddress $billingAddress) |
||
259 | { |
||
260 | $this->billingAddress = $billingAddress; |
||
261 | $this->billingAddressIdRef = $billingAddress->getId(); |
||
262 | // Add the billing address to the collection of destinations |
||
263 | $this->getDestinations()->offsetSet($billingAddress); |
||
264 | return $this; |
||
265 | } |
||
266 | |||
267 | public function getShopRunnerMessage() |
||
271 | |||
272 | public function setShopRunnerMessage($shopRunnerMessage) |
||
273 | { |
||
274 | $this->shopRunnerMessage = $this->normalizeWhitespace($shopRunnerMessage); |
||
275 | return $this; |
||
276 | } |
||
277 | |||
278 | public function getCurrency() |
||
282 | |||
283 | public function setCurrency($currency) |
||
284 | { |
||
285 | $cleaned = $this->cleanString($currency, 3); |
||
286 | if (strlen($cleaned) === 3) { |
||
287 | $this->currency = $currency; |
||
288 | } |
||
289 | return $this; |
||
290 | } |
||
291 | |||
292 | public function getAssociateName() |
||
296 | |||
297 | public function setAssociateName($associateName) |
||
298 | { |
||
299 | $this->associateName = $associateName; |
||
300 | return $this; |
||
301 | } |
||
302 | |||
303 | public function getAssociateNumber() |
||
307 | |||
308 | public function setAssociateNumber($associateNumber) |
||
309 | { |
||
310 | $this->associateNumber = $associateNumber; |
||
311 | return $this; |
||
312 | } |
||
313 | |||
314 | public function getAssociateStore() |
||
318 | |||
319 | public function setAssociateStore($associateStore) |
||
320 | { |
||
321 | $this->associateStore = $associateStore; |
||
322 | return $this; |
||
323 | } |
||
324 | |||
325 | public function getTaxHasErrors() |
||
329 | |||
330 | public function setTaxHasErrors($taxHeader) |
||
331 | { |
||
332 | $this->taxHeader = (bool) $taxHeader; |
||
333 | return $this; |
||
334 | } |
||
335 | |||
336 | public function getPrintedCatalogCode() |
||
340 | |||
341 | public function setPrintedCatalogCode($printedCatalogCode) |
||
342 | { |
||
343 | $this->printedCatalogCode = $printedCatalogCode; |
||
344 | return $this; |
||
345 | } |
||
346 | |||
347 | public function getLocale() |
||
351 | |||
352 | public function setLocale($locale) |
||
353 | { |
||
354 | $cleaned = $this->cleanString($locale, 5); |
||
355 | // local must match pattern two character language code, and underscore (_), |
||
356 | // two character country code |
||
357 | $this->locale = (preg_match('#^[a-z]{2}_[a-z]{2}$#i', $cleaned)) ? $cleaned : null; |
||
358 | return $this; |
||
359 | } |
||
360 | |||
361 | public function getDashboardRepId() |
||
365 | |||
366 | public function setDashboardRepId($dashboardRepId) |
||
367 | { |
||
368 | $this->dashboardRepId = $dashboardRepId; |
||
369 | return $this; |
||
370 | } |
||
371 | |||
372 | public function getOrderSource() |
||
376 | |||
377 | public function setOrderSource($orderSource) |
||
378 | { |
||
379 | $this->orderSource = $orderSource; |
||
380 | return $this; |
||
381 | } |
||
382 | |||
383 | public function getOrderSourceType() |
||
387 | |||
388 | public function setOrderSourceType($orderSourceType) |
||
389 | { |
||
390 | $this->orderSourceType = $orderSourceType; |
||
391 | return $this; |
||
392 | } |
||
393 | |||
394 | public function getOrderHistoryUrl() |
||
398 | |||
399 | public function setOrderHistoryUrl($orderHistoryUrl) |
||
400 | { |
||
401 | $this->orderHistoryUrl = $orderHistoryUrl; |
||
402 | return $this; |
||
403 | } |
||
404 | |||
405 | public function getVatInclusivePricing() |
||
409 | |||
410 | public function setVatInclusivePricing($vatInclusivePricing) |
||
411 | { |
||
412 | $this->vatInclusivePricing = (bool) $vatInclusivePricing; |
||
413 | return $this; |
||
414 | } |
||
415 | public function getOrderTotal() |
||
419 | |||
420 | public function setOrderTotal($orderTotal) |
||
421 | { |
||
422 | $this->orderTotal = $this->sanitizeAmount($orderTotal); |
||
423 | return $this; |
||
424 | } |
||
425 | |||
426 | protected function setupSubPayloads() |
||
427 | { |
||
428 | $this->loyaltyPrograms = $this->buildPayloadForInterface( |
||
429 | static::LOYALTY_PROGRAM_ITERABLE_INTERFACE |
||
430 | ); |
||
431 | $this->orderItems = $this->buildPayloadForInterface( |
||
432 | static::ORDER_ITEM_ITERABLE_INTERFACE |
||
433 | ); |
||
434 | $this->payments = $this->buildPayloadForInterface( |
||
435 | static::PAYMENT_ITERABLE_INTERFACE |
||
436 | ); |
||
437 | $this->shipGroups = $this->buildPayloadForInterface( |
||
438 | static::SHIP_GROUP_ITERABLE_INTERFACE |
||
439 | ); |
||
440 | $this->destinations = $this->buildPayloadForInterface( |
||
441 | static::DESTINATION_ITERABLE_INTERFACE |
||
442 | ); |
||
443 | $this->itemRelationships = $this->buildPayloadForInterface( |
||
444 | static::ITEM_RELATIONSHIP_ITERABLE_INTERFACE |
||
445 | ); |
||
446 | $this->holds = $this->buildPayloadForInterface( |
||
447 | static::ORDER_HOLD_ITERABLE_INTERFACE |
||
448 | ); |
||
449 | $this->customAttributes = $this->buildPayloadForInterface( |
||
450 | static::CUSTOM_ATTRIBUTE_ITERABLE_INTERFACE |
||
451 | ); |
||
452 | $this->templates = $this->buildPayloadForInterface( |
||
453 | static::TEMPLATE_ITERABLE_INTERFACE |
||
454 | ); |
||
455 | $this->orderContextCustomAttributes = $this->buildPayloadForInterface( |
||
456 | static::ORDER_CONTEXT_CUSTOM_ATTRIBUTE_ITERABLE_INTERFACE |
||
457 | ); |
||
458 | } |
||
459 | |||
460 | protected function deserializeExtra($serializedPayload) |
||
461 | { |
||
462 | $xpath = $this->getPayloadAsXPath($serializedPayload); |
||
463 | return $this->deserializeDateTimeValues($xpath)->deserializeExtraOrderContext($xpath); |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * Deserialize date time values as DateTime objects. |
||
468 | * |
||
469 | * @param DOMXPath |
||
470 | * @return self |
||
471 | */ |
||
472 | protected function deserializeDateTimeValues(DOMXPath $xpath) |
||
473 | { |
||
474 | $dateProperties = [ |
||
475 | 'createTime' => 'string(x:Order/x:CreateTime)', |
||
476 | 'dateOfBirth' => 'string(x:Order/x:Customer/x:DateOfBirth)', |
||
477 | ]; |
||
478 | View Code Duplication | foreach ($dateProperties as $prop => $extractPath) { |
|
479 | $value = $xpath->evaluate($extractPath); |
||
480 | $this->$prop = $value ? new DateTime($value) : null; |
||
481 | } |
||
482 | return $this; |
||
483 | } |
||
484 | |||
485 | protected function serializeContents() |
||
489 | |||
490 | /** |
||
491 | * Serialize the order details. |
||
492 | * |
||
493 | * @return string |
||
494 | */ |
||
495 | protected function serializeOrder() |
||
496 | { |
||
497 | return "<Order customerOrderId='{$this->xmlEncode($this->getOrderId())}' levelOfService='{$this->xmlEncode($this->getLevelOfService())}'>" |
||
498 | . $this->serializeOrderCustomer() |
||
499 | . "<CreateTime>{$this->getCreateTime()->format('c')}</CreateTime>" |
||
500 | . $this->getOrderItems()->serialize() |
||
501 | . '<Shipping>' |
||
502 | . $this->getShipGroups()->serialize() |
||
503 | . $this->getDestinations()->serialize() |
||
504 | . '</Shipping>' |
||
505 | . '<Payment>' |
||
506 | . "<BillingAddress ref='{$this->xmlEncode($this->getBillingAddress()->getId())}' />" |
||
507 | . $this->getPayments()->serialize() |
||
508 | . '</Payment>' |
||
509 | . $this->serializeOptionalXmlEncodedValue('ShopRunnerMessage', $this->getShopRunnerMessage()) |
||
510 | . "<Currency>{$this->xmlEncode($this->getCurrency())}</Currency>" |
||
511 | . $this->serializeAssociate() |
||
512 | . $this->serializeTaxHeader() |
||
513 | . $this->serializeOptionalXmlEncodedValue('PrintedCatalogCode', $this->getPrintedCatalogCode()) |
||
514 | . "<Locale>{$this->xmlEncode($this->getLocale())}</Locale>" |
||
515 | . $this->getItemRelationships()->serialize() |
||
516 | . $this->serializeOptionalXmlEncodedValue('DashboardRepId', $this->getDashboardRepId()) |
||
517 | . $this->serializeOrderSource() |
||
518 | . $this->getHolds()->serialize() |
||
519 | . $this->getCustomAttributes()->serialize() |
||
520 | . $this->getTemplates()->serialize() |
||
521 | . ($this->getOrderHistoryUrl() ? "<OrderHistoryUrl>{$this->xmlEncode($this->getOrderHistoryUrl())}</OrderHistoryUrl>" : "") |
||
522 | . $this->serializeVatInclusivePricing() |
||
523 | . $this->serializeOptionalAmount('OrderTotal', $this->getOrderTotal()) |
||
524 | . '</Order>'; |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * Serialize store associate details. |
||
529 | * |
||
530 | * @return string |
||
531 | */ |
||
532 | protected function serializeAssociate() |
||
533 | { |
||
534 | if (!is_null($this->getAssociateNumber())) { |
||
535 | return '<Associate>' |
||
536 | . "<Name>{$this->xmlEncode($this->getAssociateName())}</Name>" |
||
537 | . "<Number>{$this->xmlEncode($this->getAssociateNumber())}</Number>" |
||
538 | . "<Store>{$this->xmlEncode($this->getAssociateStore())}</Store>" |
||
539 | . '</Associate>'; |
||
540 | } |
||
541 | return ''; |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * Serialize tax headers. |
||
546 | * |
||
547 | * @return string |
||
548 | */ |
||
549 | protected function serializeTaxHeader() |
||
550 | { |
||
551 | $taxHasErrors = $this->getTaxHasErrors(); |
||
552 | if (!is_null($taxHasErrors)) { |
||
553 | return "<TaxHeader><Error>{$this->convertBooleanToString($taxHasErrors)}</Error></TaxHeader>"; |
||
554 | } |
||
555 | return ''; |
||
556 | } |
||
557 | |||
558 | /** |
||
559 | * Serialize the order source and order source type. |
||
560 | * |
||
561 | * @return string |
||
562 | */ |
||
563 | protected function serializeOrderSource() |
||
564 | { |
||
565 | $orso = $this->getOrderSource(); |
||
566 | if (!is_null($orso)) { |
||
567 | return sprintf('<OrderSource type="%s">', $this->xmlEncode($this->getOrderSourceType())) |
||
568 | . $this->xmlEncode($orso) |
||
569 | . '</OrderSource>'; |
||
570 | } |
||
571 | return ''; |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * Serialize if the order uses vat inclusive pricing. |
||
576 | * |
||
577 | * @return string |
||
578 | */ |
||
579 | protected function serializeVatInclusivePricing() |
||
580 | { |
||
581 | $vatInclusive = $this->getVatInclusivePricing(); |
||
582 | if (!is_null($vatInclusive)) { |
||
583 | return "<VATInclusivePricing>{$this->convertBooleanToString($vatInclusive)}</VATInclusivePricing>"; |
||
584 | } |
||
585 | return ''; |
||
586 | } |
||
587 | |||
588 | protected function getRootAttributes() |
||
589 | { |
||
590 | return array_filter([ |
||
591 | 'orderType' => $this->getOrderType(), |
||
592 | 'requestId' => $this->getRequestId(), |
||
593 | 'testType' => $this->getTestType(), |
||
594 | 'xmlns' => $this->getXmlNamespace(), |
||
595 | ]); |
||
596 | } |
||
597 | |||
598 | protected function getPersonNameRootNodeName() |
||
602 | |||
603 | protected function getSchemaFile() |
||
607 | |||
608 | protected function getRootNodeName() |
||
612 | |||
613 | protected function getXmlNamespace() |
||
617 | } |
||
618 |
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..