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 |
||
21 | final class Checkout extends HttpApi |
||
22 | { |
||
23 | const SHIPPING_ADDRESS_FIELDS = [ |
||
24 | 'firstName', |
||
25 | 'lastName', |
||
26 | 'city', |
||
27 | 'postcode', |
||
28 | 'street', |
||
29 | 'countryCode', |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * @param int $id |
||
|
|||
34 | * |
||
35 | * @throws Exception |
||
36 | * |
||
37 | * @return bool |
||
38 | */ |
||
39 | public function putAddress(int $cartId, array $shippingAddress, bool $differentBillingAddress = false, array $billingAddress = []): bool |
||
78 | |||
79 | /** |
||
80 | * @param int $cartId |
||
81 | * @param string $paymentMethodCode |
||
82 | * |
||
83 | * @throws Exception\DomainException |
||
84 | * @throws Exception\Domain\ValidationException |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | public function putPaymentMethod(int $cartId, string $paymentMethodCode): bool |
||
123 | |||
124 | /** |
||
125 | * @param int $cartId |
||
126 | * |
||
127 | * @throws Exception\DomainException |
||
128 | * @throws Exception\Domain\ValidationException |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | View Code Duplication | public function complete(int $cartId) |
|
155 | |||
156 | /** |
||
157 | * @param int $cartId |
||
158 | * |
||
159 | * @throws Exception\DomainException |
||
160 | * @throws Exception\Domain\ValidationException |
||
161 | * |
||
162 | * @return ShipmentCollection |
||
163 | */ |
||
164 | View Code Duplication | public function getShippingMethods(int $cartId): ShipmentCollection |
|
187 | |||
188 | /** |
||
189 | * @param int $cartId |
||
190 | * |
||
191 | * @throws Exception\DomainException |
||
192 | * @throws Exception\Domain\ValidationException |
||
193 | * |
||
194 | * @return PaymentCollection |
||
195 | */ |
||
196 | View Code Duplication | public function getPaymentMethods(int $cartId): PaymentCollection |
|
219 | } |
||
220 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.