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 Crunch\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 | * @const Salesforce API version |
||
| 41 | */ |
||
| 42 | const SALESFORCE_API_VERSION = 'v43.0'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Create a sf client using a client config object or an array of params |
||
| 46 | * |
||
| 47 | * @param ClientConfigInterface $clientConfig |
||
| 48 | * @param \GuzzleHttp\Client $guzzleClient |
||
| 49 | * @throws \Exception |
||
| 50 | */ |
||
| 51 | public function __construct(ClientConfigInterface $clientConfig, \GuzzleHttp\Client $guzzleClient) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Create an instance of the salesforce client using the passed in config data |
||
| 62 | * |
||
| 63 | * @param $salesforceLoginUrl |
||
| 64 | * @param $clientId |
||
| 65 | * @param $clientSecret |
||
| 66 | * @return Client |
||
| 67 | */ |
||
| 68 | public static function create($salesforceLoginUrl, $clientId, $clientSecret) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Fetch a specific object |
||
| 75 | * |
||
| 76 | * @param string $objectType |
||
| 77 | * @param string $sfId |
||
| 78 | * @param array $fields |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | public function getRecord($objectType, $sfId, array $fields) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Execute an SOQL query and return the result set |
||
| 91 | * This will loop through large result sets collecting all the data so the query should be limited |
||
| 92 | * |
||
| 93 | * @param string|null $query |
||
| 94 | * @param string|null $next_url |
||
| 95 | * @return array |
||
| 96 | * @throws \Exception |
||
| 97 | */ |
||
| 98 | public function search($query = null, $next_url = null) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Make an update request |
||
| 121 | * |
||
| 122 | * @param string $object The object type to update |
||
| 123 | * @param string $id The ID of the record to update |
||
| 124 | * @param array $data The data to put into the record |
||
| 125 | * @return bool |
||
| 126 | * @throws \Exception |
||
| 127 | */ |
||
| 128 | public function updateRecord($object, $id, array $data) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Create a new object in salesforce |
||
| 142 | * |
||
| 143 | * @param string $object |
||
| 144 | * @param array|object $data |
||
| 145 | * @return string The id of the newly created record |
||
| 146 | * @throws \Exception |
||
| 147 | */ |
||
| 148 | public function createRecord($object, $data) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Delete an object with th specified id |
||
| 163 | * |
||
| 164 | * @param string $object |
||
| 165 | * @param string $id |
||
| 166 | * @return bool |
||
| 167 | * @throws \Exception |
||
| 168 | */ |
||
| 169 | View Code Duplication | public function deleteRecord($object, $id) |
|
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * Delete multiple objects with specified ids |
||
| 182 | * |
||
| 183 | * @param $object |
||
| 184 | * @param $id |
||
| 185 | * @return bool |
||
| 186 | * @throws \Exception |
||
| 187 | */ |
||
| 188 | View Code Duplication | public function bulkDeleteRecords(array $ids) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Login with user and password |
||
| 199 | * |
||
| 200 | * @param $username |
||
| 201 | * @param $password |
||
| 202 | * @return array|mixed |
||
| 203 | * @throws \Exception |
||
| 204 | */ |
||
| 205 | View Code Duplication | public function login($username, $password) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Complete the oauth process by confirming the code and returning an access token |
||
| 224 | * |
||
| 225 | * @param $code |
||
| 226 | * @param $redirect_url |
||
| 227 | * @return array|mixed |
||
| 228 | * @throws \Exception |
||
| 229 | */ |
||
| 230 | View Code Duplication | public function authorizeConfirm($code, $redirect_url) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Get the url to redirect users to when setting up a salesforce access token |
||
| 249 | * |
||
| 250 | * @param $redirectUrl |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | public function getLoginUrl($redirectUrl) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Refresh an existing access token |
||
| 267 | * |
||
| 268 | * @return AccessToken |
||
| 269 | * @throws \Exception |
||
| 270 | */ |
||
| 271 | public function refreshToken() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param AccessToken $accessToken |
||
| 292 | */ |
||
| 293 | public function setAccessToken(AccessToken $accessToken) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $method |
||
| 301 | * @param string $url |
||
| 302 | * @param array $data |
||
| 303 | * @return mixed |
||
| 304 | * @throws AuthenticationException |
||
| 305 | * @throws RequestException |
||
| 306 | */ |
||
| 307 | private function makeRequest($method, $url, $data) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return string |
||
| 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.