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() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Get the stack trace frames of the exception that is currently being handled. |
||
| 254 | * |
||
| 255 | * @return \Whoops\Exception\FrameCollection; |
||
| 256 | */ |
||
| 257 | protected function getExceptionFrames() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get the code of the exception that is currently being handled. |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | protected function getExceptionCode() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public function contentType() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Adds an entry to the list of tables displayed in the template. |
||
| 303 | * The expected data is a simple associative array. Any nested arrays |
||
| 304 | * will be flattened with print_r |
||
| 305 | * @param string $label |
||
| 306 | * @param array $data |
||
| 307 | */ |
||
| 308 | 1 | public function addDataTable($label, array $data) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Lazily adds an entry to the list of tables displayed in the table. |
||
| 315 | * The supplied callback argument will be called when the error is rendered, |
||
| 316 | * it should produce a simple associative array. Any nested arrays will |
||
| 317 | * be flattened with print_r. |
||
| 318 | * |
||
| 319 | * @throws InvalidArgumentException If $callback is not callable |
||
| 320 | * @param string $label |
||
| 321 | * @param callable $callback Callable returning an associative array |
||
| 322 | */ |
||
| 323 | 1 | public function addDataTableCallback($label, /* callable */ $callback) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Returns all the extra data tables registered with this handler. |
||
| 344 | * Optionally accepts a 'label' parameter, to only return the data |
||
| 345 | * table under that label. |
||
| 346 | * @param string|null $label |
||
| 347 | * @return array[]|callable |
||
| 348 | */ |
||
| 349 | 2 | public function getDataTables($label = null) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Allows to disable all attempts to dynamically decide whether to |
||
| 361 | * handle or return prematurely. |
||
| 362 | * Set this to ensure that the handler will perform no matter what. |
||
| 363 | * @param bool|null $value |
||
| 364 | * @return bool|null |
||
| 365 | */ |
||
| 366 | 1 | public function handleUnconditionally($value = null) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Adds an editor resolver, identified by a string |
||
| 377 | * name, and that may be a string path, or a callable |
||
| 378 | * resolver. If the callable returns a string, it will |
||
| 379 | * be set as the file reference's href attribute. |
||
| 380 | * |
||
| 381 | * @example |
||
| 382 | * $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") |
||
| 383 | * @example |
||
| 384 | * $run->addEditor('remove-it', function($file, $line) { |
||
| 385 | * unlink($file); |
||
| 386 | * return "http://stackoverflow.com"; |
||
| 387 | * }); |
||
| 388 | * @param string $identifier |
||
| 389 | * @param string $resolver |
||
| 390 | */ |
||
| 391 | 1 | public function addEditor($identifier, $resolver) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Set the editor to use to open referenced files, by a string |
||
| 398 | * identifier, or a callable that will be executed for every |
||
| 399 | * file reference, with a $file and $line argument, and should |
||
| 400 | * return a string. |
||
| 401 | * |
||
| 402 | * @example |
||
| 403 | * $run->setEditor(function($file, $line) { return "file:///{$file}"; }); |
||
| 404 | * @example |
||
| 405 | * $run->setEditor('sublime'); |
||
| 406 | * |
||
| 407 | * @throws InvalidArgumentException If invalid argument identifier provided |
||
| 408 | * @param string|callable $editor |
||
| 409 | */ |
||
| 410 | 4 | public function setEditor($editor) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Given a string file path, and an integer file line, |
||
| 424 | * executes the editor resolver and returns, if available, |
||
| 425 | * a string that may be used as the href property for that |
||
| 426 | * file reference. |
||
| 427 | * |
||
| 428 | * @throws InvalidArgumentException If editor resolver does not return a string |
||
| 429 | * @param string $filePath |
||
| 430 | * @param int $line |
||
| 431 | * @return string|bool |
||
| 432 | */ |
||
| 433 | 4 | public function getEditorHref($filePath, $line) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Given a boolean if the editor link should |
||
| 457 | * act as an Ajax request. The editor must be a |
||
| 458 | * valid callable function/closure |
||
| 459 | * |
||
| 460 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
| 461 | * @param string $filePath |
||
| 462 | * @param int $line |
||
| 463 | * @return bool |
||
| 464 | */ |
||
| 465 | 1 | public function getEditorAjax($filePath, $line) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Given a boolean if the editor link should |
||
| 480 | * act as an Ajax request. The editor must be a |
||
| 481 | * valid callable function/closure |
||
| 482 | * |
||
| 483 | * @param string $filePath |
||
| 484 | * @param int $line |
||
| 485 | * @return array |
||
| 486 | */ |
||
| 487 | 1 | protected function getEditor($filePath, $line) |
|
| 522 | |||
| 523 | /** |
||
| 524 | * @param string $title |
||
| 525 | * @return void |
||
| 526 | */ |
||
| 527 | 1 | public function setPageTitle($title) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * @return string |
||
| 534 | */ |
||
| 535 | 1 | public function getPageTitle() |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Adds a path to the list of paths to be searched for |
||
| 542 | * resources. |
||
| 543 | * |
||
| 544 | * @throws InvalidArgumentException If $path is not a valid directory |
||
| 545 | * |
||
| 546 | * @param string $path |
||
| 547 | * @return void |
||
| 548 | */ |
||
| 549 | 2 | public function addResourcePath($path) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Adds a custom css file to be loaded. |
||
| 562 | * |
||
| 563 | * @param string $name |
||
| 564 | * @return void |
||
| 565 | */ |
||
| 566 | public function addCustomCss($name) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @return array |
||
| 573 | */ |
||
| 574 | 1 | public function getResourcePaths() |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Finds a resource, by its relative path, in all available search paths. |
||
| 581 | * The search is performed starting at the last search path, and all the |
||
| 582 | * way back to the first, enabling a cascading-type system of overrides |
||
| 583 | * for all resources. |
||
| 584 | * |
||
| 585 | * @throws RuntimeException If resource cannot be found in any of the available paths |
||
| 586 | * |
||
| 587 | * @param string $resource |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | protected function getResource($resource) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * @deprecated |
||
| 619 | * |
||
| 620 | * @return string |
||
| 621 | */ |
||
| 622 | public function getResourcesPath() |
||
| 629 | |||
| 630 | /** |
||
| 631 | * @deprecated |
||
| 632 | * |
||
| 633 | * @param string $resourcesPath |
||
| 634 | * @return void |
||
| 635 | */ |
||
| 636 | public function setResourcesPath($resourcesPath) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Return the application paths. |
||
| 643 | * |
||
| 644 | * @return array |
||
| 645 | */ |
||
| 646 | public function getApplicationPaths() |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Set the application paths. |
||
| 653 | * |
||
| 654 | * @param array $applicationPaths |
||
| 655 | */ |
||
| 656 | public function setApplicationPaths($applicationPaths) |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Set the application root path. |
||
| 663 | * |
||
| 664 | * @param string $applicationRootPath |
||
| 665 | */ |
||
| 666 | public function setApplicationRootPath($applicationRootPath) |
||
| 670 | |||
| 671 | /** |
||
| 672 | * blacklist a sensitive value within one of the superglobal arrays. |
||
| 673 | * |
||
| 674 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' |
||
| 675 | * @param $key string the key within the superglobal |
||
| 676 | */ |
||
| 677 | 1 | public function blacklist($superGlobalName, $key) { |
|
| 680 | |||
| 681 | /** |
||
| 682 | * Checks all values within the given superGlobal array. |
||
| 683 | * Blacklisted values will be replaced by a equal length string cointaining only '*' characters. |
||
| 684 | * |
||
| 685 | * We intentionally dont rely on $GLOBALS as it depends on 'auto_globals_jit' php.ini setting. |
||
| 686 | * |
||
| 687 | * @param $superGlobal array One of the superglobal arrays |
||
| 688 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' |
||
| 689 | * @return array $values without sensitive data |
||
| 690 | */ |
||
| 691 | private function masked(array $superGlobal, $superGlobalName) { |
||
| 702 | } |
||
| 703 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.