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 |
||
| 12 | abstract class AbstractObject implements ObjectInterface |
||
| 13 | { |
||
| 14 | protected $recordType; |
||
| 15 | |||
| 16 | protected $salesforce; |
||
| 17 | |||
| 18 | public function __construct(Salesforce $salesforce) |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param string $method |
||
| 25 | * @param string $url |
||
| 26 | * @param array $options |
||
| 27 | * |
||
| 28 | * @return string |
||
| 29 | */ |
||
| 30 | protected function sendRequest(string $method, string $url, array $options = []): string |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Get latest version. |
||
| 53 | * |
||
| 54 | * @return mixed |
||
| 55 | */ |
||
| 56 | protected function getVersion() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get all organisation limits. |
||
| 63 | */ |
||
| 64 | protected function listOrganisationLimits() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * List all available resources. |
||
| 71 | * |
||
| 72 | * @return mixed |
||
| 73 | */ |
||
| 74 | protected function listAvailableResources() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * List all objects. |
||
| 81 | * |
||
| 82 | * @return mixed |
||
| 83 | */ |
||
| 84 | protected function listObjects() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Describe an object. |
||
| 91 | * |
||
| 92 | * @param $objectName |
||
| 93 | * |
||
| 94 | * @return mixed |
||
| 95 | */ |
||
| 96 | protected function describeObject($objectName) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Describe basic object. |
||
| 103 | * |
||
| 104 | * @param $objectName |
||
| 105 | * |
||
| 106 | * @return mixed |
||
| 107 | */ |
||
| 108 | protected function describeBasicObject($objectName) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Run report. |
||
| 115 | * |
||
| 116 | * @param string $id |
||
| 117 | * @param bool $includeDetails |
||
| 118 | * @return mixed |
||
| 119 | * |
||
| 120 | */ |
||
| 121 | public function runReport(string $id, bool $includeDetails) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Run Salesforce query. |
||
| 132 | * |
||
| 133 | * @param $query |
||
| 134 | * |
||
| 135 | * @return mixed |
||
| 136 | */ |
||
| 137 | protected function query($query) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get record. |
||
| 146 | * |
||
| 147 | * @param string $id |
||
| 148 | * |
||
| 149 | * @param array $fields |
||
| 150 | * @return bool|mixed |
||
| 151 | */ |
||
| 152 | public function get(string $id, array $fields = []) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Update. |
||
| 169 | * |
||
| 170 | * @param string $id |
||
| 171 | * @param $params |
||
| 172 | * @return bool|mixed |
||
| 173 | * @throws SalesforceException |
||
| 174 | */ |
||
| 175 | public function update(string $id, array $params) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Insert new account. |
||
| 202 | * |
||
| 203 | * @param $params |
||
| 204 | * |
||
| 205 | * @return bool |
||
| 206 | * @throws SalesforceException |
||
| 207 | */ |
||
| 208 | View Code Duplication | public function create(array $params) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Delete a given record |
||
| 227 | * |
||
| 228 | * @param string $id |
||
| 229 | * @return bool |
||
| 230 | * @throws SalesforceException |
||
| 231 | */ |
||
| 232 | View Code Duplication | public function delete(string $id) |
|
| 246 | |||
| 247 | protected function getType() |
||
| 255 | } |
||
| 256 |
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: