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 UpdateOrderShippingStatusRequest extends AbstractRequest |
||
19 | { |
||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $params = []; |
||
24 | |||
25 | /** |
||
26 | * @param string $sellerId |
||
27 | * @return self |
||
28 | */ |
||
29 | View Code Duplication | public function setSellerId(string $sellerId): self |
|
|
|||
30 | { |
||
31 | if (isset($this->params['SellerId'])) { |
||
32 | throw new LogicException('SellerId is already set.'); |
||
33 | } |
||
34 | |||
35 | $this->params['SellerId'] = $sellerId; |
||
36 | |||
37 | return $this; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param string $orderId |
||
42 | * @return self |
||
43 | */ |
||
44 | View Code Duplication | public function setOrderId(string $orderId): self |
|
45 | { |
||
46 | if (isset($this->params['Target']['OrderId'])) { |
||
47 | throw new LogicException('OrderId is already set.'); |
||
48 | } |
||
49 | |||
50 | $this->params['Target']['OrderId'] = $orderId; |
||
51 | |||
52 | return $this; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param bool $isPointFix |
||
57 | * @return self |
||
58 | */ |
||
59 | View Code Duplication | public function setIsPointFix(bool $isPointFix): self |
|
60 | { |
||
61 | if (isset($this->params['Target']['IsPointFix'])) { |
||
62 | throw new LogicException('IsPointFix is already set.'); |
||
63 | } |
||
64 | |||
65 | $this->params['Target']['IsPointFix'] = $isPointFix ? 'true' : 'false'; |
||
66 | |||
67 | return $this; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param string $operationUser |
||
72 | * @return self |
||
73 | */ |
||
74 | View Code Duplication | public function setOperationUser(string $operationUser): self |
|
75 | { |
||
76 | if (isset($this->params['Target']['OperationUser'])) { |
||
77 | throw new LogicException('OperationUser is already set.'); |
||
78 | } |
||
79 | |||
80 | $this->params['Target']['OperationUser'] = $operationUser; |
||
81 | |||
82 | return $this; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param ShipStatus $shipStatus |
||
87 | * @return self |
||
88 | */ |
||
89 | View Code Duplication | public function setShipStatus(ShipStatus $shipStatus): self |
|
90 | { |
||
91 | if (isset($this->params['Order']['Ship']['ShipStatus'])) { |
||
92 | throw new LogicException('ShipStatus is already set.'); |
||
93 | } |
||
94 | |||
95 | $this->params['Order']['Ship']['ShipStatus'] = $shipStatus->getValue(); |
||
96 | |||
97 | return $this; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param ShipMethod $shipMethod |
||
102 | * @return self |
||
103 | */ |
||
104 | View Code Duplication | public function setShipMethod(ShipMethod $shipMethod): self |
|
105 | { |
||
106 | if (isset($this->params['Order']['Ship']['ShipMethod'])) { |
||
107 | throw new LogicException('ShipMethod is already set.'); |
||
108 | } |
||
109 | |||
110 | $this->params['Order']['Ship']['ShipMethod'] = $shipMethod->getValue(); |
||
111 | |||
112 | return $this; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param string $shipNotes |
||
117 | * @return self |
||
118 | */ |
||
119 | View Code Duplication | public function setShipNotes(string $shipNotes): self |
|
133 | |||
134 | /** |
||
135 | * @param string $shipInvoiceNumber1 |
||
136 | * @return self |
||
137 | */ |
||
138 | View Code Duplication | public function setShipInvoiceNumber1(string $shipInvoiceNumber1): self |
|
148 | |||
149 | /** |
||
150 | * @param string $shipInvoiceNumber2 |
||
151 | * @return self |
||
152 | */ |
||
153 | View Code Duplication | public function setShipInvoiceNumber2(string $shipInvoiceNumber2): self |
|
163 | |||
164 | /** |
||
165 | * @param string $shipUrl |
||
166 | * @return self |
||
167 | */ |
||
168 | View Code Duplication | public function setShipUrl(string $shipUrl): self |
|
178 | |||
179 | /** |
||
180 | * @param \DateTimeImmutable $shipDate |
||
181 | * @return self |
||
182 | */ |
||
183 | View Code Duplication | public function setShipDate(\DateTimeImmutable $shipDate): self |
|
195 | |||
196 | /** |
||
197 | * @param \DateTimeImmutable $arrivalDate |
||
198 | * @return self |
||
199 | */ |
||
200 | View Code Duplication | public function setArrivalDate(\DateTimeImmutable $arrivalDate): self |
|
212 | |||
213 | /** |
||
214 | * @throws InvalidRequestException |
||
215 | */ |
||
216 | protected function validateParams(): void |
||
217 | { |
||
218 | if (!isset($this->params['SellerId'])) { |
||
234 | |||
235 | /** |
||
236 | * @return UpdateOrderShippingStatusApi |
||
237 | */ |
||
238 | public function api() |
||
242 | |||
243 | /** |
||
244 | * @return UpdateOrderShippingStatusResponse |
||
245 | */ |
||
246 | public function response() |
||
250 | |||
251 | } |
||
252 |
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.