Complex classes like PrettyPageHandler 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 PrettyPageHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class PrettyPageHandler extends Handler |
||
19 | { |
||
20 | const EDITOR_SUBLIME = "sublime"; |
||
21 | const EDITOR_TEXTMATE = "textmate"; |
||
22 | const EDITOR_EMACS = "emacs"; |
||
23 | const EDITOR_MACVIM = "macvim"; |
||
24 | const EDITOR_PHPSTORM = "phpstorm"; |
||
25 | const EDITOR_IDEA = "idea"; |
||
26 | const EDITOR_VSCODE = "vscode"; |
||
27 | const EDITOR_ATOM = "atom"; |
||
28 | const EDITOR_ESPRESSO = "espresso"; |
||
29 | const EDITOR_XDEBUG = "xdebug"; |
||
30 | |||
31 | /** |
||
32 | * Search paths to be scanned for resources. |
||
33 | * |
||
34 | * Stored in the reverse order they're declared. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | private $searchPaths = []; |
||
39 | |||
40 | /** |
||
41 | * Fast lookup cache for known resource locations. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $resourceCache = []; |
||
46 | |||
47 | /** |
||
48 | * The name of the custom css file. |
||
49 | * |
||
50 | * @var string|null |
||
51 | */ |
||
52 | private $customCss = null; |
||
53 | |||
54 | /** |
||
55 | * The name of the custom js file. |
||
56 | * |
||
57 | * @var string|null |
||
58 | */ |
||
59 | private $customJs = null; |
||
60 | |||
61 | /** |
||
62 | * @var array[] |
||
63 | */ |
||
64 | private $extraTables = []; |
||
65 | |||
66 | /** |
||
67 | * @var bool |
||
68 | */ |
||
69 | private $handleUnconditionally = false; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | private $pageTitle = "Whoops! There was an error."; |
||
75 | |||
76 | /** |
||
77 | * @var array[] |
||
78 | */ |
||
79 | private $applicationPaths; |
||
80 | |||
81 | /** |
||
82 | * @var array[] |
||
83 | */ |
||
84 | private $blacklist = [ |
||
85 | '_GET' => [], |
||
86 | '_POST' => [], |
||
87 | '_FILES' => [], |
||
88 | '_COOKIE' => [], |
||
89 | '_SESSION' => [], |
||
90 | '_SERVER' => [], |
||
91 | '_ENV' => [], |
||
92 | ]; |
||
93 | |||
94 | /** |
||
95 | * An identifier for a known IDE/text editor. |
||
96 | * |
||
97 | * Either a string, or a calalble that resolves a string, that can be used |
||
98 | * to open a given file in an editor. If the string contains the special |
||
99 | * substrings %file or %line, they will be replaced with the correct data. |
||
100 | * |
||
101 | * @example |
||
102 | * "txmt://open?url=%file&line=%line" |
||
103 | * |
||
104 | * @var callable|string $editor |
||
105 | */ |
||
106 | protected $editor; |
||
107 | |||
108 | /** |
||
109 | * A list of known editor strings. |
||
110 | * |
||
111 | * @var array |
||
112 | */ |
||
113 | protected $editors = [ |
||
114 | "sublime" => "subl://open?url=file://%file&line=%line", |
||
115 | "textmate" => "txmt://open?url=file://%file&line=%line", |
||
116 | "emacs" => "emacs://open?url=file://%file&line=%line", |
||
117 | "macvim" => "mvim://open/?url=file://%file&line=%line", |
||
118 | "phpstorm" => "phpstorm://open?file=%file&line=%line", |
||
119 | "idea" => "idea://open?file=%file&line=%line", |
||
120 | "vscode" => "vscode://file/%file:%line", |
||
121 | "atom" => "atom://core/open/file?filename=%file&line=%line", |
||
122 | "espresso" => "x-espresso://open?filepath=%file&lines=%line", |
||
123 | ]; |
||
124 | |||
125 | /** |
||
126 | * @var TemplateHelper |
||
127 | */ |
||
128 | protected $templateHelper; |
||
129 | |||
130 | /** |
||
131 | * Constructor. |
||
132 | * |
||
133 | * @return void |
||
|
|||
134 | */ |
||
135 | 1 | public function __construct() |
|
175 | |||
176 | /** |
||
177 | * @return int|null |
||
178 | */ |
||
179 | 1 | public function handle() |
|
293 | |||
294 | /** |
||
295 | * Get the stack trace frames of the exception currently being handled. |
||
296 | * |
||
297 | * @return \Whoops\Exception\FrameCollection |
||
298 | */ |
||
299 | protected function getExceptionFrames() |
||
316 | |||
317 | /** |
||
318 | * Get the code of the exception currently being handled. |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | protected function getExceptionCode() |
||
334 | |||
335 | /** |
||
336 | * @return string |
||
337 | */ |
||
338 | public function contentType() |
||
342 | |||
343 | /** |
||
344 | * Adds an entry to the list of tables displayed in the template. |
||
345 | * |
||
346 | * The expected data is a simple associative array. Any nested arrays |
||
347 | * will be flattened with `print_r`. |
||
348 | * |
||
349 | * @param string $label |
||
350 | * @param array $data |
||
351 | * |
||
352 | * @return void |
||
353 | */ |
||
354 | 1 | public function addDataTable($label, array $data) |
|
358 | |||
359 | /** |
||
360 | * Lazily adds an entry to the list of tables displayed in the table. |
||
361 | * |
||
362 | * The supplied callback argument will be called when the error is |
||
363 | * rendered, it should produce a simple associative array. Any nested |
||
364 | * arrays will be flattened with `print_r`. |
||
365 | * |
||
366 | * @param string $label |
||
367 | * @param callable $callback Callable returning an associative array |
||
368 | * |
||
369 | * @throws InvalidArgumentException If $callback is not callable |
||
370 | * |
||
371 | * @return void |
||
372 | */ |
||
373 | 1 | public function addDataTableCallback($label, /* callable */ $callback) |
|
391 | |||
392 | /** |
||
393 | * Returns all the extra data tables registered with this handler. |
||
394 | * |
||
395 | * Optionally accepts a 'label' parameter, to only return the data table |
||
396 | * under that label. |
||
397 | * |
||
398 | * @param string|null $label |
||
399 | * |
||
400 | * @return array[]|callable |
||
401 | */ |
||
402 | 2 | public function getDataTables($label = null) |
|
411 | |||
412 | /** |
||
413 | * Set whether to handle unconditionally. |
||
414 | * |
||
415 | * Allows to disable all attempts to dynamically decide whether to handle |
||
416 | * or return prematurely. Set this to ensure that the handler will perform, |
||
417 | * no matter what. |
||
418 | * |
||
419 | * @param bool|null $value |
||
420 | * |
||
421 | * @return bool|null |
||
422 | */ |
||
423 | 1 | public function handleUnconditionally($value = null) |
|
431 | |||
432 | /** |
||
433 | * Adds an editor resolver. |
||
434 | * |
||
435 | * Either a string, or a closure that resolves a string, that can be used |
||
436 | * to open a given file in an editor. If the string contains the special |
||
437 | * substrings %file or %line, they will be replaced with the correct data. |
||
438 | * |
||
439 | * @example |
||
440 | * $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") |
||
441 | * @example |
||
442 | * $run->addEditor('remove-it', function($file, $line) { |
||
443 | * unlink($file); |
||
444 | * return "http://stackoverflow.com"; |
||
445 | * }); |
||
446 | * |
||
447 | * @param string $identifier |
||
448 | * @param string|callable $resolver |
||
449 | * |
||
450 | * @return void |
||
451 | */ |
||
452 | 1 | public function addEditor($identifier, $resolver) |
|
456 | |||
457 | /** |
||
458 | * Set the editor to use to open referenced files. |
||
459 | * |
||
460 | * Pass either the name of a configured editor, or a closure that directly |
||
461 | * resolves an editor string. |
||
462 | * |
||
463 | * @example |
||
464 | * $run->setEditor(function($file, $line) { return "file:///{$file}"; }); |
||
465 | * @example |
||
466 | * $run->setEditor('sublime'); |
||
467 | * |
||
468 | * @param string|callable $editor |
||
469 | * |
||
470 | * @throws InvalidArgumentException If invalid argument identifier provided |
||
471 | * |
||
472 | * @return void |
||
473 | */ |
||
474 | 4 | public function setEditor($editor) |
|
485 | |||
486 | /** |
||
487 | * Get the editor href for a given file and line, if available. |
||
488 | * |
||
489 | * @param string $filePath |
||
490 | * @param int $line |
||
491 | * |
||
492 | * @throws InvalidArgumentException If editor resolver does not return a string |
||
493 | * |
||
494 | * @return string|bool |
||
495 | */ |
||
496 | 4 | public function getEditorHref($filePath, $line) |
|
517 | |||
518 | /** |
||
519 | * Determine if the editor link should act as an Ajax request. |
||
520 | * |
||
521 | * @param string $filePath |
||
522 | * @param int $line |
||
523 | * |
||
524 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
525 | * |
||
526 | * @return bool |
||
527 | */ |
||
528 | 1 | public function getEditorAjax($filePath, $line) |
|
540 | |||
541 | /** |
||
542 | * Determines both the editor and if ajax should be used. |
||
543 | * |
||
544 | * @param string $filePath |
||
545 | * @param int $line |
||
546 | * |
||
547 | * @return array |
||
548 | */ |
||
549 | 1 | protected function getEditor($filePath, $line) |
|
588 | |||
589 | /** |
||
590 | * Set the page title. |
||
591 | * |
||
592 | * @param string $title |
||
593 | * |
||
594 | * @return void |
||
595 | */ |
||
596 | 1 | public function setPageTitle($title) |
|
600 | |||
601 | /** |
||
602 | * Get the page title. |
||
603 | * |
||
604 | * @return string |
||
605 | */ |
||
606 | 1 | public function getPageTitle() |
|
610 | |||
611 | /** |
||
612 | * Adds a path to the list of paths to be searched for resources. |
||
613 | * |
||
614 | * @param string $path |
||
615 | * |
||
616 | * @throws InvalidArgumentException If $path is not a valid directory |
||
617 | * |
||
618 | * @return void |
||
619 | */ |
||
620 | 2 | public function addResourcePath($path) |
|
630 | |||
631 | /** |
||
632 | * Adds a custom css file to be loaded. |
||
633 | * |
||
634 | * @param string|null $name |
||
635 | * |
||
636 | * @return void |
||
637 | */ |
||
638 | public function addCustomCss($name) |
||
642 | |||
643 | /** |
||
644 | * Adds a custom js file to be loaded. |
||
645 | * |
||
646 | * @param string|null $name |
||
647 | * |
||
648 | * @return void |
||
649 | */ |
||
650 | public function addCustomJs($name) |
||
654 | |||
655 | /** |
||
656 | * @return array |
||
657 | */ |
||
658 | 1 | public function getResourcePaths() |
|
662 | |||
663 | /** |
||
664 | * Finds a resource, by its relative path, in all available search paths. |
||
665 | * |
||
666 | * The search is performed starting at the last search path, and all the |
||
667 | * way back to the first, enabling a cascading-type system of overrides for |
||
668 | * all resources. |
||
669 | * |
||
670 | * @param string $resource |
||
671 | * |
||
672 | * @throws RuntimeException If resource cannot be found in any of the available paths |
||
673 | * |
||
674 | * @return string |
||
675 | */ |
||
676 | protected function getResource($resource) |
||
702 | |||
703 | /** |
||
704 | * @deprecated |
||
705 | * |
||
706 | * @return string |
||
707 | */ |
||
708 | public function getResourcesPath() |
||
715 | |||
716 | /** |
||
717 | * @deprecated |
||
718 | * |
||
719 | * @param string $resourcesPath |
||
720 | * |
||
721 | * @return void |
||
722 | */ |
||
723 | public function setResourcesPath($resourcesPath) |
||
727 | |||
728 | /** |
||
729 | * Return the application paths. |
||
730 | * |
||
731 | * @return array |
||
732 | */ |
||
733 | public function getApplicationPaths() |
||
737 | |||
738 | /** |
||
739 | * Set the application paths. |
||
740 | * |
||
741 | * @param array $applicationPaths |
||
742 | * |
||
743 | * @return void |
||
744 | */ |
||
745 | public function setApplicationPaths($applicationPaths) |
||
749 | |||
750 | /** |
||
751 | * Set the application root path. |
||
752 | * |
||
753 | * @param string $applicationRootPath |
||
754 | * |
||
755 | * @return void |
||
756 | */ |
||
757 | public function setApplicationRootPath($applicationRootPath) |
||
761 | |||
762 | /** |
||
763 | * blacklist a sensitive value within one of the superglobal arrays. |
||
764 | * Alias for the hideSuperglobalKey method. |
||
765 | * |
||
766 | * @param string $superGlobalName The name of the superglobal array, e.g. '_GET' |
||
767 | * @param string $key The key within the superglobal |
||
768 | * @see hideSuperglobalKey |
||
769 | * |
||
770 | * @return void |
||
771 | */ |
||
772 | 1 | public function blacklist($superGlobalName, $key) |
|
776 | |||
777 | /** |
||
778 | * Hide a sensitive value within one of the superglobal arrays. |
||
779 | * |
||
780 | * @param string $superGlobalName The name of the superglobal array, e.g. '_GET' |
||
781 | * @param string $key The key within the superglobal |
||
782 | * @return void |
||
783 | */ |
||
784 | public function hideSuperglobalKey($superGlobalName, $key) |
||
788 | |||
789 | /** |
||
790 | * Checks all values within the given superGlobal array. |
||
791 | * |
||
792 | * Blacklisted values will be replaced by a equal length string cointaining |
||
793 | * only '*' characters. We intentionally dont rely on $GLOBALS as it |
||
794 | * depends on the 'auto_globals_jit' php.ini setting. |
||
795 | * |
||
796 | * @param array $superGlobal One of the superglobal arrays |
||
797 | * @param string $superGlobalName The name of the superglobal array, e.g. '_GET' |
||
798 | * |
||
799 | * @return array $values without sensitive data |
||
800 | */ |
||
801 | private function masked(array $superGlobal, $superGlobalName) |
||
815 | } |
||
816 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.