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 |
||
| 11 | abstract class AbstractObject implements ObjectInterface |
||
| 12 | { |
||
| 13 | protected $salesforce; |
||
| 14 | |||
| 15 | public function __construct(Salesforce $salesforce) |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param string $method |
||
| 22 | * @param string $url |
||
| 23 | * @param array $options |
||
| 24 | * |
||
| 25 | * @return object |
||
| 26 | */ |
||
| 27 | protected function sendRequest(string $method, string $url, array $options = []) |
||
| 54 | |||
| 55 | protected function getType() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get latest version. |
||
| 66 | * |
||
| 67 | * @return mixed |
||
| 68 | */ |
||
| 69 | public function getVersion() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get all organisation limits. |
||
| 76 | */ |
||
| 77 | public function listOrganisationLimits() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * List all available resources. |
||
| 84 | * |
||
| 85 | * @return mixed |
||
| 86 | */ |
||
| 87 | public function listAvailableResources() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * List all objects. |
||
| 94 | * |
||
| 95 | * @return mixed |
||
| 96 | */ |
||
| 97 | public function listObjects() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Describe an object. |
||
| 104 | * |
||
| 105 | * @param $objectName |
||
| 106 | * |
||
| 107 | * @return mixed |
||
| 108 | */ |
||
| 109 | public function describeObject($objectName) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Describe basic object. |
||
| 116 | * |
||
| 117 | * @param $objectName |
||
| 118 | * |
||
| 119 | * @return mixed |
||
| 120 | */ |
||
| 121 | public function describeBasicObject($objectName) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Run Salesforce query. |
||
| 128 | * |
||
| 129 | * @param $query |
||
| 130 | * |
||
| 131 | * @return mixed |
||
| 132 | */ |
||
| 133 | public function query($query) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get record. |
||
| 144 | * |
||
| 145 | * @param string $id |
||
| 146 | * |
||
| 147 | * @param array $fields |
||
| 148 | */ |
||
| 149 | public function get(string $id, array $fields = []) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Update. |
||
| 156 | * |
||
| 157 | * @param string $id |
||
| 158 | * @param $params |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | public function update(string $id, array $params) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Insert new account. |
||
| 172 | * |
||
| 173 | * @param $params |
||
| 174 | * |
||
| 175 | * @throws SalesforceException |
||
| 176 | */ |
||
| 177 | View Code Duplication | public function create(array $params) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Delete a given record |
||
| 192 | * |
||
| 193 | * @param string $id |
||
| 194 | * @throws SalesforceException |
||
| 195 | */ |
||
| 196 | View Code Duplication | public function delete(string $id) |
|
| 206 | |||
| 207 | public function report(string $id, bool $includeDetails = true) |
||
| 215 | } |
||
| 216 |
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: