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 |
||
20 | class UpdateOrderShippingStatusRequest extends AbstractRequest |
||
21 | { |
||
22 | /** |
||
23 | * @return UpdateOrderShippingStatusApi |
||
24 | */ |
||
25 | public function api() |
||
29 | |||
30 | /** |
||
31 | * @return UpdateOrderShippingStatusResponse |
||
32 | */ |
||
33 | public function response() |
||
37 | |||
38 | /** |
||
39 | * @throws InvalidRequestException |
||
40 | */ |
||
41 | protected function validateParams(): void |
||
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getParams() |
||
72 | |||
73 | /** |
||
74 | * @param string $sellerId |
||
75 | * @return self |
||
76 | */ |
||
77 | View Code Duplication | public function setSellerId(string $sellerId): self |
|
87 | |||
88 | /** |
||
89 | * @param string $orderId |
||
90 | * @return self |
||
91 | */ |
||
92 | public function setOrderId(string $orderId): self |
||
93 | { |
||
94 | if (isset($this->params['Target']['OrderId'])) { |
||
95 | throw new LogicException('OrderId is already set.'); |
||
96 | } |
||
97 | |||
98 | $this->params['Target']['OrderId'] = $orderId; |
||
99 | |||
100 | return $this; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param bool $isPointFix |
||
105 | * @return self |
||
106 | */ |
||
107 | public function setIsPointFix(bool $isPointFix): self |
||
108 | { |
||
109 | if (isset($this->params['Target']['IsPointFix'])) { |
||
110 | throw new LogicException('IsPointFix is already set.'); |
||
111 | } |
||
112 | |||
113 | $this->params['Target']['IsPointFix'] = $isPointFix ? 'true' : 'false'; |
||
114 | |||
115 | return $this; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param string $operationUser |
||
120 | * @return self |
||
121 | */ |
||
122 | public function setOperationUser(string $operationUser): self |
||
123 | { |
||
124 | if (isset($this->params['Target']['OperationUser'])) { |
||
125 | throw new LogicException('OperationUser is already set.'); |
||
126 | } |
||
127 | |||
128 | $this->params['Target']['OperationUser'] = $operationUser; |
||
129 | |||
130 | return $this; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @param ShipStatus $shipStatus |
||
135 | * @return self |
||
136 | */ |
||
137 | public function setShipStatus(ShipStatus $shipStatus): self |
||
138 | { |
||
139 | if (isset($this->params['Order']['Ship']['ShipStatus'])) { |
||
140 | throw new LogicException('ShipStatus is already set.'); |
||
141 | } |
||
142 | |||
143 | $this->params['Order']['Ship']['ShipStatus'] = $shipStatus->getValue(); |
||
144 | |||
145 | return $this; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param ShipMethod $shipMethod |
||
150 | * @return self |
||
151 | */ |
||
152 | public function setShipMethod(ShipMethod $shipMethod): self |
||
153 | { |
||
154 | if (isset($this->params['Order']['Ship']['ShipMethod'])) { |
||
155 | throw new LogicException('ShipMethod is already set.'); |
||
156 | } |
||
157 | |||
158 | $this->params['Order']['Ship']['ShipMethod'] = $shipMethod->getValue(); |
||
159 | |||
160 | return $this; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param string $shipNotes |
||
165 | * @return self |
||
166 | */ |
||
167 | View Code Duplication | public function setShipNotes(string $shipNotes): self |
|
181 | |||
182 | /** |
||
183 | * @param string $shipInvoiceNumber1 |
||
184 | * @return self |
||
185 | */ |
||
186 | View Code Duplication | public function setShipInvoiceNumber1(string $shipInvoiceNumber1): self |
|
196 | |||
197 | /** |
||
198 | * @param string $shipInvoiceNumber2 |
||
199 | * @return self |
||
200 | */ |
||
201 | View Code Duplication | public function setShipInvoiceNumber2(string $shipInvoiceNumber2): self |
|
211 | |||
212 | /** |
||
213 | * @param string $shipUrl |
||
214 | * @return self |
||
215 | */ |
||
216 | View Code Duplication | public function setShipUrl(string $shipUrl): self |
|
226 | |||
227 | /** |
||
228 | * @param \DateTimeImmutable $shipDate |
||
229 | * @return self |
||
230 | */ |
||
231 | View Code Duplication | public function setShipDate(\DateTimeImmutable $shipDate): self |
|
243 | |||
244 | /** |
||
245 | * @param \DateTimeImmutable $arrivalDate |
||
246 | * @return self |
||
247 | */ |
||
248 | View Code Duplication | public function setArrivalDate(\DateTimeImmutable $arrivalDate): self |
|
260 | |||
261 | } |
||
262 |
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.