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 |
||
23 | class MercadoLibreClient |
||
24 | { |
||
25 | // Mercado Libre API URI. |
||
26 | const BASE_URI = 'https://api.mercadolibre.com'; |
||
27 | |||
28 | /** |
||
29 | * Guzzle Client |
||
30 | * |
||
31 | * @var GuzzleClient |
||
32 | */ |
||
33 | private $guzzleClient; |
||
34 | |||
35 | /** |
||
36 | * Access Token from MercadoLibre OAuth |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $access_token; |
||
41 | |||
42 | /** |
||
43 | * Serializer |
||
44 | * |
||
45 | * @var SerializerInterface |
||
46 | */ |
||
47 | private $serializer; |
||
48 | |||
49 | /** |
||
50 | * MercadoLibreClient constructor. |
||
51 | * |
||
52 | * @param array $config |
||
53 | * @param SerializerInterface $serializer |
||
54 | */ |
||
55 | 11 | public function __construct( |
|
65 | |||
66 | /** |
||
67 | * Get Guzzle client |
||
68 | * |
||
69 | * @return GuzzleClient |
||
70 | */ |
||
71 | 10 | public function getGuzzleClient() |
|
75 | |||
76 | /** |
||
77 | * @param string $access_token |
||
78 | * |
||
79 | * @return $this |
||
80 | */ |
||
81 | 7 | public function setAccessToken($access_token) |
|
86 | |||
87 | /** |
||
88 | * @return string |
||
89 | */ |
||
90 | 10 | public function getAccessToken() |
|
94 | |||
95 | /** |
||
96 | * Show User resource |
||
97 | * |
||
98 | * @param $customer_id |
||
99 | * |
||
100 | * @return object |
||
101 | */ |
||
102 | 6 | View Code Duplication | public function showUser($customer_id) |
112 | |||
113 | 3 | View Code Duplication | public function showUserMe() |
123 | |||
124 | /** |
||
125 | * Set query |
||
126 | * |
||
127 | * @param array $query |
||
128 | * |
||
129 | * @return array |
||
130 | */ |
||
131 | 9 | private function setQuery(array $query = []) |
|
139 | } |
||
140 |
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.