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 PayPalPayment implements IPayPalPayment |
||
| 30 | { |
||
| 31 | use TPayload, TAmount, TCustomAttributeContainer, TPaymentContext; |
||
| 32 | |||
| 33 | const ROOT_NODE = 'PayPal'; |
||
| 34 | |||
| 35 | /** @var float */ |
||
| 36 | protected $amount; |
||
| 37 | /** @var float */ |
||
| 38 | protected $amountAuthorized; |
||
| 39 | /** @Datvar DateTime */ |
||
| 40 | protected $createTimeStamp; |
||
| 41 | /** @svar string */ |
||
| 42 | protected $authorizationResponseCode; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param IValidatorIterator |
||
| 46 | * @param ISchemaValidator |
||
| 47 | * @param IPayloadMap |
||
| 48 | * @param LoggerInterface |
||
| 49 | * @param IPayload |
||
| 50 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 51 | */ |
||
| 52 | View Code Duplication | 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 | 'amount' => 'string(x:Amount)', |
||
| 67 | 'amountAuthorized' => 'string(x:AmountAuthorized)', |
||
| 68 | 'authorizationResponseCode' => 'string(x:Authorization/x:ResponseCode)', |
||
| 69 | 'orderId' => 'string(x:PaymentContext/x:PaymentSessionId)', |
||
| 70 | 'tenderType' => 'string(x:PaymentContext/x:TenderType)', |
||
| 71 | 'accountUniqueId' => 'string(x:PaymentContext/x:PaymentAccountUniqueId)', |
||
| 72 | 'paymentRequestId' => 'string(x:PaymentRequestId)', |
||
| 73 | ]; |
||
| 74 | $this->booleanExtractionPaths = [ |
||
| 75 | 'panIsToken' => 'string(x:PaymentContext/x:PaymentAccountUniqueId/@isToken)', |
||
| 76 | ]; |
||
| 77 | $this->subpayloadExtractionPaths = [ |
||
| 78 | 'customAttributes' => 'x:CustomAttributes', |
||
| 79 | ]; |
||
| 80 | |||
| 81 | $this->customAttributes = $this->buildPayloadForInterface(self::CUSTOM_ATTRIBUTE_ITERABLE_INTERFACE); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function getAmount() |
||
| 88 | |||
| 89 | public function setAmount($amount) |
||
| 90 | { |
||
| 91 | $this->amount = $this->sanitizeAmount($amount); |
||
| 92 | return $this; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function getAmountAuthorized() |
||
| 99 | |||
| 100 | public function setAmountAuthorized($amountAuthorized) |
||
| 101 | { |
||
| 102 | $this->amountAuthorized = $this->sanitizeAmount($amountAuthorized); |
||
| 103 | return $this; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function getCreateTimeStamp() |
||
| 110 | |||
| 111 | public function setCreateTimeStamp(DateTime $createTimeStamp) |
||
| 112 | { |
||
| 113 | $this->createTimeStamp = $createTimeStamp; |
||
| 114 | return $this; |
||
| 115 | } |
||
| 116 | |||
| 117 | public function getAuthorizationResponseCode() |
||
| 121 | |||
| 122 | public function setAuthorizationResponseCode($authorizationResponseCode) |
||
| 123 | { |
||
| 124 | $this->authorizationResponseCode = $authorizationResponseCode; |
||
| 125 | return $this; |
||
| 126 | } |
||
| 127 | |||
| 128 | protected function serializeContents() |
||
| 129 | { |
||
| 130 | return $this->serializeAmount('Amount', $this->getAmount()) |
||
| 131 | . $this->serializeAmount('AmountAuthorized', $this->getAmountAuthorized()) |
||
| 132 | . $this->serializePaymentContext() |
||
| 133 | . "<CreateTimeStamp>{$this->getCreateTimeStamp()->format('c')}</CreateTimeStamp>" |
||
| 134 | . $this->serializePaymentRequestId() |
||
| 135 | . "<Authorization><ResponseCode>{$this->xmlEncode($this->getAuthorizationResponseCode())}</ResponseCode></Authorization>" |
||
| 136 | . $this->getCustomAttributes()->serialize(); |
||
| 137 | } |
||
| 138 | |||
| 139 | View Code Duplication | protected function deserializeExtra($serializedPayload) |
|
| 140 | { |
||
| 141 | $xpath = $this->getPayloadAsXPath($serializedPayload); |
||
| 142 | $createTimestampValue = $xpath->evaluate('string(x:CreateTimeStamp)'); |
||
| 143 | $this->createTimeStamp = $createTimestampValue ? new DateTime($createTimestampValue) : null; |
||
| 144 | return $this; |
||
| 145 | } |
||
| 146 | |||
| 147 | protected function getRootNodeName() |
||
| 151 | |||
| 152 | protected function getXmlNamespace() |
||
| 156 | } |
||
| 157 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.