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 | /** | ||
| 21 | * Search paths to be scanned for resources, in the reverse | ||
| 22 | * order they're declared. | ||
| 23 | * | ||
| 24 | * @var array | ||
| 25 | */ | ||
| 26 | private $searchPaths = []; | ||
| 27 | |||
| 28 | /** | ||
| 29 | * Fast lookup cache for known resource locations. | ||
| 30 | * | ||
| 31 | * @var array | ||
| 32 | */ | ||
| 33 | private $resourceCache = []; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * The name of the custom css file. | ||
| 37 | * | ||
| 38 | * @var string | ||
| 39 | */ | ||
| 40 | private $customCss = null; | ||
| 41 | |||
| 42 | /** | ||
| 43 | * @var array[] | ||
| 44 | */ | ||
| 45 | private $extraTables = []; | ||
| 46 | |||
| 47 | /** | ||
| 48 | * @var bool | ||
| 49 | */ | ||
| 50 | private $handleUnconditionally = false; | ||
| 51 | |||
| 52 | /** | ||
| 53 | * @var string | ||
| 54 | */ | ||
| 55 | private $pageTitle = "Whoops! There was an error."; | ||
| 56 | |||
| 57 | /** | ||
| 58 | * @var array[] | ||
| 59 | */ | ||
| 60 | private $applicationPaths; | ||
| 61 | |||
| 62 | /** | ||
| 63 | * @var array[] | ||
| 64 | */ | ||
| 65 | private $blacklist = [ | ||
| 66 | '_GET' => [], | ||
| 67 | '_POST' => [], | ||
| 68 | '_FILES' => [], | ||
| 69 | '_COOKIE' => [], | ||
| 70 | '_SESSION' => [], | ||
| 71 | '_SERVER' => [], | ||
| 72 | '_ENV' => [], | ||
| 73 | ]; | ||
| 74 | |||
| 75 | /** | ||
| 76 | * A string identifier for a known IDE/text editor, or a closure | ||
| 77 | * that resolves a string that can be used to open a given file | ||
| 78 | * in an editor. If the string contains the special substrings | ||
| 79 | * %file or %line, they will be replaced with the correct data. | ||
| 80 | * | ||
| 81 | * @example | ||
| 82 | * "txmt://open?url=%file&line=%line" | ||
| 83 | * @var mixed $editor | ||
| 84 | */ | ||
| 85 | protected $editor; | ||
| 86 | |||
| 87 | /** | ||
| 88 | * A list of known editor strings | ||
| 89 | * @var array | ||
| 90 | */ | ||
| 91 | protected $editors = [ | ||
| 92 | "sublime" => "subl://open?url=file://%file&line=%line", | ||
| 93 | "textmate" => "txmt://open?url=file://%file&line=%line", | ||
| 94 | "emacs" => "emacs://open?url=file://%file&line=%line", | ||
| 95 | "macvim" => "mvim://open/?url=file://%file&line=%line", | ||
| 96 | "phpstorm" => "phpstorm://open?file=%file&line=%line", | ||
| 97 | "idea" => "idea://open?file=%file&line=%line", | ||
| 98 | "vscode" => "vscode://file/%file:%line", | ||
| 99 | "atom" => "atom://core/open/file?filename=%file&line=%line", | ||
| 100 | ]; | ||
| 101 | |||
| 102 | /** | ||
| 103 | * @var TemplateHelper | ||
| 104 | */ | ||
| 105 | private $templateHelper; | ||
| 106 | |||
| 107 | /** | ||
| 108 | * Constructor. | ||
| 109 | */ | ||
| 110 | 1 | public function __construct() | |
| 146 | |||
| 147 | /** | ||
| 148 | * @return int|null | ||
| 149 | */ | ||
| 150 | 1 | public function handle() | |
| 256 | |||
| 257 | /** | ||
| 258 | * Get the stack trace frames of the exception that is currently being handled. | ||
| 259 | * | ||
| 260 | * @return \Whoops\Exception\FrameCollection; | ||
| 261 | */ | ||
| 262 | protected function getExceptionFrames() | ||
| 279 | |||
| 280 | /** | ||
| 281 | * Get the code of the exception that is currently being handled. | ||
| 282 | * | ||
| 283 | * @return string | ||
| 284 | */ | ||
| 285 | protected function getExceptionCode() | ||
| 297 | |||
| 298 | /** | ||
| 299 | * @return string | ||
| 300 | */ | ||
| 301 | public function contentType() | ||
| 305 | |||
| 306 | /** | ||
| 307 | * Adds an entry to the list of tables displayed in the template. | ||
| 308 | * The expected data is a simple associative array. Any nested arrays | ||
| 309 | * will be flattened with print_r | ||
| 310 | * @param string $label | ||
| 311 | * @param array $data | ||
| 312 | */ | ||
| 313 | 1 | public function addDataTable($label, array $data) | |
| 317 | |||
| 318 | /** | ||
| 319 | * Lazily adds an entry to the list of tables displayed in the table. | ||
| 320 | * The supplied callback argument will be called when the error is rendered, | ||
| 321 | * it should produce a simple associative array. Any nested arrays will | ||
| 322 | * be flattened with print_r. | ||
| 323 | * | ||
| 324 | * @throws InvalidArgumentException If $callback is not callable | ||
| 325 | * @param string $label | ||
| 326 | * @param callable $callback Callable returning an associative array | ||
| 327 | */ | ||
| 328 | 1 | public function addDataTableCallback($label, /* callable */ $callback) | |
| 346 | |||
| 347 | /** | ||
| 348 | * Returns all the extra data tables registered with this handler. | ||
| 349 | * Optionally accepts a 'label' parameter, to only return the data | ||
| 350 | * table under that label. | ||
| 351 | * @param string|null $label | ||
| 352 | * @return array[]|callable | ||
| 353 | */ | ||
| 354 | 2 | public function getDataTables($label = null) | |
| 363 | |||
| 364 | /** | ||
| 365 | * Allows to disable all attempts to dynamically decide whether to | ||
| 366 | * handle or return prematurely. | ||
| 367 | * Set this to ensure that the handler will perform no matter what. | ||
| 368 | * @param bool|null $value | ||
| 369 | * @return bool|null | ||
| 370 | */ | ||
| 371 | 1 | public function handleUnconditionally($value = null) | |
| 379 | |||
| 380 | /** | ||
| 381 | * Adds an editor resolver, identified by a string | ||
| 382 | * name, and that may be a string path, or a callable | ||
| 383 | * resolver. If the callable returns a string, it will | ||
| 384 | * be set as the file reference's href attribute. | ||
| 385 | * | ||
| 386 | * @example | ||
| 387 |      *  $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") | ||
| 388 | * @example | ||
| 389 |      *   $run->addEditor('remove-it', function($file, $line) { | ||
| 390 | * unlink($file); | ||
| 391 | * return "http://stackoverflow.com"; | ||
| 392 | * }); | ||
| 393 | * @param string $identifier | ||
| 394 | * @param string $resolver | ||
| 395 | */ | ||
| 396 | 1 | public function addEditor($identifier, $resolver) | |
| 400 | |||
| 401 | /** | ||
| 402 | * Set the editor to use to open referenced files, by a string | ||
| 403 | * identifier, or a callable that will be executed for every | ||
| 404 | * file reference, with a $file and $line argument, and should | ||
| 405 | * return a string. | ||
| 406 | * | ||
| 407 | * @example | ||
| 408 |      *   $run->setEditor(function($file, $line) { return "file:///{$file}"; }); | ||
| 409 | * @example | ||
| 410 |      *   $run->setEditor('sublime'); | ||
| 411 | * | ||
| 412 | * @throws InvalidArgumentException If invalid argument identifier provided | ||
| 413 | * @param string|callable $editor | ||
| 414 | */ | ||
| 415 | 4 | public function setEditor($editor) | |
| 426 | |||
| 427 | /** | ||
| 428 | * Given a string file path, and an integer file line, | ||
| 429 | * executes the editor resolver and returns, if available, | ||
| 430 | * a string that may be used as the href property for that | ||
| 431 | * file reference. | ||
| 432 | * | ||
| 433 | * @throws InvalidArgumentException If editor resolver does not return a string | ||
| 434 | * @param string $filePath | ||
| 435 | * @param int $line | ||
| 436 | * @return string|bool | ||
| 437 | */ | ||
| 438 | 4 | public function getEditorHref($filePath, $line) | |
| 459 | |||
| 460 | /** | ||
| 461 | * Given a boolean if the editor link should | ||
| 462 | * act as an Ajax request. The editor must be a | ||
| 463 | * valid callable function/closure | ||
| 464 | * | ||
| 465 | * @throws UnexpectedValueException If editor resolver does not return a boolean | ||
| 466 | * @param string $filePath | ||
| 467 | * @param int $line | ||
| 468 | * @return bool | ||
| 469 | */ | ||
| 470 | 1 | public function getEditorAjax($filePath, $line) | |
| 482 | |||
| 483 | /** | ||
| 484 | * Given a boolean if the editor link should | ||
| 485 | * act as an Ajax request. The editor must be a | ||
| 486 | * valid callable function/closure | ||
| 487 | * | ||
| 488 | * @param string $filePath | ||
| 489 | * @param int $line | ||
| 490 | * @return array | ||
| 491 | */ | ||
| 492 | 1 | protected function getEditor($filePath, $line) | |
| 531 | |||
| 532 | /** | ||
| 533 | * @param string $title | ||
| 534 | * @return void | ||
| 535 | */ | ||
| 536 | 1 | public function setPageTitle($title) | |
| 540 | |||
| 541 | /** | ||
| 542 | * @return string | ||
| 543 | */ | ||
| 544 | 1 | public function getPageTitle() | |
| 548 | |||
| 549 | /** | ||
| 550 | * Adds a path to the list of paths to be searched for | ||
| 551 | * resources. | ||
| 552 | * | ||
| 553 | * @throws InvalidArgumentException If $path is not a valid directory | ||
| 554 | * | ||
| 555 | * @param string $path | ||
| 556 | * @return void | ||
| 557 | */ | ||
| 558 | 2 | public function addResourcePath($path) | |
| 568 | |||
| 569 | /** | ||
| 570 | * Adds a custom css file to be loaded. | ||
| 571 | * | ||
| 572 | * @param string $name | ||
| 573 | * @return void | ||
| 574 | */ | ||
| 575 | public function addCustomCss($name) | ||
| 579 | |||
| 580 | /** | ||
| 581 | * @return array | ||
| 582 | */ | ||
| 583 | 1 | public function getResourcePaths() | |
| 587 | |||
| 588 | /** | ||
| 589 | * Finds a resource, by its relative path, in all available search paths. | ||
| 590 | * The search is performed starting at the last search path, and all the | ||
| 591 | * way back to the first, enabling a cascading-type system of overrides | ||
| 592 | * for all resources. | ||
| 593 | * | ||
| 594 | * @throws RuntimeException If resource cannot be found in any of the available paths | ||
| 595 | * | ||
| 596 | * @param string $resource | ||
| 597 | * @return string | ||
| 598 | */ | ||
| 599 | protected function getResource($resource) | ||
| 625 | |||
| 626 | /** | ||
| 627 | * @deprecated | ||
| 628 | * | ||
| 629 | * @return string | ||
| 630 | */ | ||
| 631 | public function getResourcesPath() | ||
| 638 | |||
| 639 | /** | ||
| 640 | * @deprecated | ||
| 641 | * | ||
| 642 | * @param string $resourcesPath | ||
| 643 | * @return void | ||
| 644 | */ | ||
| 645 | public function setResourcesPath($resourcesPath) | ||
| 649 | |||
| 650 | /** | ||
| 651 | * Return the application paths. | ||
| 652 | * | ||
| 653 | * @return array | ||
| 654 | */ | ||
| 655 | public function getApplicationPaths() | ||
| 659 | |||
| 660 | /** | ||
| 661 | * Set the application paths. | ||
| 662 | * | ||
| 663 | * @param array $applicationPaths | ||
| 664 | */ | ||
| 665 | public function setApplicationPaths($applicationPaths) | ||
| 669 | |||
| 670 | /** | ||
| 671 | * Set the application root path. | ||
| 672 | * | ||
| 673 | * @param string $applicationRootPath | ||
| 674 | */ | ||
| 675 | public function setApplicationRootPath($applicationRootPath) | ||
| 679 | |||
| 680 | /** | ||
| 681 | * blacklist a sensitive value within one of the superglobal arrays. | ||
| 682 | * | ||
| 683 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' | ||
| 684 | * @param $key string the key within the superglobal | ||
| 685 | */ | ||
| 686 | 1 | public function blacklist($superGlobalName, $key) | |
| 690 | |||
| 691 | /** | ||
| 692 | * Checks all values within the given superGlobal array. | ||
| 693 | * Blacklisted values will be replaced by a equal length string cointaining only '*' characters. | ||
| 694 | * | ||
| 695 | * We intentionally dont rely on $GLOBALS as it depends on 'auto_globals_jit' php.ini setting. | ||
| 696 | * | ||
| 697 | * @param $superGlobal array One of the superglobal arrays | ||
| 698 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' | ||
| 699 | * @return array $values without sensitive data | ||
| 700 | */ | ||
| 701 | private function masked(array $superGlobal, $superGlobalName) | ||
| 715 | |||
| 716 | /** | ||
| 717 | * Check if a value matches a pattern. | ||
| 718 | * | ||
| 719 | * Based on Laravel's \Illuminate\Support\Str::is() method. | ||
| 720 | * Copyright (c) Taylor Otwell | ||
| 721 | * | ||
| 722 | * @param $pattern | ||
| 723 | * @param $value | ||
| 724 | * @return bool | ||
| 725 | */ | ||
| 726 | private function matchesPattern($pattern, $value) | ||
| 748 | } | ||
| 749 | 
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.