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 | const EDITOR_NETBEANS = "netbeans"; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Search paths to be scanned for resources. |
||
| 34 | * |
||
| 35 | * Stored in the reverse order they're declared. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private $searchPaths = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Fast lookup cache for known resource locations. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private $resourceCache = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The name of the custom css file. |
||
| 50 | * |
||
| 51 | * @var string|null |
||
| 52 | */ |
||
| 53 | private $customCss = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The name of the custom js file. |
||
| 57 | * |
||
| 58 | * @var string|null |
||
| 59 | */ |
||
| 60 | private $customJs = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array[] |
||
| 64 | */ |
||
| 65 | private $extraTables = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | private $handleUnconditionally = false; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | private $pageTitle = "Whoops! There was an error."; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array[] |
||
| 79 | */ |
||
| 80 | private $applicationPaths; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array[] |
||
| 84 | */ |
||
| 85 | private $blacklist = [ |
||
| 86 | '_GET' => [], |
||
| 87 | '_POST' => [], |
||
| 88 | '_FILES' => [], |
||
| 89 | '_COOKIE' => [], |
||
| 90 | '_SESSION' => [], |
||
| 91 | '_SERVER' => [], |
||
| 92 | '_ENV' => [], |
||
| 93 | ]; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * An identifier for a known IDE/text editor. |
||
| 97 | * |
||
| 98 | * Either a string, or a calalble that resolves a string, that can be used |
||
| 99 | * to open a given file in an editor. If the string contains the special |
||
| 100 | * substrings %file or %line, they will be replaced with the correct data. |
||
| 101 | * |
||
| 102 | * @example |
||
| 103 | * "txmt://open?url=%file&line=%line" |
||
| 104 | * |
||
| 105 | * @var callable|string $editor |
||
| 106 | */ |
||
| 107 | protected $editor; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * A list of known editor strings. |
||
| 111 | * |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | protected $editors = [ |
||
| 115 | "sublime" => "subl://open?url=file://%file&line=%line", |
||
| 116 | "textmate" => "txmt://open?url=file://%file&line=%line", |
||
| 117 | "emacs" => "emacs://open?url=file://%file&line=%line", |
||
| 118 | "macvim" => "mvim://open/?url=file://%file&line=%line", |
||
| 119 | "phpstorm" => "phpstorm://open?file=%file&line=%line", |
||
| 120 | "idea" => "idea://open?file=%file&line=%line", |
||
| 121 | "vscode" => "vscode://file/%file:%line", |
||
| 122 | "atom" => "atom://core/open/file?filename=%file&line=%line", |
||
| 123 | "espresso" => "x-espresso://open?filepath=%file&lines=%line", |
||
| 124 | "netbeans" => "netbeans://open/?f=%file:%line", |
||
| 125 | ]; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var TemplateHelper |
||
| 129 | */ |
||
| 130 | protected $templateHelper; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Constructor. |
||
| 134 | * |
||
| 135 | * @return void |
||
|
|
|||
| 136 | */ |
||
| 137 | 1 | public function __construct() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @return int|null |
||
| 180 | * |
||
| 181 | * @throws \Exception |
||
| 182 | */ |
||
| 183 | 1 | public function handle() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Get the stack trace frames of the exception currently being handled. |
||
| 300 | * |
||
| 301 | * @return \Whoops\Exception\FrameCollection |
||
| 302 | */ |
||
| 303 | protected function getExceptionFrames() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get the code of the exception currently being handled. |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | protected function getExceptionCode() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function contentType() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Adds an entry to the list of tables displayed in the template. |
||
| 349 | * |
||
| 350 | * The expected data is a simple associative array. Any nested arrays |
||
| 351 | * will be flattened with `print_r`. |
||
| 352 | * |
||
| 353 | * @param string $label |
||
| 354 | * @param array $data |
||
| 355 | * |
||
| 356 | * @return static |
||
| 357 | */ |
||
| 358 | 1 | public function addDataTable($label, array $data) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Lazily adds an entry to the list of tables displayed in the table. |
||
| 366 | * |
||
| 367 | * The supplied callback argument will be called when the error is |
||
| 368 | * rendered, it should produce a simple associative array. Any nested |
||
| 369 | * arrays will be flattened with `print_r`. |
||
| 370 | * |
||
| 371 | * @param string $label |
||
| 372 | * @param callable $callback Callable returning an associative array |
||
| 373 | * |
||
| 374 | * @throws InvalidArgumentException If $callback is not callable |
||
| 375 | * |
||
| 376 | * @return static |
||
| 377 | */ |
||
| 378 | 1 | public function addDataTableCallback($label, /* callable */ $callback) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Returns all the extra data tables registered with this handler. |
||
| 401 | * |
||
| 402 | * Optionally accepts a 'label' parameter, to only return the data table |
||
| 403 | * under that label. |
||
| 404 | * |
||
| 405 | * @param string|null $label |
||
| 406 | * |
||
| 407 | * @return array[]|callable |
||
| 408 | */ |
||
| 409 | 2 | public function getDataTables($label = null) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Set whether to handle unconditionally. |
||
| 421 | * |
||
| 422 | * Allows to disable all attempts to dynamically decide whether to handle |
||
| 423 | * or return prematurely. Set this to ensure that the handler will perform, |
||
| 424 | * no matter what. |
||
| 425 | * |
||
| 426 | * @param bool|null $value |
||
| 427 | * |
||
| 428 | * @return bool|static |
||
| 429 | */ |
||
| 430 | 1 | public function handleUnconditionally($value = null) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Adds an editor resolver. |
||
| 442 | * |
||
| 443 | * Either a string, or a closure that resolves a string, that can be used |
||
| 444 | * to open a given file in an editor. If the string contains the special |
||
| 445 | * substrings %file or %line, they will be replaced with the correct data. |
||
| 446 | * |
||
| 447 | * @example |
||
| 448 | * $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") |
||
| 449 | * @example |
||
| 450 | * $run->addEditor('remove-it', function($file, $line) { |
||
| 451 | * unlink($file); |
||
| 452 | * return "http://stackoverflow.com"; |
||
| 453 | * }); |
||
| 454 | * |
||
| 455 | * @param string $identifier |
||
| 456 | * @param string|callable $resolver |
||
| 457 | * |
||
| 458 | * @return static |
||
| 459 | */ |
||
| 460 | 1 | public function addEditor($identifier, $resolver) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Set the editor to use to open referenced files. |
||
| 468 | * |
||
| 469 | * Pass either the name of a configured editor, or a closure that directly |
||
| 470 | * resolves an editor string. |
||
| 471 | * |
||
| 472 | * @example |
||
| 473 | * $run->setEditor(function($file, $line) { return "file:///{$file}"; }); |
||
| 474 | * @example |
||
| 475 | * $run->setEditor('sublime'); |
||
| 476 | * |
||
| 477 | * @param string|callable $editor |
||
| 478 | * |
||
| 479 | * @throws InvalidArgumentException If invalid argument identifier provided |
||
| 480 | * |
||
| 481 | * @return static |
||
| 482 | */ |
||
| 483 | 4 | public function setEditor($editor) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Get the editor href for a given file and line, if available. |
||
| 498 | * |
||
| 499 | * @param string $filePath |
||
| 500 | * @param int $line |
||
| 501 | * |
||
| 502 | * @throws InvalidArgumentException If editor resolver does not return a string |
||
| 503 | * |
||
| 504 | * @return string|bool |
||
| 505 | */ |
||
| 506 | 4 | public function getEditorHref($filePath, $line) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Determine if the editor link should act as an Ajax request. |
||
| 530 | * |
||
| 531 | * @param string $filePath |
||
| 532 | * @param int $line |
||
| 533 | * |
||
| 534 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
| 535 | * |
||
| 536 | * @return bool |
||
| 537 | */ |
||
| 538 | 1 | public function getEditorAjax($filePath, $line) |
|
| 550 | |||
| 551 | /** |
||
| 552 | * Determines both the editor and if ajax should be used. |
||
| 553 | * |
||
| 554 | * @param string $filePath |
||
| 555 | * @param int $line |
||
| 556 | * |
||
| 557 | * @return array |
||
| 558 | */ |
||
| 559 | 1 | protected function getEditor($filePath, $line) |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Set the page title. |
||
| 601 | * |
||
| 602 | * @param string $title |
||
| 603 | * |
||
| 604 | * @return static |
||
| 605 | */ |
||
| 606 | 1 | public function setPageTitle($title) |
|
| 611 | |||
| 612 | /** |
||
| 613 | * Get the page title. |
||
| 614 | * |
||
| 615 | * @return string |
||
| 616 | */ |
||
| 617 | 1 | public function getPageTitle() |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Adds a path to the list of paths to be searched for resources. |
||
| 624 | * |
||
| 625 | * @param string $path |
||
| 626 | * |
||
| 627 | * @throws InvalidArgumentException If $path is not a valid directory |
||
| 628 | * |
||
| 629 | * @return static |
||
| 630 | */ |
||
| 631 | 2 | public function addResourcePath($path) |
|
| 642 | |||
| 643 | /** |
||
| 644 | * Adds a custom css file to be loaded. |
||
| 645 | * |
||
| 646 | * @param string|null $name |
||
| 647 | * |
||
| 648 | * @return static |
||
| 649 | */ |
||
| 650 | public function addCustomCss($name) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Adds a custom js file to be loaded. |
||
| 658 | * |
||
| 659 | * @param string|null $name |
||
| 660 | * |
||
| 661 | * @return static |
||
| 662 | */ |
||
| 663 | public function addCustomJs($name) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * @return array |
||
| 671 | */ |
||
| 672 | 1 | public function getResourcePaths() |
|
| 676 | |||
| 677 | /** |
||
| 678 | * Finds a resource, by its relative path, in all available search paths. |
||
| 679 | * |
||
| 680 | * The search is performed starting at the last search path, and all the |
||
| 681 | * way back to the first, enabling a cascading-type system of overrides for |
||
| 682 | * all resources. |
||
| 683 | * |
||
| 684 | * @param string $resource |
||
| 685 | * |
||
| 686 | * @throws RuntimeException If resource cannot be found in any of the available paths |
||
| 687 | * |
||
| 688 | * @return string |
||
| 689 | */ |
||
| 690 | protected function getResource($resource) |
||
| 716 | |||
| 717 | /** |
||
| 718 | * @deprecated |
||
| 719 | * |
||
| 720 | * @return string |
||
| 721 | */ |
||
| 722 | public function getResourcesPath() |
||
| 729 | |||
| 730 | /** |
||
| 731 | * @deprecated |
||
| 732 | * |
||
| 733 | * @param string $resourcesPath |
||
| 734 | * |
||
| 735 | * @return static |
||
| 736 | */ |
||
| 737 | public function setResourcesPath($resourcesPath) |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Return the application paths. |
||
| 745 | * |
||
| 746 | * @return array |
||
| 747 | */ |
||
| 748 | public function getApplicationPaths() |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Set the application paths. |
||
| 755 | * |
||
| 756 | * @param array $applicationPaths |
||
| 757 | * |
||
| 758 | * @return void |
||
| 759 | */ |
||
| 760 | public function setApplicationPaths($applicationPaths) |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Set the application root path. |
||
| 767 | * |
||
| 768 | * @param string $applicationRootPath |
||
| 769 | * |
||
| 770 | * @return void |
||
| 771 | */ |
||
| 772 | public function setApplicationRootPath($applicationRootPath) |
||
| 776 | |||
| 777 | /** |
||
| 778 | * blacklist a sensitive value within one of the superglobal arrays. |
||
| 779 | * Alias for the hideSuperglobalKey method. |
||
| 780 | * |
||
| 781 | * @param string $superGlobalName The name of the superglobal array, e.g. '_GET' |
||
| 782 | * @param string $key The key within the superglobal |
||
| 783 | * @see hideSuperglobalKey |
||
| 784 | * |
||
| 785 | * @return static |
||
| 786 | */ |
||
| 787 | 1 | public function blacklist($superGlobalName, $key) |
|
| 792 | |||
| 793 | /** |
||
| 794 | * Hide a sensitive value within one of the superglobal arrays. |
||
| 795 | * |
||
| 796 | * @param string $superGlobalName The name of the superglobal array, e.g. '_GET' |
||
| 797 | * @param string $key The key within the superglobal |
||
| 798 | * @return static |
||
| 799 | */ |
||
| 800 | public function hideSuperglobalKey($superGlobalName, $key) |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Checks all values within the given superGlobal array. |
||
| 807 | * |
||
| 808 | * Blacklisted values will be replaced by a equal length string containing |
||
| 809 | * only '*' characters for string values. |
||
| 810 | * Non-string values will be replaced with a fixed asterisk count. |
||
| 811 | * We intentionally dont rely on $GLOBALS as it depends on the 'auto_globals_jit' php.ini setting. |
||
| 812 | * |
||
| 813 | * @param array $superGlobal One of the superglobal arrays |
||
| 814 | * @param string $superGlobalName The name of the superglobal array, e.g. '_GET' |
||
| 815 | * |
||
| 816 | * @return array $values without sensitive data |
||
| 817 | */ |
||
| 818 | private function masked(array $superGlobal, $superGlobalName) |
||
| 832 | } |
||
| 833 |
Adding a
@returnannotation 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.