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:
Complex classes like Users often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Users, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Users extends AbstractApi |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | * |
||
23 | * @return Users |
||
24 | */ |
||
25 | public function deletePhoto() { |
||
45 | |||
46 | |||
47 | |||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | * |
||
52 | * @param string $user |
||
53 | * @return string |
||
54 | */ |
||
55 | public function getPresence($user) { |
||
85 | |||
86 | |||
87 | |||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | public function identity() { |
||
114 | |||
115 | |||
116 | |||
117 | |||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | * |
||
122 | * @param string $user |
||
123 | * @return string |
||
124 | */ |
||
125 | public function info($user) { |
||
155 | |||
156 | |||
157 | |||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | * |
||
162 | * @param bool $presence |
||
163 | * @return string |
||
164 | */ |
||
165 | View Code Duplication | public function list_users($presence = NULL) { |
|
166 | |||
167 | // Check if the type of the variables is valid. |
||
168 | if (!is_string($presence) && ($presence != NULL)) { |
||
169 | throw new InvalidArgumentException("The type of the presence variable is not valid."); |
||
170 | } |
||
171 | |||
172 | // Set the arguments of the request |
||
173 | $arguments = array(); |
||
174 | |||
175 | if ($presence != NULL) { |
||
176 | $arguments["presence"] = $presence; |
||
177 | } |
||
178 | |||
179 | $this->setUrl("users.list", $arguments); |
||
180 | |||
181 | // Send the request |
||
182 | try { |
||
183 | $client = new \GuzzleHttp\Client(); |
||
184 | $json_response = $client->request('GET', $this->getUrl(), []); |
||
185 | $response = json_decode( $json_response->getBody() ); |
||
186 | } |
||
187 | catch (RequestException $e) { |
||
188 | throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
||
189 | } |
||
190 | |||
191 | if($response->{'ok'} === FALSE) { |
||
192 | throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
||
193 | } |
||
194 | |||
195 | return $json_response->getBody(); |
||
196 | } |
||
197 | |||
198 | |||
199 | |||
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | * |
||
204 | * @return Users |
||
205 | */ |
||
206 | public function setActive() { |
||
226 | |||
227 | |||
228 | |||
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | * |
||
233 | * @param string $image |
||
234 | * @param integer $crop_x |
||
235 | * @param integer $crop_y |
||
236 | * @param integer $crop_w |
||
237 | * @return Users |
||
238 | */ |
||
239 | public function setPhoto($image, $crop_x = NULL, $crop_y = NULL, $crop_w = NULL) { |
||
295 | |||
296 | |||
297 | |||
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | * |
||
302 | * @param string $presence |
||
303 | * @return Users |
||
304 | */ |
||
305 | public function setPresence($presence) { |
||
335 | } |
||
336 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.