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 $recordType; |
||
13 | |||
14 | /** |
||
15 | * @param string $method |
||
16 | * @param string $url |
||
17 | * @param array $options |
||
18 | * |
||
19 | * @return string |
||
20 | */ |
||
21 | private function sendRequest(string $method, string $url, array $options = []): string |
||
41 | |||
42 | /** |
||
43 | * Get latest version. |
||
44 | * |
||
45 | * @return mixed |
||
46 | */ |
||
47 | protected function getVersion() |
||
51 | |||
52 | /** |
||
53 | * Get all organisation limits. |
||
54 | */ |
||
55 | protected function listOrganisationLimits() |
||
59 | |||
60 | /** |
||
61 | * List all available resources. |
||
62 | * |
||
63 | * @return mixed |
||
64 | */ |
||
65 | protected function listAvailableResources() |
||
69 | |||
70 | /** |
||
71 | * List all objects. |
||
72 | * |
||
73 | * @return mixed |
||
74 | */ |
||
75 | protected function listObjects() |
||
79 | |||
80 | /** |
||
81 | * Describe an object. |
||
82 | * |
||
83 | * @param $objectName |
||
84 | * |
||
85 | * @return mixed |
||
86 | */ |
||
87 | protected function describeObject($objectName) |
||
91 | |||
92 | /** |
||
93 | * Describe basic object. |
||
94 | * |
||
95 | * @param $objectName |
||
96 | * |
||
97 | * @return mixed |
||
98 | */ |
||
99 | protected function describeBasicObject($objectName) |
||
103 | |||
104 | /** |
||
105 | * Run report. |
||
106 | * |
||
107 | * @param $params |
||
108 | * |
||
109 | * @return mixed |
||
110 | */ |
||
111 | public function runReport($params) |
||
119 | |||
120 | /** |
||
121 | * Run Salesforce query. |
||
122 | * |
||
123 | * @param $query |
||
124 | * |
||
125 | * @return mixed |
||
126 | */ |
||
127 | protected function query($query) |
||
133 | |||
134 | /** |
||
135 | * Get record. |
||
136 | * |
||
137 | * @param string $id |
||
138 | * |
||
139 | * @param array $fields |
||
140 | * @return bool|mixed |
||
141 | */ |
||
142 | public function get(string $id, array $fields = []) |
||
156 | |||
157 | /** |
||
158 | * Update. |
||
159 | * |
||
160 | * @param string $id |
||
161 | * @param $params |
||
162 | * @return bool|mixed |
||
163 | * @throws SalesforceException |
||
164 | */ |
||
165 | public function update(string $id, array $params) |
||
189 | |||
190 | /** |
||
191 | * Insert new account. |
||
192 | * |
||
193 | * @param $params |
||
194 | * |
||
195 | * @return bool |
||
196 | * @throws SalesforceException |
||
197 | */ |
||
198 | View Code Duplication | public function create(array $params) |
|
214 | |||
215 | /** |
||
216 | * Delete a given record |
||
217 | * |
||
218 | * @param string $id |
||
219 | * @return bool |
||
220 | * @throws SalesforceException |
||
221 | */ |
||
222 | View Code Duplication | public function delete(string $id) |
|
236 | |||
237 | protected function getType() |
||
245 | } |
||
246 |
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: