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 |
||
32 | class InStorePickUpItem implements IInStorePickUpItem |
||
33 | { |
||
34 | use TPayload, TItem, TAmount, TOrderItem, TPhysicalAddress { |
||
35 | TPhysicalAddress::getLines as getAddressLines; |
||
36 | TPhysicalAddress::setLines as setAddressLines; |
||
37 | TPhysicalAddress::getCity as getAddressCity; |
||
38 | TPhysicalAddress::setCity as setAddressCity; |
||
39 | TPhysicalAddress::getMainDivision as getAddressMainDivision; |
||
40 | TPhysicalAddress::setMainDivision as setAddressMainDivision; |
||
41 | TPhysicalAddress::getCountryCode as getAddressCountryCode; |
||
42 | TPhysicalAddress::setCountryCode as setAddressCountryCode; |
||
43 | TPhysicalAddress::getPostalCode as getAddressPostalCode; |
||
44 | TPhysicalAddress::setPostalCode as setAddressPostalCode; |
||
45 | } |
||
46 | |||
47 | const ROOT_NODE = 'OrderItem'; |
||
48 | |||
49 | /** |
||
50 | * @param IValidatorIterator |
||
51 | * @param ISchemaValidator |
||
52 | * @param IPayloadMap |
||
53 | * @param LoggerInterface |
||
54 | * @param IPayload |
||
55 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
56 | */ |
||
57 | View Code Duplication | public function __construct( |
|
|
|||
58 | IValidatorIterator $validators, |
||
59 | ISchemaValidator $schemaValidator, |
||
60 | IPayloadMap $payloadMap, |
||
61 | LoggerInterface $logger, |
||
62 | IPayload $parentPayload = null |
||
63 | ) { |
||
64 | $this->logger = $logger; |
||
65 | $this->validators = $validators; |
||
66 | $this->schemaValidator = $schemaValidator; |
||
67 | $this->payloadMap = $payloadMap; |
||
68 | $this->parentPayload = $parentPayload; |
||
69 | $this->payloadFactory = new PayloadFactory; |
||
70 | |||
71 | $this->extractionPaths = [ |
||
72 | 'itemId' => 'string(@itemId)', |
||
73 | 'id' => 'string(@lineId)', |
||
74 | 'quantity' => 'string(x:Quantity)', |
||
75 | 'storeFrontId' => 'string(x:InStorePickupDetails/x:StoreFrontId)', |
||
76 | 'storeFrontName' => 'string(x:InStorePickupDetails/x:StoreFrontName)', |
||
77 | 'city' => 'string(x:InStorePickupDetails/x:StoreFrontAddress/x:City)', |
||
78 | 'countryCode' => 'string(x:InStorePickupDetails/x:StoreFrontAddress/x:CountryCode)', |
||
79 | 'shippingMethodCarrierType' => 'string(x:InStorePickupDetails/x:StoreFrontAddress)', |
||
80 | ]; |
||
81 | $this->optionalExtractionPaths = [ |
||
82 | 'mainDivision' => 'x:InStorePickupDetails/x:StoreFrontAddress/x:MainDivision', |
||
83 | 'postalCode' => 'x:InStorePickupDetails/x:StoreFrontAddress/x:PostalCode', |
||
84 | ]; |
||
85 | $this->booleanExtractionPaths = [ |
||
86 | 'giftWrapRequested' => 'string(x:GiftwrapRequested)', |
||
87 | ]; |
||
88 | $this->addressLinesExtractionMap = [ |
||
89 | [ |
||
90 | 'property' => 'lines', |
||
91 | 'xPath' => 'x:InStorePickupDetails/x:StoreFrontAddress/*[starts-with(name(), "Line")]' |
||
92 | ], |
||
93 | ]; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * This is the identifier of the store in which the line item will be picked up. |
||
98 | * |
||
99 | * restrictions: length <= 100 |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getStoreFrontId() |
||
106 | |||
107 | /** |
||
108 | * @param string |
||
109 | * @return self |
||
110 | */ |
||
111 | public function setStoreFrontId($id) |
||
112 | { |
||
113 | $this->id = $this->cleanString($id, 100); |
||
114 | return $this; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Store Name |
||
119 | * |
||
120 | * restrictions: length <= 100 |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getStoreFrontName() |
||
127 | |||
128 | /** |
||
129 | * @param string |
||
130 | * @return self |
||
131 | */ |
||
132 | public function setStoreFrontName($name) |
||
133 | { |
||
134 | $this->name = $this->cleanString($name, 100); |
||
135 | return $this; |
||
136 | } |
||
137 | |||
138 | protected function serializeContents() |
||
139 | { |
||
140 | return $this->serializeQuantity() |
||
141 | . "<InStorePickupDetails>" |
||
142 | . "<StoreFrontId>{$this->xmlEncode($this->getStoreFrontId())}</StoreFrontId>" |
||
143 | . "<StoreFrontName>{$this->xmlEncode($this->getStoreFrontName())}</StoreFrontName>" |
||
144 | . $this->serializePhysicalAddress() |
||
145 | . "</InStorePickupDetails>" |
||
146 | . $this->serializeGiftWrapRequested(); |
||
147 | } |
||
148 | |||
149 | protected function getRootAttributes() |
||
150 | { |
||
151 | return [ |
||
152 | 'itemId' => $this->getItemId(), |
||
153 | 'lineId' => $this->getId(), |
||
154 | ]; |
||
155 | } |
||
156 | |||
157 | protected function getPhysicalAddressRootNodeName() |
||
161 | } |
||
162 |
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.