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 OrderCreditIssued implements IOrderCreditIssued |
||
| 29 | { |
||
| 30 | use TTopLevelPayload, TOrderEvent, TCurrency, TLoyaltyProgramCustomer, TOrderItemContainer, TReturnSummary, TAmount; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param IValidatorIterator |
||
| 34 | * @param ISchemaValidator |
||
| 35 | * @param IPayloadMap |
||
| 36 | * @param LoggerInterface |
||
| 37 | * @param IPayload |
||
| 38 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 39 | */ |
||
| 40 | public function __construct( |
||
| 41 | IValidatorIterator $validators, |
||
| 42 | ISchemaValidator $schemaValidator, |
||
| 43 | IPayloadMap $payloadMap, |
||
| 44 | LoggerInterface $logger, |
||
| 45 | IPayload $parentPayload = null |
||
| 46 | ) { |
||
| 47 | $this->logger = $logger; |
||
|
|
|||
| 48 | $this->validators = $validators; |
||
| 49 | $this->schemaValidator = $schemaValidator; |
||
| 50 | $this->payloadMap = $payloadMap; |
||
| 51 | $this->parentPayload = $parentPayload; |
||
| 52 | $this->payloadFactory = new PayloadFactory(); |
||
| 53 | |||
| 54 | $this->loyaltyPrograms = |
||
| 55 | $this->buildPayloadForInterface(static::LOYALTY_PROGRAM_ITERABLE_INTERFACE); |
||
| 56 | $this->orderItems = |
||
| 57 | $this->buildPayloadForInterface(static::ORDER_ITEM_ITERABLE_INTERFACE); |
||
| 58 | |||
| 59 | $this->extractionPaths = [ |
||
| 60 | 'storeId' => 'string(@storeId)', |
||
| 61 | 'currencyCode' => 'string(@currency)', |
||
| 62 | 'currencySymbol' => 'string(@currencySymbol)', |
||
| 63 | 'customerFirstName' => 'string(x:Customer/x:Name/x:FirstName)', |
||
| 64 | 'customerLastName' => 'string(x:Customer/x:Name/x:LastName)', |
||
| 65 | 'customerEmailAddress' => 'string(x:Customer/x:Name/x:EmailAddress)', |
||
| 66 | 'orderId' => 'string(@customerOrderId)', |
||
| 67 | 'returnOrCredit' => 'string(x:CreditIssuedSummary/@returnOrCreditIssued)', |
||
| 68 | 'referenceNumber' => 'string(x:CreditIssuedSummary/@creditRefNumber)', |
||
| 69 | 'totalCredit' => 'string(x:CreditIssuedSummary/@totalCredit)' |
||
| 70 | ]; |
||
| 71 | |||
| 72 | $this->optionalExtractionPaths = [ |
||
| 73 | 'customerId' => 'x:Customer/@customerId', |
||
| 74 | 'customerMiddleName' => 'x:Customer/x:Name/x:MiddleName', |
||
| 75 | 'customerHonorificName' => 'x:Customer/x:Name/x:Honorific', |
||
| 76 | 'customerEmailAddress' => 'x:Customer/x:EmailAddress' |
||
| 77 | ]; |
||
| 78 | |||
| 79 | $this->subpayloadExtractionPaths = [ |
||
| 80 | 'loyaltyPrograms' => 'x:Customer/x:LoyaltyPrograms', |
||
| 81 | 'orderItems' => 'x:AdjustedOrderItems' |
||
| 82 | ]; |
||
| 83 | } |
||
| 84 | |||
| 85 | View Code Duplication | protected function getRootAttributes() |
|
| 86 | { |
||
| 87 | return [ |
||
| 88 | 'xmlns' => $this->getXmlNamespace(), |
||
| 89 | 'customerOrderId' => $this->getCustomerOrderId(), |
||
| 90 | 'storeId' => $this->getStoreId(), |
||
| 91 | 'currency' => $this->getCurrencyCode(), |
||
| 92 | 'currencySymbol' => $this->getCurrencySymbol() |
||
| 93 | ]; |
||
| 94 | } |
||
| 95 | |||
| 96 | public function getEventType() |
||
| 100 | |||
| 101 | protected function getSchemaFile() |
||
| 105 | |||
| 106 | protected function getXmlNamespace() |
||
| 110 | |||
| 111 | protected function getRootNodeName() |
||
| 115 | |||
| 116 | protected function serializeContents() |
||
| 117 | { |
||
| 122 | |||
| 123 | protected function serializeOrderSummary() |
||
| 133 | } |
||
| 134 |
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..