Complex classes like OnRequestHandler 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 OnRequestHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | 1 | final class OnRequestHandler |
|
35 | { |
||
36 | private const REDIRECT = 'redirect'; |
||
37 | private const NO_REDIRECT = 'noRedirect'; |
||
38 | private const REDIRECT_WITHOUT_PATH = 'redirectWithoutPath'; |
||
39 | |||
40 | private const MOBILE = 'mobile'; |
||
41 | private const TABLET = 'tablet'; |
||
42 | private const PHONE = 'phone'; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | public $redirectConf = []; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | public $isFullPath = true; |
||
53 | |||
54 | /** |
||
55 | * @var Http\IRequest |
||
56 | */ |
||
57 | private $httpRequest; |
||
58 | |||
59 | /** |
||
60 | * @var Http\IResponse |
||
61 | */ |
||
62 | private $httpResponse; |
||
63 | |||
64 | /** |
||
65 | * @var Application\IRouter |
||
66 | */ |
||
67 | private $router; |
||
68 | |||
69 | /** |
||
70 | * @var MobileDetect\MobileDetect |
||
71 | */ |
||
72 | private $mobileDetect; |
||
73 | |||
74 | /** |
||
75 | * @var Helpers\DeviceView |
||
76 | */ |
||
77 | private $deviceView; |
||
78 | |||
79 | /** |
||
80 | * @var OnResponseHandler |
||
81 | */ |
||
82 | private $onResponseHandler; |
||
83 | |||
84 | /** |
||
85 | * @param Http\IRequest $httpRequest |
||
86 | * @param Http\IResponse $httpResponse |
||
87 | * @param Application\IRouter $router |
||
88 | * @param OnResponseHandler $onResponseHandler |
||
89 | * @param MobileDetect\MobileDetect $mobileDetect |
||
90 | * @param Helpers\DeviceView $deviceView |
||
91 | */ |
||
92 | public function __construct( |
||
110 | |||
111 | /** |
||
112 | * @param Application\Application $application |
||
113 | * |
||
114 | * @return void |
||
115 | */ |
||
116 | public function __invoke(Application\Application $application) : void |
||
201 | |||
202 | /** |
||
203 | * Detects phone redirections |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | private function hasPhoneRedirect() : bool |
||
232 | |||
233 | /** |
||
234 | * Detects tablet redirections |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | private function hasTabletRedirect() : bool |
||
263 | |||
264 | /** |
||
265 | * Detects mobile redirections |
||
266 | * |
||
267 | * @return bool |
||
268 | */ |
||
269 | private function hasMobileRedirect() : bool |
||
293 | |||
294 | /** |
||
295 | * If a modified Response for phone devices is needed |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | private function needPhoneResponseModify() : bool |
||
311 | |||
312 | /** |
||
313 | * If a modified Response for tablet devices is needed |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | private function needTabletResponseModify() : bool |
||
329 | |||
330 | /** |
||
331 | * If a modified Response for mobile devices is needed |
||
332 | * |
||
333 | * @return bool |
||
334 | */ |
||
335 | private function needMobileResponseModify() : bool |
||
347 | |||
348 | /** |
||
349 | * If a modified Response for non-mobile devices is needed |
||
350 | * |
||
351 | * @return bool |
||
352 | */ |
||
353 | private function needNotMobileResponseModify() : bool |
||
365 | |||
366 | /** |
||
367 | * Gets the RedirectResponse by switch param |
||
368 | * |
||
369 | * @return Responses\RedirectResponse |
||
370 | */ |
||
371 | private function getRedirectResponseBySwitchParam() : Responses\RedirectResponse |
||
391 | |||
392 | /** |
||
393 | * Gets the device RedirectResponse |
||
394 | * |
||
395 | * @param string $device |
||
396 | * |
||
397 | * @return Responses\RedirectResponse|NULL |
||
398 | */ |
||
399 | private function getDeviceRedirectResponse(string $device) : ?Responses\RedirectResponse |
||
410 | |||
411 | /** |
||
412 | * Gets the redirect url |
||
413 | * |
||
414 | * @param string $platform |
||
415 | * |
||
416 | * @return string|NULL |
||
417 | */ |
||
418 | private function getRedirectUrl(string $platform) : ?string |
||
432 | |||
433 | /** |
||
434 | * Gets named option from current route |
||
435 | * |
||
436 | * @param string $name |
||
437 | * |
||
438 | * @return string|NULL |
||
439 | */ |
||
440 | private function getRoutingOption(string $name) : ?string |
||
462 | |||
463 | /** |
||
464 | * Gets the current host |
||
465 | * |
||
466 | * @return string |
||
467 | */ |
||
468 | private function getCurrentHost() : string |
||
472 | } |
||
473 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.