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 | ]; | ||
| 100 | |||
| 101 | /** | ||
| 102 | * @var TemplateHelper | ||
| 103 | */ | ||
| 104 | private $templateHelper; | ||
| 105 | |||
| 106 | /** | ||
| 107 | * Constructor. | ||
| 108 | */ | ||
| 109 | 1 | public function __construct() | |
| 145 | |||
| 146 | /** | ||
| 147 | * @return int|null | ||
| 148 | */ | ||
| 149 | 1 | public function handle() | |
| 254 | |||
| 255 | /** | ||
| 256 | * Get frames. | ||
| 257 | * | ||
| 258 | * @return \Whoops\Exception\FrameCollection; | ||
| 259 | */ | ||
| 260 | protected function getFrames() | ||
| 277 | |||
| 278 | /** | ||
| 279 | * Get exception code. | ||
| 280 | * | ||
| 281 | * @return string | ||
| 282 | */ | ||
| 283 | protected function getCode() | ||
| 295 | |||
| 296 | /** | ||
| 297 | * @return string | ||
| 298 | */ | ||
| 299 | public function contentType() | ||
| 303 | |||
| 304 | /** | ||
| 305 | * Adds an entry to the list of tables displayed in the template. | ||
| 306 | * The expected data is a simple associative array. Any nested arrays | ||
| 307 | * will be flattened with print_r | ||
| 308 | * @param string $label | ||
| 309 | * @param array $data | ||
| 310 | */ | ||
| 311 | 1 | public function addDataTable($label, array $data) | |
| 315 | |||
| 316 | /** | ||
| 317 | * Lazily adds an entry to the list of tables displayed in the table. | ||
| 318 | * The supplied callback argument will be called when the error is rendered, | ||
| 319 | * it should produce a simple associative array. Any nested arrays will | ||
| 320 | * be flattened with print_r. | ||
| 321 | * | ||
| 322 | * @throws InvalidArgumentException If $callback is not callable | ||
| 323 | * @param string $label | ||
| 324 | * @param callable $callback Callable returning an associative array | ||
| 325 | */ | ||
| 326 | 1 | public function addDataTableCallback($label, /* callable */ $callback) | |
| 344 | |||
| 345 | /** | ||
| 346 | * Returns all the extra data tables registered with this handler. | ||
| 347 | * Optionally accepts a 'label' parameter, to only return the data | ||
| 348 | * table under that label. | ||
| 349 | * @param string|null $label | ||
| 350 | * @return array[]|callable | ||
| 351 | */ | ||
| 352 | 2 | public function getDataTables($label = null) | |
| 361 | |||
| 362 | /** | ||
| 363 | * Allows to disable all attempts to dynamically decide whether to | ||
| 364 | * handle or return prematurely. | ||
| 365 | * Set this to ensure that the handler will perform no matter what. | ||
| 366 | * @param bool|null $value | ||
| 367 | * @return bool|null | ||
| 368 | */ | ||
| 369 | 1 | public function handleUnconditionally($value = null) | |
| 377 | |||
| 378 | /** | ||
| 379 | * Adds an editor resolver, identified by a string | ||
| 380 | * name, and that may be a string path, or a callable | ||
| 381 | * resolver. If the callable returns a string, it will | ||
| 382 | * be set as the file reference's href attribute. | ||
| 383 | * | ||
| 384 | * @example | ||
| 385 |      *  $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") | ||
| 386 | * @example | ||
| 387 |      *   $run->addEditor('remove-it', function($file, $line) { | ||
| 388 | * unlink($file); | ||
| 389 | * return "http://stackoverflow.com"; | ||
| 390 | * }); | ||
| 391 | * @param string $identifier | ||
| 392 | * @param string $resolver | ||
| 393 | */ | ||
| 394 | 1 | public function addEditor($identifier, $resolver) | |
| 398 | |||
| 399 | /** | ||
| 400 | * Set the editor to use to open referenced files, by a string | ||
| 401 | * identifier, or a callable that will be executed for every | ||
| 402 | * file reference, with a $file and $line argument, and should | ||
| 403 | * return a string. | ||
| 404 | * | ||
| 405 | * @example | ||
| 406 |      *   $run->setEditor(function($file, $line) { return "file:///{$file}"; }); | ||
| 407 | * @example | ||
| 408 |      *   $run->setEditor('sublime'); | ||
| 409 | * | ||
| 410 | * @throws InvalidArgumentException If invalid argument identifier provided | ||
| 411 | * @param string|callable $editor | ||
| 412 | */ | ||
| 413 | 4 | public function setEditor($editor) | |
| 424 | |||
| 425 | /** | ||
| 426 | * Given a string file path, and an integer file line, | ||
| 427 | * executes the editor resolver and returns, if available, | ||
| 428 | * a string that may be used as the href property for that | ||
| 429 | * file reference. | ||
| 430 | * | ||
| 431 | * @throws InvalidArgumentException If editor resolver does not return a string | ||
| 432 | * @param string $filePath | ||
| 433 | * @param int $line | ||
| 434 | * @return string|bool | ||
| 435 | */ | ||
| 436 | 4 | public function getEditorHref($filePath, $line) | |
| 457 | |||
| 458 | /** | ||
| 459 | * Given a boolean if the editor link should | ||
| 460 | * act as an Ajax request. The editor must be a | ||
| 461 | * valid callable function/closure | ||
| 462 | * | ||
| 463 | * @throws UnexpectedValueException If editor resolver does not return a boolean | ||
| 464 | * @param string $filePath | ||
| 465 | * @param int $line | ||
| 466 | * @return bool | ||
| 467 | */ | ||
| 468 | 1 | public function getEditorAjax($filePath, $line) | |
| 480 | |||
| 481 | /** | ||
| 482 | * Given a boolean if the editor link should | ||
| 483 | * act as an Ajax request. The editor must be a | ||
| 484 | * valid callable function/closure | ||
| 485 | * | ||
| 486 | * @param string $filePath | ||
| 487 | * @param int $line | ||
| 488 | * @return array | ||
| 489 | */ | ||
| 490 | 1 | protected function getEditor($filePath, $line) | |
| 525 | |||
| 526 | /** | ||
| 527 | * @param string $title | ||
| 528 | * @return void | ||
| 529 | */ | ||
| 530 | 1 | public function setPageTitle($title) | |
| 534 | |||
| 535 | /** | ||
| 536 | * @return string | ||
| 537 | */ | ||
| 538 | 1 | public function getPageTitle() | |
| 542 | |||
| 543 | /** | ||
| 544 | * Adds a path to the list of paths to be searched for | ||
| 545 | * resources. | ||
| 546 | * | ||
| 547 | * @throws InvalidArgumentException If $path is not a valid directory | ||
| 548 | * | ||
| 549 | * @param string $path | ||
| 550 | * @return void | ||
| 551 | */ | ||
| 552 | 2 | public function addResourcePath($path) | |
| 562 | |||
| 563 | /** | ||
| 564 | * Adds a custom css file to be loaded. | ||
| 565 | * | ||
| 566 | * @param string $name | ||
| 567 | * @return void | ||
| 568 | */ | ||
| 569 | public function addCustomCss($name) | ||
| 573 | |||
| 574 | /** | ||
| 575 | * @return array | ||
| 576 | */ | ||
| 577 | 1 | public function getResourcePaths() | |
| 581 | |||
| 582 | /** | ||
| 583 | * Finds a resource, by its relative path, in all available search paths. | ||
| 584 | * The search is performed starting at the last search path, and all the | ||
| 585 | * way back to the first, enabling a cascading-type system of overrides | ||
| 586 | * for all resources. | ||
| 587 | * | ||
| 588 | * @throws RuntimeException If resource cannot be found in any of the available paths | ||
| 589 | * | ||
| 590 | * @param string $resource | ||
| 591 | * @return string | ||
| 592 | */ | ||
| 593 | protected function getResource($resource) | ||
| 619 | |||
| 620 | /** | ||
| 621 | * @deprecated | ||
| 622 | * | ||
| 623 | * @return string | ||
| 624 | */ | ||
| 625 | public function getResourcesPath() | ||
| 632 | |||
| 633 | /** | ||
| 634 | * @deprecated | ||
| 635 | * | ||
| 636 | * @param string $resourcesPath | ||
| 637 | * @return void | ||
| 638 | */ | ||
| 639 | public function setResourcesPath($resourcesPath) | ||
| 643 | |||
| 644 | /** | ||
| 645 | * Return the application paths. | ||
| 646 | * | ||
| 647 | * @return array | ||
| 648 | */ | ||
| 649 | public function getApplicationPaths() | ||
| 653 | |||
| 654 | /** | ||
| 655 | * Set the application paths. | ||
| 656 | * | ||
| 657 | * @param array $applicationPaths | ||
| 658 | */ | ||
| 659 | public function setApplicationPaths($applicationPaths) | ||
| 663 | |||
| 664 | /** | ||
| 665 | * Set the application root path. | ||
| 666 | * | ||
| 667 | * @param string $applicationRootPath | ||
| 668 | */ | ||
| 669 | public function setApplicationRootPath($applicationRootPath) | ||
| 673 | |||
| 674 | /** | ||
| 675 | * blacklist a sensitive value within one of the superglobal arrays. | ||
| 676 | * | ||
| 677 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' | ||
| 678 | * @param $key string the key within the superglobal | ||
| 679 | */ | ||
| 680 | 1 |     public function blacklist($superGlobalName, $key) { | |
| 683 | |||
| 684 | /** | ||
| 685 | * Checks all values within the given superGlobal array. | ||
| 686 | * Blacklisted values will be replaced by a equal length string cointaining only '*' characters. | ||
| 687 | * | ||
| 688 | * We intentionally dont rely on $GLOBALS as it depends on 'auto_globals_jit' php.ini setting. | ||
| 689 | * | ||
| 690 | * @param $superGlobal array One of the superglobal arrays | ||
| 691 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' | ||
| 692 | * @return array $values without sensitive data | ||
| 693 | */ | ||
| 694 |     private function getMasked(array $superGlobal, $superGlobalName) { | ||
| 705 | } | ||
| 706 | 
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.