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 |
||
52 | class RequestHandlerController extends OCSController { |
||
53 | |||
54 | /** @var FederatedShareProvider */ |
||
55 | private $federatedShareProvider; |
||
56 | |||
57 | /** @var IAppManager */ |
||
58 | private $appManager; |
||
59 | |||
60 | /** @var IUserManager */ |
||
61 | private $userManager; |
||
62 | |||
63 | /** @var AddressHandler */ |
||
64 | private $addressHandler; |
||
65 | |||
66 | /** @var FedShareManager */ |
||
67 | private $fedShareManager; |
||
68 | |||
69 | /** |
||
70 | * Server2Server constructor. |
||
71 | * |
||
72 | * @param string $appName |
||
73 | * @param IRequest $request |
||
74 | * @param FederatedShareProvider $federatedShareProvider |
||
75 | * @param IAppManager $appManager |
||
76 | * @param IUserManager $userManager |
||
77 | * @param AddressHandler $addressHandler |
||
78 | * @param FedShareManager $fedShareManager |
||
79 | */ |
||
80 | public function __construct($appName, |
||
81 | IRequest $request, |
||
82 | FederatedShareProvider $federatedShareProvider, |
||
83 | IAppManager $appManager, |
||
84 | IUserManager $userManager, |
||
85 | AddressHandler $addressHandler, |
||
86 | FedShareManager $fedShareManager |
||
87 | ) { |
||
88 | parent::__construct($appName, $request); |
||
89 | |||
90 | $this->federatedShareProvider = $federatedShareProvider; |
||
91 | $this->appManager = $appManager; |
||
92 | $this->userManager = $userManager; |
||
93 | $this->addressHandler = $addressHandler; |
||
94 | $this->fedShareManager = $fedShareManager; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @NoCSRFRequired |
||
99 | * @PublicPage |
||
100 | * |
||
101 | * create a new share |
||
102 | * |
||
103 | * @return Result |
||
104 | */ |
||
105 | public function createShare() { |
||
191 | |||
192 | /** |
||
193 | * @NoCSRFRequired |
||
194 | * @PublicPage |
||
195 | * |
||
196 | * create re-share on behalf of another user |
||
197 | * |
||
198 | * @param int $id |
||
199 | * |
||
200 | * @return Result |
||
201 | */ |
||
202 | public function reShare($id) { |
||
250 | |||
251 | /** |
||
252 | * @NoCSRFRequired |
||
253 | * @PublicPage |
||
254 | * |
||
255 | * accept server-to-server share |
||
256 | * |
||
257 | * @param int $id |
||
258 | * |
||
259 | * @return Result |
||
260 | */ |
||
261 | View Code Duplication | public function acceptShare($id) { |
|
277 | |||
278 | /** |
||
279 | * @NoCSRFRequired |
||
280 | * @PublicPage |
||
281 | * |
||
282 | * decline server-to-server share |
||
283 | * |
||
284 | * @param int $id |
||
285 | * |
||
286 | * @return Result |
||
287 | */ |
||
288 | View Code Duplication | public function declineShare($id) { |
|
305 | |||
306 | /** |
||
307 | * @NoCSRFRequired |
||
308 | * @PublicPage |
||
309 | * |
||
310 | * remove server-to-server share if it was unshared by the owner |
||
311 | * |
||
312 | * @param int $id |
||
313 | * |
||
314 | * @return Result |
||
315 | */ |
||
316 | public function unshare($id) { |
||
334 | |||
335 | /** |
||
336 | * @NoCSRFRequired |
||
337 | * @PublicPage |
||
338 | * |
||
339 | * federated share was revoked, either by the owner or the re-sharer |
||
340 | * |
||
341 | * @param int $id |
||
342 | * |
||
343 | * @return Result |
||
344 | */ |
||
345 | public function revoke($id) { |
||
355 | |||
356 | /** |
||
357 | * @NoCSRFRequired |
||
358 | * @PublicPage |
||
359 | * |
||
360 | * update share information to keep federated re-shares in sync |
||
361 | * |
||
362 | * @param int $id |
||
363 | * |
||
364 | * @return Result |
||
365 | */ |
||
366 | public function updatePermissions($id) { |
||
382 | |||
383 | /** |
||
384 | * Get share by id, validate it's type and token |
||
385 | * |
||
386 | * @param int $id |
||
387 | * |
||
388 | * @return IShare |
||
389 | * |
||
390 | * @throws Share\Exceptions\ShareNotFound |
||
391 | * @throws InvalidShareException |
||
392 | */ |
||
393 | View Code Duplication | protected function getValidShare($id) { |
|
394 | $share = $this->federatedShareProvider->getShareById($id); |
||
395 | $token = $this->request->getParam('token', null); |
||
396 | if ($share->getShareType() !== FederatedShareProvider::SHARE_TYPE_REMOTE |
||
397 | || $share->getToken() !== $token |
||
398 | ) { |
||
399 | throw new InvalidShareException(); |
||
400 | } |
||
401 | return $share; |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Make sure that incoming shares are enabled |
||
406 | * |
||
407 | * @return void |
||
408 | * |
||
409 | * @throws NotSupportedException |
||
410 | */ |
||
411 | protected function assertIncomingSharingEnabled() { |
||
418 | |||
419 | /** |
||
420 | * Make sure that outgoing shares are enabled |
||
421 | * |
||
422 | * @return void |
||
423 | * |
||
424 | * @throws NotSupportedException |
||
425 | */ |
||
426 | protected function assertOutgoingSharingEnabled() { |
||
433 | |||
434 | /** |
||
435 | * Check if value is null or an array has any null item |
||
436 | * |
||
437 | * @param mixed $param |
||
438 | * |
||
439 | * @return bool |
||
440 | */ |
||
441 | View Code Duplication | protected function hasNull($param) { |
|
448 | } |
||
449 |
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: