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 |
||
| 10 | abstract class AbstractObject implements ObjectInterface |
||
| 11 | { |
||
| 12 | protected $salesforce; |
||
| 13 | |||
| 14 | public function __construct(Salesforce $salesforce) |
||
| 15 | { |
||
| 16 | $this->salesforce = $salesforce; |
||
| 17 | } |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @param string $method |
||
| 21 | * @param string $url |
||
| 22 | * @param array $options |
||
| 23 | * |
||
| 24 | * @return object |
||
| 25 | */ |
||
| 26 | protected function sendRequest(string $method, string $url, array $options = []) |
||
| 27 | { |
||
| 28 | event(new RequestSent([ |
||
| 29 | 'data' => $options, |
||
| 30 | 'url' => $url, |
||
| 31 | 'class' => get_class($this), |
||
| 32 | 'type' => 'REQUEST', |
||
| 33 | ])); |
||
| 34 | |||
| 35 | $response = json_decode( |
||
| 36 | $this->salesforce->client->request($method, $this->salesforce->baseUrl . $url, $options) |
||
| 37 | ->getBody()); |
||
| 38 | |||
| 39 | event(new ResponseReceived([ |
||
| 40 | 'data' => $response, |
||
| 41 | 'url' => $url, |
||
| 42 | 'class' => get_class($this), |
||
| 43 | 'type' => 'RESPONSE', |
||
| 44 | ])); |
||
| 45 | |||
| 46 | return $response; |
||
| 47 | } |
||
| 48 | |||
| 49 | protected function getType() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get latest version. |
||
| 60 | * |
||
| 61 | * @return mixed |
||
| 62 | */ |
||
| 63 | public function getVersion() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get all organisation limits. |
||
| 70 | */ |
||
| 71 | public function listOrganisationLimits() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * List all available resources. |
||
| 78 | * |
||
| 79 | * @return mixed |
||
| 80 | */ |
||
| 81 | public function listAvailableResources() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * List all objects. |
||
| 88 | * |
||
| 89 | * @return mixed |
||
| 90 | */ |
||
| 91 | public function listObjects() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Describe an object. |
||
| 98 | * |
||
| 99 | * @param $objectName |
||
| 100 | * |
||
| 101 | * @return mixed |
||
| 102 | */ |
||
| 103 | public function describeObject($objectName) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Describe basic object. |
||
| 110 | * |
||
| 111 | * @param $objectName |
||
| 112 | * |
||
| 113 | * @return mixed |
||
| 114 | */ |
||
| 115 | public function describeBasicObject($objectName) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param $opportunityId |
||
| 122 | */ |
||
| 123 | public function getAllByOpportunityId($opportunityId) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Run Salesforce query. |
||
| 138 | * |
||
| 139 | * @param $query |
||
| 140 | * |
||
| 141 | * @return mixed |
||
| 142 | */ |
||
| 143 | public function query($query) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get record. |
||
| 154 | * |
||
| 155 | * @param string $id |
||
| 156 | * |
||
| 157 | * @param array $fields |
||
| 158 | */ |
||
| 159 | public function get(string $id, array $fields = []) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Update. |
||
| 166 | * |
||
| 167 | * @param string $id |
||
| 168 | * @param $params |
||
| 169 | * @throws SalesforceException |
||
| 170 | */ |
||
| 171 | View Code Duplication | public function update(string $id, array $params) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Insert new account. |
||
| 188 | * |
||
| 189 | * @param $params |
||
| 190 | * |
||
| 191 | * @throws SalesforceException |
||
| 192 | */ |
||
| 193 | View Code Duplication | public function create(array $params) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Delete a given record |
||
| 208 | * |
||
| 209 | * @param string $id |
||
| 210 | * @throws SalesforceException |
||
| 211 | */ |
||
| 212 | View Code Duplication | public function delete(string $id) |
|
| 222 | } |
||
| 223 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: