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, in the reverse |
||
| 33 | * order they're declared. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $searchPaths = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Fast lookup cache for known resource locations. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $resourceCache = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The name of the custom css file. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $customCss = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array[] |
||
| 55 | */ |
||
| 56 | private $extraTables = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | private $handleUnconditionally = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private $pageTitle = "Whoops! There was an error."; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array[] |
||
| 70 | */ |
||
| 71 | private $applicationPaths; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array[] |
||
| 75 | */ |
||
| 76 | private $blacklist = [ |
||
| 77 | '_GET' => [], |
||
| 78 | '_POST' => [], |
||
| 79 | '_FILES' => [], |
||
| 80 | '_COOKIE' => [], |
||
| 81 | '_SESSION' => [], |
||
| 82 | '_SERVER' => [], |
||
| 83 | '_ENV' => [], |
||
| 84 | ]; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * A string identifier for a known IDE/text editor, or a closure |
||
| 88 | * that resolves a string that can be used to open a given file |
||
| 89 | * in an editor. If the string contains the special substrings |
||
| 90 | * %file or %line, they will be replaced with the correct data. |
||
| 91 | * |
||
| 92 | * @example |
||
| 93 | * "txmt://open?url=%file&line=%line" |
||
| 94 | * @var mixed $editor |
||
| 95 | */ |
||
| 96 | protected $editor; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * A list of known editor strings |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | protected $editors = [ |
||
| 103 | "sublime" => "subl://open?url=file://%file&line=%line", |
||
| 104 | "textmate" => "txmt://open?url=file://%file&line=%line", |
||
| 105 | "emacs" => "emacs://open?url=file://%file&line=%line", |
||
| 106 | "macvim" => "mvim://open/?url=file://%file&line=%line", |
||
| 107 | "phpstorm" => "phpstorm://open?file=%file&line=%line", |
||
| 108 | "idea" => "idea://open?file=%file&line=%line", |
||
| 109 | "vscode" => "vscode://file/%file:%line", |
||
| 110 | "atom" => "atom://core/open/file?filename=%file&line=%line", |
||
| 111 | "espresso" => "x-espresso://open?filepath=%file&lines=%line", |
||
| 112 | ]; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var TemplateHelper |
||
| 116 | */ |
||
| 117 | private $templateHelper; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Constructor. |
||
| 121 | */ |
||
| 122 | 1 | public function __construct() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @return int|null |
||
| 164 | */ |
||
| 165 | 1 | public function handle() |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Get the stack trace frames of the exception that is currently being handled. |
||
| 274 | * |
||
| 275 | * @return \Whoops\Exception\FrameCollection; |
||
| 276 | */ |
||
| 277 | protected function getExceptionFrames() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get the code of the exception that is currently being handled. |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | protected function getExceptionCode() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public function contentType() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Adds an entry to the list of tables displayed in the template. |
||
| 323 | * The expected data is a simple associative array. Any nested arrays |
||
| 324 | * will be flattened with print_r |
||
| 325 | * @param string $label |
||
| 326 | * @param array $data |
||
| 327 | */ |
||
| 328 | 1 | public function addDataTable($label, array $data) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Lazily adds an entry to the list of tables displayed in the table. |
||
| 335 | * The supplied callback argument will be called when the error is rendered, |
||
| 336 | * it should produce a simple associative array. Any nested arrays will |
||
| 337 | * be flattened with print_r. |
||
| 338 | * |
||
| 339 | * @throws InvalidArgumentException If $callback is not callable |
||
| 340 | * @param string $label |
||
| 341 | * @param callable $callback Callable returning an associative array |
||
| 342 | */ |
||
| 343 | 1 | public function addDataTableCallback($label, /* callable */ $callback) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Returns all the extra data tables registered with this handler. |
||
| 364 | * Optionally accepts a 'label' parameter, to only return the data |
||
| 365 | * table under that label. |
||
| 366 | * @param string|null $label |
||
| 367 | * @return array[]|callable |
||
| 368 | */ |
||
| 369 | 2 | public function getDataTables($label = null) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Allows to disable all attempts to dynamically decide whether to |
||
| 381 | * handle or return prematurely. |
||
| 382 | * Set this to ensure that the handler will perform no matter what. |
||
| 383 | * @param bool|null $value |
||
| 384 | * @return bool|null |
||
| 385 | */ |
||
| 386 | 1 | public function handleUnconditionally($value = null) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Adds an editor resolver, identified by a string |
||
| 397 | * name, and that may be a string path, or a callable |
||
| 398 | * resolver. If the callable returns a string, it will |
||
| 399 | * be set as the file reference's href attribute. |
||
| 400 | * |
||
| 401 | * @example |
||
| 402 | * $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") |
||
| 403 | * @example |
||
| 404 | * $run->addEditor('remove-it', function($file, $line) { |
||
| 405 | * unlink($file); |
||
| 406 | * return "http://stackoverflow.com"; |
||
| 407 | * }); |
||
| 408 | * @param string $identifier |
||
| 409 | * @param string|callable $resolver |
||
| 410 | */ |
||
| 411 | 1 | public function addEditor($identifier, $resolver) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Set the editor to use to open referenced files, by a string |
||
| 418 | * identifier, or a callable that will be executed for every |
||
| 419 | * file reference, with a $file and $line argument, and should |
||
| 420 | * return a string. |
||
| 421 | * |
||
| 422 | * @example |
||
| 423 | * $run->setEditor(function($file, $line) { return "file:///{$file}"; }); |
||
| 424 | * @example |
||
| 425 | * $run->setEditor('sublime'); |
||
| 426 | * |
||
| 427 | * @throws InvalidArgumentException If invalid argument identifier provided |
||
| 428 | * @param string|callable $editor |
||
| 429 | */ |
||
| 430 | 4 | public function setEditor($editor) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Given a string file path, and an integer file line, |
||
| 444 | * executes the editor resolver and returns, if available, |
||
| 445 | * a string that may be used as the href property for that |
||
| 446 | * file reference. |
||
| 447 | * |
||
| 448 | * @throws InvalidArgumentException If editor resolver does not return a string |
||
| 449 | * @param string $filePath |
||
| 450 | * @param int $line |
||
| 451 | * @return string|bool |
||
| 452 | */ |
||
| 453 | 4 | public function getEditorHref($filePath, $line) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Given a boolean if the editor link should |
||
| 477 | * act as an Ajax request. The editor must be a |
||
| 478 | * valid callable function/closure |
||
| 479 | * |
||
| 480 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
| 481 | * @param string $filePath |
||
| 482 | * @param int $line |
||
| 483 | * @return bool |
||
| 484 | */ |
||
| 485 | 1 | public function getEditorAjax($filePath, $line) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Given a boolean if the editor link should |
||
| 500 | * act as an Ajax request. The editor must be a |
||
| 501 | * valid callable function/closure |
||
| 502 | * |
||
| 503 | * @param string $filePath |
||
| 504 | * @param int $line |
||
| 505 | * @return array |
||
| 506 | */ |
||
| 507 | 1 | protected function getEditor($filePath, $line) |
|
| 546 | |||
| 547 | /** |
||
| 548 | * @param string $title |
||
| 549 | * @return void |
||
| 550 | */ |
||
| 551 | 1 | public function setPageTitle($title) |
|
| 555 | |||
| 556 | /** |
||
| 557 | * @return string |
||
| 558 | */ |
||
| 559 | 1 | public function getPageTitle() |
|
| 563 | |||
| 564 | /** |
||
| 565 | * Adds a path to the list of paths to be searched for |
||
| 566 | * resources. |
||
| 567 | * |
||
| 568 | * @throws InvalidArgumentException If $path is not a valid directory |
||
| 569 | * |
||
| 570 | * @param string $path |
||
| 571 | * @return void |
||
| 572 | */ |
||
| 573 | 2 | public function addResourcePath($path) |
|
| 583 | |||
| 584 | /** |
||
| 585 | * Adds a custom css file to be loaded. |
||
| 586 | * |
||
| 587 | * @param string $name |
||
| 588 | * @return void |
||
| 589 | */ |
||
| 590 | public function addCustomCss($name) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @return array |
||
| 597 | */ |
||
| 598 | 1 | public function getResourcePaths() |
|
| 602 | |||
| 603 | /** |
||
| 604 | * Finds a resource, by its relative path, in all available search paths. |
||
| 605 | * The search is performed starting at the last search path, and all the |
||
| 606 | * way back to the first, enabling a cascading-type system of overrides |
||
| 607 | * for all resources. |
||
| 608 | * |
||
| 609 | * @throws RuntimeException If resource cannot be found in any of the available paths |
||
| 610 | * |
||
| 611 | * @param string $resource |
||
| 612 | * @return string |
||
| 613 | */ |
||
| 614 | protected function getResource($resource) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * @deprecated |
||
| 643 | * |
||
| 644 | * @return string |
||
| 645 | */ |
||
| 646 | public function getResourcesPath() |
||
| 653 | |||
| 654 | /** |
||
| 655 | * @deprecated |
||
| 656 | * |
||
| 657 | * @param string $resourcesPath |
||
| 658 | * @return void |
||
| 659 | */ |
||
| 660 | public function setResourcesPath($resourcesPath) |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Return the application paths. |
||
| 667 | * |
||
| 668 | * @return array |
||
| 669 | */ |
||
| 670 | public function getApplicationPaths() |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Set the application paths. |
||
| 677 | * |
||
| 678 | * @param array $applicationPaths |
||
| 679 | */ |
||
| 680 | public function setApplicationPaths($applicationPaths) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Set the application root path. |
||
| 687 | * |
||
| 688 | * @param string $applicationRootPath |
||
| 689 | */ |
||
| 690 | public function setApplicationRootPath($applicationRootPath) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * blacklist a sensitive value within one of the superglobal arrays. |
||
| 697 | * |
||
| 698 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' |
||
| 699 | * @param $key string the key within the superglobal |
||
| 700 | */ |
||
| 701 | 1 | public function blacklist($superGlobalName, $key) |
|
| 705 | |||
| 706 | /** |
||
| 707 | * Checks all values within the given superGlobal array. |
||
| 708 | * Blacklisted values will be replaced by a equal length string cointaining only '*' characters. |
||
| 709 | * |
||
| 710 | * We intentionally dont rely on $GLOBALS as it depends on 'auto_globals_jit' php.ini setting. |
||
| 711 | * |
||
| 712 | * @param $superGlobal array One of the superglobal arrays |
||
| 713 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' |
||
| 714 | * @return array $values without sensitive data |
||
| 715 | */ |
||
| 716 | private function masked(array $superGlobal, $superGlobalName) |
||
| 728 | } |
||
| 729 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.