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 |
||
19 | View Code Duplication | class AddressValidationWrapper |
|
|
|||
20 | { |
||
21 | /** |
||
22 | * @var \Ups\AddressValidation |
||
23 | */ |
||
24 | private $upsAddressValidation; |
||
25 | |||
26 | /** |
||
27 | * Shipping constructor. |
||
28 | * |
||
29 | * @param AddressValidation $upsAddressValidation |
||
30 | */ |
||
31 | public function __construct(AddressValidation $upsAddressValidation) |
||
35 | |||
36 | /** |
||
37 | * Get address suggestions from UPS. |
||
38 | * |
||
39 | * @param $address |
||
40 | * @param int $requestOption |
||
41 | * @param int $maxSuggestion |
||
42 | * |
||
43 | * @throws Exception |
||
44 | * |
||
45 | * @return \StdClass |
||
46 | */ |
||
47 | public function validate( |
||
54 | |||
55 | /** |
||
56 | * @return RequestInterface |
||
57 | */ |
||
58 | public function getRequest() |
||
64 | |||
65 | /** |
||
66 | * @param RequestInterface $request |
||
67 | * |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function setRequest(RequestInterface $request) |
||
76 | |||
77 | /** |
||
78 | * @return ResponseInterface |
||
79 | */ |
||
80 | public function getResponse() |
||
84 | |||
85 | /** |
||
86 | * @param ResponseInterface $response |
||
87 | * |
||
88 | * @return $this |
||
89 | */ |
||
90 | public function setResponse(ResponseInterface $response) |
||
96 | } |
||
97 |
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.