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 namespace DarkGold\Salesforce; |
||
7 | class Client |
||
8 | { |
||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | protected $salesforceLoginUrl; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $clientId; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $clientSecret; |
||
23 | |||
24 | /** |
||
25 | * @var AccessToken |
||
26 | */ |
||
27 | private $accessToken; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $baseUrl; |
||
33 | |||
34 | /** |
||
35 | * @var \GuzzleHttp\Client |
||
36 | */ |
||
37 | private $guzzleClient; |
||
38 | |||
39 | /** |
||
40 | * The version of the API we are going to be using. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $version = '43.0'; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * Create a sf client using a client config object or an array of params |
||
49 | * |
||
50 | * @param ClientConfigInterface $clientConfig |
||
51 | * @param \GuzzleHttp\Client $guzzleClient |
||
52 | * |
||
53 | * @throws \Exception |
||
54 | */ |
||
55 | public function __construct(ClientConfigInterface $clientConfig, \GuzzleHttp\Client $guzzleClient) |
||
63 | |||
64 | /** |
||
65 | * Create an instance of the salesforce client using the passed in config data |
||
66 | * |
||
67 | * @param $salesforceLoginUrl |
||
68 | * @param $clientId |
||
69 | * @param $clientSecret |
||
70 | * |
||
71 | * @return Client |
||
72 | * |
||
73 | * @throws \Exception |
||
74 | */ |
||
75 | public static function create($salesforceLoginUrl, $clientId, $clientSecret) |
||
79 | |||
80 | /** |
||
81 | * Fetch a specific object |
||
82 | * |
||
83 | * @param string $objectType |
||
84 | * @param string $sfId |
||
85 | * @param array $fields |
||
86 | * |
||
87 | * @return string |
||
88 | * |
||
89 | * @throws AuthenticationException |
||
90 | * @throws RequestException |
||
91 | */ |
||
92 | View Code Duplication | public function getRecord($objectType, $sfId, array $fields) |
|
99 | |||
100 | /** |
||
101 | * Execute an SOQL query and return the result set |
||
102 | * This will loop through large result sets collecting all the data so the query should be limited |
||
103 | * |
||
104 | * @param string|null $query |
||
105 | * @param string|null $next_url |
||
106 | * |
||
107 | * @return array |
||
108 | * |
||
109 | * @throws \Exception |
||
110 | */ |
||
111 | public function search($query = null, $next_url = null) |
||
131 | |||
132 | /** |
||
133 | * Get the details of an object. Includes the individual metadata at all levels for the object. |
||
134 | * |
||
135 | * @param $objectName |
||
136 | * |
||
137 | * @return array |
||
138 | * |
||
139 | * @throws AuthenticationException |
||
140 | * @throws RequestException |
||
141 | */ |
||
142 | View Code Duplication | public function describe($objectName) |
|
150 | |||
151 | /** |
||
152 | * Make an update request |
||
153 | * |
||
154 | * @param string $object The object type to update |
||
155 | * @param string $id The ID of the record to update |
||
156 | * @param array $data The data to put into the record |
||
157 | * |
||
158 | * @return bool |
||
159 | * |
||
160 | * @throws \Exception |
||
161 | */ |
||
162 | public function updateRecord($object, $id, array $data) |
||
173 | |||
174 | /** |
||
175 | * Create a new object in salesforce |
||
176 | * |
||
177 | * @param string $object |
||
178 | * @param array $data |
||
179 | * @return bool |
||
180 | * @throws \Exception |
||
181 | */ |
||
182 | public function createRecord($object, array $data) |
||
194 | |||
195 | /** |
||
196 | * Delete an object with th specified id |
||
197 | * |
||
198 | * @param $object |
||
199 | * @param $id |
||
200 | * |
||
201 | * @return bool |
||
202 | * |
||
203 | * @throws \Exception |
||
204 | */ |
||
205 | public function deleteRecord($object, $id) |
||
213 | |||
214 | /** |
||
215 | * Complete the oauth process by confirming the code and returning an access token |
||
216 | * |
||
217 | * @param $code |
||
218 | * @param $redirect_url |
||
219 | * |
||
220 | * @return array|mixed |
||
221 | * |
||
222 | * @throws \Exception |
||
223 | */ |
||
224 | public function authorizeConfirm($code, $redirect_url) |
||
240 | |||
241 | /** |
||
242 | * Get the url to redirect users to when setting up a salesforce access token |
||
243 | * |
||
244 | * @param $redirectUrl |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | public function getLoginUrl($redirectUrl) |
||
259 | |||
260 | /** |
||
261 | * Refresh an existing access token |
||
262 | * |
||
263 | * @return AccessToken |
||
264 | * |
||
265 | * @throws \Exception |
||
266 | */ |
||
267 | public function refreshToken() |
||
285 | |||
286 | /** |
||
287 | * @param AccessToken $accessToken |
||
288 | */ |
||
289 | public function setAccessToken(AccessToken $accessToken) |
||
294 | |||
295 | /** |
||
296 | * @param string $method |
||
297 | * @param string $url |
||
298 | * @param array $data |
||
299 | * |
||
300 | * @return mixed |
||
301 | * |
||
302 | * @throws AuthenticationException |
||
303 | * @throws RequestException |
||
304 | */ |
||
305 | private function makeRequest($method, $url, $data) |
||
326 | |||
327 | /** |
||
328 | * @return string |
||
329 | * |
||
330 | * @throws AuthenticationException |
||
331 | */ |
||
332 | private function getAuthHeader() |
||
340 | |||
341 | } |
||
342 |
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.