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 RequestHandlerController 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 RequestHandlerController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
51 | class RequestHandlerController extends OCSController { |
||
52 | |||
53 | /** @var FederatedShareProvider */ |
||
54 | private $federatedShareProvider; |
||
55 | |||
56 | /** @var IAppManager */ |
||
57 | private $appManager; |
||
58 | |||
59 | /** @var IUserManager */ |
||
60 | private $userManager; |
||
61 | |||
62 | /** @var AddressHandler */ |
||
63 | private $addressHandler; |
||
64 | |||
65 | /** @var FedShareManager */ |
||
66 | private $fedShareManager; |
||
67 | |||
68 | /** |
||
69 | * Server2Server constructor. |
||
70 | * |
||
71 | * @param string $appName |
||
72 | * @param IRequest $request |
||
73 | * @param FederatedShareProvider $federatedShareProvider |
||
74 | * @param IAppManager $appManager |
||
75 | * @param IUserManager $userManager |
||
76 | * @param AddressHandler $addressHandler |
||
77 | * @param FedShareManager $fedShareManager |
||
78 | */ |
||
79 | public function __construct($appName, |
||
95 | |||
96 | /** |
||
97 | * @NoCSRFRequired |
||
98 | * @PublicPage |
||
99 | * |
||
100 | * create a new share |
||
101 | * |
||
102 | * @return Result |
||
103 | */ |
||
104 | public function createShare() { |
||
180 | |||
181 | /** |
||
182 | * @NoCSRFRequired |
||
183 | * @PublicPage |
||
184 | * |
||
185 | * create re-share on behalf of another user |
||
186 | * |
||
187 | * @param int $id |
||
188 | * |
||
189 | * @return Result |
||
190 | */ |
||
191 | public function reShare($id) { |
||
239 | |||
240 | /** |
||
241 | * @NoCSRFRequired |
||
242 | * @PublicPage |
||
243 | * |
||
244 | * accept server-to-server share |
||
245 | * |
||
246 | * @param int $id |
||
247 | * |
||
248 | * @return Result |
||
249 | */ |
||
250 | View Code Duplication | public function acceptShare($id) { |
|
266 | |||
267 | /** |
||
268 | * @NoCSRFRequired |
||
269 | * @PublicPage |
||
270 | * |
||
271 | * decline server-to-server share |
||
272 | * |
||
273 | * @param int $id |
||
274 | * |
||
275 | * @return Result |
||
276 | */ |
||
277 | View Code Duplication | public function declineShare($id) { |
|
294 | |||
295 | /** |
||
296 | * @NoCSRFRequired |
||
297 | * @PublicPage |
||
298 | * |
||
299 | * remove server-to-server share if it was unshared by the owner |
||
300 | * |
||
301 | * @param int $id |
||
302 | * |
||
303 | * @return Result |
||
304 | */ |
||
305 | public function unshare($id) { |
||
323 | |||
324 | /** |
||
325 | * @NoCSRFRequired |
||
326 | * @PublicPage |
||
327 | * |
||
328 | * federated share was revoked, either by the owner or the re-sharer |
||
329 | * |
||
330 | * @param int $id |
||
331 | * |
||
332 | * @return Result |
||
333 | */ |
||
334 | public function revoke($id) { |
||
344 | |||
345 | /** |
||
346 | * @NoCSRFRequired |
||
347 | * @PublicPage |
||
348 | * |
||
349 | * update share information to keep federated re-shares in sync |
||
350 | * |
||
351 | * @param int $id |
||
352 | * |
||
353 | * @return Result |
||
354 | */ |
||
355 | public function updatePermissions($id) { |
||
371 | |||
372 | /** |
||
373 | * Get share by id, validate it's type and token |
||
374 | * |
||
375 | * @param int $id |
||
376 | * |
||
377 | * @return IShare |
||
378 | * |
||
379 | * @throws Share\Exceptions\ShareNotFound |
||
380 | * @throws InvalidShareException |
||
381 | */ |
||
382 | protected function getValidShare($id) { |
||
392 | |||
393 | /** |
||
394 | * Make sure that incoming shares are enabled |
||
395 | * |
||
396 | * @return void |
||
397 | * |
||
398 | * @throws NotSupportedException |
||
399 | */ |
||
400 | protected function assertIncomingSharingEnabled() { |
||
407 | |||
408 | /** |
||
409 | * Make sure that outgoing shares are enabled |
||
410 | * |
||
411 | * @return void |
||
412 | * |
||
413 | * @throws NotSupportedException |
||
414 | */ |
||
415 | protected function assertOutgoingSharingEnabled() { |
||
422 | |||
423 | /** |
||
424 | * Check if value is null or an array has any null item |
||
425 | * |
||
426 | * @param mixed $param |
||
427 | * |
||
428 | * @return bool |
||
429 | */ |
||
430 | protected function hasNull($param) { |
||
437 | } |
||
438 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: