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 |
||
18 | class SearchOrdersRequest extends AbstractRequest |
||
19 | { |
||
20 | |||
21 | public function __construct() |
||
101 | |||
102 | /** |
||
103 | * @return AbstractApi |
||
104 | */ |
||
105 | public function api() |
||
109 | |||
110 | /** |
||
111 | * @return AbstractResponse |
||
112 | */ |
||
113 | public function response() |
||
117 | |||
118 | /** |
||
119 | * @return void |
||
120 | */ |
||
121 | protected function validateParams() |
||
135 | |||
136 | |||
137 | /** |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getParams() |
||
149 | |||
150 | /** |
||
151 | * @param string $orderId |
||
152 | * @return self |
||
153 | */ |
||
154 | public function setOrderId(string $orderId): self |
||
155 | { |
||
156 | if (isset($this->params['Search']['Condition']['OrderId'])) { |
||
157 | throw new LogicException('OrderId is already set.'); |
||
158 | } |
||
159 | $this->params['Search']['Condition']['OrderId'] = $orderId; |
||
160 | |||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param string $sellerId |
||
166 | * @return self |
||
167 | */ |
||
168 | View Code Duplication | public function setSellerId(string $sellerId): self |
|
178 | |||
179 | /** |
||
180 | * @param bool $isSeen |
||
181 | * @return self |
||
182 | */ |
||
183 | public function setIsSeen(bool $isSeen): self |
||
184 | { |
||
185 | if (isset($this->params['Search']['Condition']['IsSeen'])) { |
||
186 | throw new LogicException('IsSeen is already set.'); |
||
187 | } |
||
188 | $this->params['Search']['Condition']['IsSeen'] = $isSeen ? 'true' : 'false'; |
||
189 | |||
190 | return $this; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param null|DateTimeImmutable $from |
||
195 | * @param null|DateTimeImmutable $to |
||
196 | * @return self |
||
197 | */ |
||
198 | public function setOrderedDateTimeRange(DateTimeImmutable $from = null, DateTimeImmutable $to = null): self |
||
229 | |||
230 | /** |
||
231 | * @param OrderStatus $orderStatus |
||
232 | * @return self |
||
233 | */ |
||
234 | public function setOrderStatus(OrderStatus $orderStatus): self |
||
235 | { |
||
236 | if (isset($this->params['Search']['Condition']['OrderStatus'])) { |
||
237 | throw new LogicException('OrderStatus is already set.'); |
||
238 | } |
||
239 | $this->params['Search']['Condition']['OrderStatus'] = $orderStatus->getValue(); |
||
240 | |||
241 | return $this; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @param ShipStatus $shipStatus |
||
246 | * @return self |
||
247 | */ |
||
248 | public function setShipStatus(ShipStatus $shipStatus): self |
||
249 | { |
||
250 | if (isset($this->params['Search']['Condition']['ShipStatus'])) { |
||
251 | throw new LogicException('ShipStatus is already set.'); |
||
252 | } |
||
253 | $this->params['Search']['Condition']['ShipStatus'] = $shipStatus->getValue(); |
||
254 | |||
255 | return $this; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param StoreStatus $shipStatus |
||
260 | * @return self |
||
261 | */ |
||
262 | public function setStoreStatus(StoreStatus $shipStatus): self |
||
271 | |||
272 | /** |
||
273 | * @param int $offset |
||
274 | * @return self |
||
275 | */ |
||
276 | View Code Duplication | public function setOffset(int $offset): self |
|
290 | |||
291 | /** |
||
292 | * @param int $limit |
||
293 | * @return self |
||
294 | */ |
||
295 | View Code Duplication | public function setLimit(int $limit): self |
|
309 | |||
310 | } |
||
311 |
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.