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 |
||
6 | class Order extends Endpoint |
||
7 | { |
||
8 | /** |
||
9 | * @param \DateTimeInterface $dateStart |
||
10 | * @param \DateTimeInterface $dateEnd |
||
11 | * @param int|null $orderStateId |
||
12 | * @return int |
||
13 | */ |
||
14 | public function countByModifiedInterval(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, int $orderStateId = null) : int |
||
31 | |||
32 | /** |
||
33 | * @param array|\stdClass $order |
||
34 | * @return array |
||
35 | */ |
||
36 | public function createOrder($order) : array |
||
40 | |||
41 | /** |
||
42 | * @param \DateTimeInterface $dateStart |
||
43 | * @param \DateTimeInterface $dateEnd |
||
44 | * @return array |
||
45 | */ |
||
46 | public function getOrders(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd) : array |
||
59 | |||
60 | /** |
||
61 | * @param int $orderId |
||
62 | * @return array |
||
63 | */ |
||
64 | public function getOrder(int $orderId) : array |
||
70 | |||
71 | /** |
||
72 | * Deletes an order |
||
73 | * |
||
74 | * @param int $orderId |
||
75 | * @return bool |
||
76 | */ |
||
77 | public function deleteOrder(int $orderId) : bool |
||
83 | |||
84 | /** |
||
85 | * @param int $orderId |
||
86 | * @return bool |
||
87 | */ |
||
88 | public function completeOrder(int $orderId) : bool |
||
97 | |||
98 | /** |
||
99 | * @param int $customerNumber |
||
100 | * @return array |
||
101 | */ |
||
102 | public function getOrdersByCustomerNumber(int $customerNumber) : array |
||
111 | |||
112 | /** |
||
113 | * @param \DateTimeInterface $dateStart |
||
114 | * @param \DateTimeInterface $dateEnd |
||
115 | * @param int $page |
||
116 | * @param int $pageSize |
||
117 | * @param int|null $orderStateId |
||
118 | * @return array |
||
119 | */ |
||
120 | public function getOrdersInModifiedInterval(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, int $page = 1, int $pageSize = 100, int $orderStateId = null) : array |
||
141 | |||
142 | /** |
||
143 | * @return array |
||
144 | */ |
||
145 | public function getOrderStates() : array |
||
149 | |||
150 | /** |
||
151 | * @param int $orderId |
||
152 | * @param string $comment |
||
153 | * @return bool |
||
154 | */ |
||
155 | View Code Duplication | public function setOrderComment(int $orderId, string $comment) : bool |
|
169 | |||
170 | /** |
||
171 | * @param int $orderId |
||
172 | * @param int $orderState |
||
173 | * @return bool |
||
174 | */ |
||
175 | View Code Duplication | public function setOrderState(int $orderId, int $orderState) : bool |
|
185 | |||
186 | /** |
||
187 | * @param int $orderId |
||
188 | * @param string $trackingNumber |
||
189 | * @return bool |
||
190 | */ |
||
191 | View Code Duplication | public function setTrackNumber(int $orderId, string $trackingNumber) : bool |
|
205 | |||
206 | /****************** |
||
207 | * Helper methods * |
||
208 | *****************/ |
||
209 | /** |
||
210 | * Will copy an order based on the order id |
||
211 | * If the $orderLines is empty, it will also copy the order lines |
||
212 | * If the $orderState is > 0, the method will update the order state |
||
213 | * If $completeOrder is true, the method will also complete the order, otherwise it will be marked as incomplete by default |
||
214 | * Returns the new order |
||
215 | * |
||
216 | * @param int $orderId |
||
217 | * @param array $orderLines |
||
218 | * @param int $orderState |
||
219 | * @param boolean $completeOrder |
||
220 | * @return array |
||
221 | */ |
||
222 | public function copyOrder(int $orderId, array $orderLines = [], int $orderState = 0, bool $completeOrder = true) : array |
||
255 | } |
||
256 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: