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 |
||
24 | class MercadoLibreClient |
||
25 | { |
||
26 | // Mercado Libre API URI. |
||
27 | const BASE_URI = 'https://api.mercadolibre.com'; |
||
28 | |||
29 | /** |
||
30 | * Guzzle Client |
||
31 | * |
||
32 | * @var GuzzleClient |
||
33 | */ |
||
34 | private $guzzleClient; |
||
35 | |||
36 | /** |
||
37 | * Access Token from MercadoLibre OAuth |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | private $access_token; |
||
42 | |||
43 | /** |
||
44 | * Serializer |
||
45 | * |
||
46 | * @var SerializerInterface |
||
47 | */ |
||
48 | private $serializer; |
||
49 | |||
50 | /** |
||
51 | * MercadoLibreClient constructor. |
||
52 | * |
||
53 | * @param array $config |
||
54 | * @param SerializerInterface $serializer |
||
55 | */ |
||
56 | 13 | public function __construct( |
|
66 | |||
67 | /** |
||
68 | * Get Guzzle client |
||
69 | * |
||
70 | * @return GuzzleClient |
||
71 | */ |
||
72 | 12 | public function getGuzzleClient() |
|
76 | |||
77 | /** |
||
78 | * @param string $access_token |
||
79 | * |
||
80 | * @return $this |
||
81 | */ |
||
82 | 7 | public function setAccessToken($access_token) |
|
87 | |||
88 | /** |
||
89 | * @return string |
||
90 | */ |
||
91 | 12 | public function getAccessToken() |
|
95 | |||
96 | /** |
||
97 | * Show User resource |
||
98 | * |
||
99 | * @param $customer_id |
||
100 | * |
||
101 | * @return array|\JMS\Serializer\scalar|object |
||
102 | */ |
||
103 | 6 | View Code Duplication | public function userShow($customer_id) |
113 | |||
114 | /** |
||
115 | * Show User me resource |
||
116 | * |
||
117 | * @return array|\JMS\Serializer\scalar|object |
||
118 | */ |
||
119 | 3 | View Code Duplication | public function userShowMe() |
129 | |||
130 | /** |
||
131 | * List Categories resource |
||
132 | * |
||
133 | * @param $site_id string |
||
134 | * |
||
135 | * @return array|\JMS\Serializer\scalar|object |
||
136 | */ |
||
137 | 2 | public function categoryList($site_id) |
|
148 | |||
149 | /** |
||
150 | * Set query |
||
151 | * |
||
152 | * @param array $query |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | 11 | private function setQuery(array $query = []) |
|
164 | } |
||
165 |
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.