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() |
|
254 | |||
255 | /** |
||
256 | * Get the stack trace frames of the exception that is currently being handled. |
||
257 | * |
||
258 | * @return \Whoops\Exception\FrameCollection; |
||
259 | */ |
||
260 | protected function getExceptionFrames() |
||
277 | |||
278 | /** |
||
279 | * Get the code of the exception that is currently being handled. |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | protected function getExceptionCode() |
||
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) |
|
529 | |||
530 | /** |
||
531 | * @param string $title |
||
532 | * @return void |
||
533 | */ |
||
534 | 1 | public function setPageTitle($title) |
|
538 | |||
539 | /** |
||
540 | * @return string |
||
541 | */ |
||
542 | 1 | public function getPageTitle() |
|
546 | |||
547 | /** |
||
548 | * Adds a path to the list of paths to be searched for |
||
549 | * resources. |
||
550 | * |
||
551 | * @throws InvalidArgumentException If $path is not a valid directory |
||
552 | * |
||
553 | * @param string $path |
||
554 | * @return void |
||
555 | */ |
||
556 | 2 | public function addResourcePath($path) |
|
566 | |||
567 | /** |
||
568 | * Adds a custom css file to be loaded. |
||
569 | * |
||
570 | * @param string $name |
||
571 | * @return void |
||
572 | */ |
||
573 | public function addCustomCss($name) |
||
577 | |||
578 | /** |
||
579 | * @return array |
||
580 | */ |
||
581 | 1 | public function getResourcePaths() |
|
585 | |||
586 | /** |
||
587 | * Finds a resource, by its relative path, in all available search paths. |
||
588 | * The search is performed starting at the last search path, and all the |
||
589 | * way back to the first, enabling a cascading-type system of overrides |
||
590 | * for all resources. |
||
591 | * |
||
592 | * @throws RuntimeException If resource cannot be found in any of the available paths |
||
593 | * |
||
594 | * @param string $resource |
||
595 | * @return string |
||
596 | */ |
||
597 | protected function getResource($resource) |
||
623 | |||
624 | /** |
||
625 | * @deprecated |
||
626 | * |
||
627 | * @return string |
||
628 | */ |
||
629 | public function getResourcesPath() |
||
636 | |||
637 | /** |
||
638 | * @deprecated |
||
639 | * |
||
640 | * @param string $resourcesPath |
||
641 | * @return void |
||
642 | */ |
||
643 | public function setResourcesPath($resourcesPath) |
||
647 | |||
648 | /** |
||
649 | * Return the application paths. |
||
650 | * |
||
651 | * @return array |
||
652 | */ |
||
653 | public function getApplicationPaths() |
||
657 | |||
658 | /** |
||
659 | * Set the application paths. |
||
660 | * |
||
661 | * @param array $applicationPaths |
||
662 | */ |
||
663 | public function setApplicationPaths($applicationPaths) |
||
667 | |||
668 | /** |
||
669 | * Set the application root path. |
||
670 | * |
||
671 | * @param string $applicationRootPath |
||
672 | */ |
||
673 | public function setApplicationRootPath($applicationRootPath) |
||
677 | |||
678 | /** |
||
679 | * blacklist a sensitive value within one of the superglobal arrays. |
||
680 | * |
||
681 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' |
||
682 | * @param $key string the key within the superglobal |
||
683 | */ |
||
684 | 1 | public function blacklist($superGlobalName, $key) |
|
688 | |||
689 | /** |
||
690 | * Checks all values within the given superGlobal array. |
||
691 | * Blacklisted values will be replaced by a equal length string cointaining only '*' characters. |
||
692 | * |
||
693 | * We intentionally dont rely on $GLOBALS as it depends on 'auto_globals_jit' php.ini setting. |
||
694 | * |
||
695 | * @param $superGlobal array One of the superglobal arrays |
||
696 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' |
||
697 | * @return array $values without sensitive data |
||
698 | */ |
||
699 | private function masked(array $superGlobal, $superGlobalName) |
||
711 | } |
||
712 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.