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 StoredValueCardPayment implements IStoredValueCardPayment |
||
30 | { |
||
31 | use TPayload, TAmount, TCustomAttributeContainer, TCardPayment; |
||
32 | |||
33 | const ROOT_NODE = 'StoredValueCard'; |
||
34 | |||
35 | /** @var DateTime */ |
||
36 | protected $createTimeStamp; |
||
37 | /** @var float */ |
||
38 | protected $amount; |
||
39 | /** @var string */ |
||
40 | protected $pin; |
||
41 | |||
42 | /** |
||
43 | * @param IValidatorIterator |
||
44 | * @param ISchemaValidator |
||
45 | * @param IPayloadMap |
||
46 | * @param LoggerInterface |
||
47 | * @param IPayload |
||
48 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
49 | */ |
||
50 | public function __construct( |
||
51 | IValidatorIterator $validators, |
||
52 | ISchemaValidator $schemaValidator, |
||
53 | IPayloadMap $payloadMap, |
||
54 | LoggerInterface $logger, |
||
55 | IPayload $parentPayload = null |
||
56 | ) { |
||
57 | $this->logger = $logger; |
||
|
|||
58 | $this->validators = $validators; |
||
59 | $this->schemaValidator = $schemaValidator; |
||
60 | $this->payloadMap = $payloadMap; |
||
61 | $this->parentPayload = $parentPayload; |
||
62 | $this->payloadFactory = new PayloadFactory; |
||
63 | |||
64 | $this->extractionPaths = [ |
||
65 | 'orderId' => 'string(x:PaymentContext/x:PaymentSessionId)', |
||
66 | 'tenderType' => 'string(x:PaymentContext/x:TenderType)', |
||
67 | 'accountUniqueId' => 'string(x:PaymentContext/x:PaymentAccountUniqueId)', |
||
68 | 'paymentRequestId' => 'string(x:PaymentRequestId)', |
||
69 | ]; |
||
70 | $this->optionalExtractionPaths = [ |
||
71 | 'amount' => 'x:Amount', |
||
72 | 'pin' => 'x:Pin', |
||
73 | ]; |
||
74 | $this->booleanExtractionPaths = [ |
||
75 | 'isMockPayment' => 'string(@isMockPayment)', |
||
76 | 'panIsToken' => 'string(x:PaymentContext/x:PaymentAccountUniqueId/@isToken)' |
||
77 | ]; |
||
78 | $this->subpayloadExtractionPaths = [ |
||
79 | 'customAttributes' => 'x:CustomAttributes', |
||
80 | ]; |
||
81 | |||
82 | $this->customAttributes = $this->buildPayloadForInterface(self::CUSTOM_ATTRIBUTE_ITERABLE_INTERFACE); |
||
83 | } |
||
84 | |||
85 | public function getCreateTimeStamp() |
||
89 | |||
90 | public function setCreateTimeStamp(DateTime $createTimeStamp) |
||
91 | { |
||
92 | $this->createTimeStamp = $createTimeStamp; |
||
93 | return $this; |
||
94 | } |
||
95 | |||
96 | public function getAmount() |
||
100 | |||
101 | public function setAmount($amount) |
||
102 | { |
||
103 | $this->amount = $this->sanitizeAmount($amount); |
||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | public function getPin() |
||
111 | |||
112 | public function setPin($pin) |
||
113 | { |
||
114 | $this->pin = $this->cleanString($pin, 22); |
||
115 | return $this; |
||
116 | } |
||
117 | |||
118 | protected function serializeContents() |
||
119 | { |
||
120 | return $this->serializePaymentContext() |
||
121 | . $this->serializePaymentRequestId() |
||
122 | . "<CreateTimeStamp>{$this->getCreateTimeStamp()->format('c')}</CreateTimeStamp>" |
||
123 | . $this->serializeOptionalXmlEncodedValue('Pin', $this->getPin()) |
||
124 | . $this->serializeAmount('Amount', $this->getAmount()) |
||
125 | . $this->getCustomAttributes()->serialize(); |
||
126 | } |
||
127 | |||
128 | View Code Duplication | protected function deserializeExtra($serializedPayload) |
|
129 | { |
||
130 | $xpath = $this->getPayloadAsXPath($serializedPayload); |
||
131 | $createTimestampValue = $xpath->evaluate('string(x:CreateTimeStamp)'); |
||
132 | $this->createTimeStamp = $createTimestampValue ? new DateTime($createTimestampValue) : null; |
||
133 | return $this; |
||
134 | } |
||
135 | |||
136 | View Code Duplication | protected function getRootAttributes() |
|
137 | { |
||
138 | $isMockPayment = $this->getIsMockPayment(); |
||
139 | return !is_null($isMockPayment) |
||
140 | ? ['isMockPayment' => $this->convertBooleanToString($isMockPayment)] |
||
141 | : []; |
||
142 | } |
||
143 | |||
144 | protected function getRootNodeName() |
||
148 | |||
149 | protected function getXmlNamespace() |
||
153 | } |
||
154 |
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..