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 | /** |
||
77 | * A string identifier for a known IDE/text editor, or a closure |
||
78 | * that resolves a string that can be used to open a given file |
||
79 | * in an editor. If the string contains the special substrings |
||
80 | * %file or %line, they will be replaced with the correct data. |
||
81 | * |
||
82 | * @example |
||
83 | * "txmt://open?url=%file&line=%line" |
||
84 | * @var mixed $editor |
||
85 | */ |
||
86 | protected $editor; |
||
87 | |||
88 | /** |
||
89 | * A list of known editor strings |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $editors = [ |
||
93 | "sublime" => "subl://open?url=file://%file&line=%line", |
||
94 | "textmate" => "txmt://open?url=file://%file&line=%line", |
||
95 | "emacs" => "emacs://open?url=file://%file&line=%line", |
||
96 | "macvim" => "mvim://open/?url=file://%file&line=%line", |
||
97 | "phpstorm" => "phpstorm://open?file=%file&line=%line", |
||
98 | "idea" => "idea://open?file=%file&line=%line", |
||
99 | "vscode" => "vscode://file/%file:%line", |
||
100 | ]; |
||
101 | |||
102 | /** |
||
103 | * @var TemplateHelper |
||
104 | */ |
||
105 | private $templateHelper; |
||
106 | |||
107 | /** |
||
108 | * Constructor. |
||
109 | */ |
||
110 | 2 | public function __construct() |
|
146 | |||
147 | /** |
||
148 | * @return int|null |
||
149 | */ |
||
150 | 1 | public function handle() |
|
252 | |||
253 | /** |
||
254 | * Get the stack trace frames of the exception that is currently being handled. |
||
255 | * |
||
256 | * @return \Whoops\Exception\FrameCollection; |
||
257 | */ |
||
258 | protected function getExceptionFrames() |
||
275 | |||
276 | /** |
||
277 | * Get the code of the exception that is currently being handled. |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | protected function getExceptionCode() |
||
293 | |||
294 | /** |
||
295 | * @return string |
||
296 | */ |
||
297 | public function contentType() |
||
301 | |||
302 | /** |
||
303 | * Adds an entry to the list of tables displayed in the template. |
||
304 | * The expected data is a simple associative array. Any nested arrays |
||
305 | * will be flattened with print_r |
||
306 | * @param string $label |
||
307 | * @param array $data |
||
308 | */ |
||
309 | 1 | public function addDataTable($label, array $data) |
|
313 | |||
314 | /** |
||
315 | * Lazily adds an entry to the list of tables displayed in the table. |
||
316 | * The supplied callback argument will be called when the error is rendered, |
||
317 | * it should produce a simple associative array. Any nested arrays will |
||
318 | * be flattened with print_r. |
||
319 | * |
||
320 | * @throws InvalidArgumentException If $callback is not callable |
||
321 | * @param string $label |
||
322 | * @param callable $callback Callable returning an associative array |
||
323 | */ |
||
324 | 1 | public function addDataTableCallback($label, /* callable */ $callback) |
|
342 | |||
343 | /** |
||
344 | * Returns all the extra data tables registered with this handler. |
||
345 | * Optionally accepts a 'label' parameter, to only return the data |
||
346 | * table under that label. |
||
347 | * @param string|null $label |
||
348 | * @return array[]|callable |
||
349 | */ |
||
350 | 2 | public function getDataTables($label = null) |
|
359 | |||
360 | /** |
||
361 | * Allows to disable all attempts to dynamically decide whether to |
||
362 | * handle or return prematurely. |
||
363 | * Set this to ensure that the handler will perform no matter what. |
||
364 | * @param bool|null $value |
||
365 | * @return bool|null |
||
366 | */ |
||
367 | 2 | public function handleUnconditionally($value = null) |
|
375 | |||
376 | /** |
||
377 | * Adds an editor resolver, identified by a string |
||
378 | * name, and that may be a string path, or a callable |
||
379 | * resolver. If the callable returns a string, it will |
||
380 | * be set as the file reference's href attribute. |
||
381 | * |
||
382 | * @example |
||
383 | * $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") |
||
384 | * @example |
||
385 | * $run->addEditor('remove-it', function($file, $line) { |
||
386 | * unlink($file); |
||
387 | * return "http://stackoverflow.com"; |
||
388 | * }); |
||
389 | * @param string $identifier |
||
390 | * @param string $resolver |
||
391 | */ |
||
392 | 1 | public function addEditor($identifier, $resolver) |
|
396 | |||
397 | /** |
||
398 | * Set the editor to use to open referenced files, by a string |
||
399 | * identifier, or a callable that will be executed for every |
||
400 | * file reference, with a $file and $line argument, and should |
||
401 | * return a string. |
||
402 | * |
||
403 | * @example |
||
404 | * $run->setEditor(function($file, $line) { return "file:///{$file}"; }); |
||
405 | * @example |
||
406 | * $run->setEditor('sublime'); |
||
407 | * |
||
408 | * @throws InvalidArgumentException If invalid argument identifier provided |
||
409 | * @param string|callable $editor |
||
410 | */ |
||
411 | 4 | public function setEditor($editor) |
|
422 | |||
423 | /** |
||
424 | * Given a string file path, and an integer file line, |
||
425 | * executes the editor resolver and returns, if available, |
||
426 | * a string that may be used as the href property for that |
||
427 | * file reference. |
||
428 | * |
||
429 | * @throws InvalidArgumentException If editor resolver does not return a string |
||
430 | * @param string $filePath |
||
431 | * @param int $line |
||
432 | * @return string|bool |
||
433 | */ |
||
434 | 4 | public function getEditorHref($filePath, $line) |
|
455 | |||
456 | /** |
||
457 | * Given a boolean if the editor link should |
||
458 | * act as an Ajax request. The editor must be a |
||
459 | * valid callable function/closure |
||
460 | * |
||
461 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
462 | * @param string $filePath |
||
463 | * @param int $line |
||
464 | * @return bool |
||
465 | */ |
||
466 | 1 | public function getEditorAjax($filePath, $line) |
|
478 | |||
479 | /** |
||
480 | * Given a boolean if the editor link should |
||
481 | * act as an Ajax request. The editor must be a |
||
482 | * valid callable function/closure |
||
483 | * |
||
484 | * @param string $filePath |
||
485 | * @param int $line |
||
486 | * @return array |
||
487 | */ |
||
488 | 1 | protected function getEditor($filePath, $line) |
|
523 | |||
524 | /** |
||
525 | * @param string $title |
||
526 | * @return void |
||
527 | */ |
||
528 | 1 | public function setPageTitle($title) |
|
532 | |||
533 | /** |
||
534 | * @return string |
||
535 | */ |
||
536 | 1 | public function getPageTitle() |
|
540 | |||
541 | /** |
||
542 | * Adds a path to the list of paths to be searched for |
||
543 | * resources. |
||
544 | * |
||
545 | * @throws InvalidArgumentException If $path is not a valid directory |
||
546 | * |
||
547 | * @param string $path |
||
548 | * @return void |
||
549 | */ |
||
550 | 2 | public function addResourcePath($path) |
|
560 | |||
561 | /** |
||
562 | * Adds a custom css file to be loaded. |
||
563 | * |
||
564 | * @param string $name |
||
565 | * @return void |
||
566 | */ |
||
567 | public function addCustomCss($name) |
||
571 | |||
572 | /** |
||
573 | * @return array |
||
574 | */ |
||
575 | 1 | public function getResourcePaths() |
|
579 | |||
580 | /** |
||
581 | * Finds a resource, by its relative path, in all available search paths. |
||
582 | * The search is performed starting at the last search path, and all the |
||
583 | * way back to the first, enabling a cascading-type system of overrides |
||
584 | * for all resources. |
||
585 | * |
||
586 | * @throws RuntimeException If resource cannot be found in any of the available paths |
||
587 | * |
||
588 | * @param string $resource |
||
589 | * @return string |
||
590 | */ |
||
591 | protected function getResource($resource) |
||
617 | |||
618 | /** |
||
619 | * @deprecated |
||
620 | * |
||
621 | * @return string |
||
622 | */ |
||
623 | public function getResourcesPath() |
||
630 | |||
631 | /** |
||
632 | * @deprecated |
||
633 | * |
||
634 | * @param string $resourcesPath |
||
635 | * @return void |
||
636 | */ |
||
637 | public function setResourcesPath($resourcesPath) |
||
641 | |||
642 | /** |
||
643 | * Return the application paths. |
||
644 | * |
||
645 | * @return array |
||
646 | */ |
||
647 | public function getApplicationPaths() |
||
651 | |||
652 | /** |
||
653 | * Set the application paths. |
||
654 | * |
||
655 | * @param array $applicationPaths |
||
656 | */ |
||
657 | public function setApplicationPaths($applicationPaths) |
||
661 | |||
662 | /** |
||
663 | * Set the application root path. |
||
664 | * |
||
665 | * @param string $applicationRootPath |
||
666 | */ |
||
667 | public function setApplicationRootPath($applicationRootPath) |
||
671 | |||
672 | /** |
||
673 | * blacklist a sensitive value within one of the superglobal arrays. |
||
674 | * |
||
675 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' |
||
676 | * @param $key string the key within the superglobal |
||
677 | */ |
||
678 | 2 | public function blacklist($superGlobalName, $key) |
|
682 | |||
683 | /** |
||
684 | * Get the blacklisted values for the given superGlobal |
||
685 | * |
||
686 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' |
||
687 | * @return array |
||
688 | */ |
||
689 | 1 | public function getBlacklistForSuperGlobal($superGlobalName) |
|
696 | |||
697 | /** |
||
698 | * Checks all values within the given superGlobal array. |
||
699 | * Blacklisted values will be replaced by a equal length string cointaining only '*' characters. |
||
700 | * |
||
701 | * We intentionally dont rely on $GLOBALS as it depends on 'auto_globals_jit' php.ini setting. |
||
702 | * |
||
703 | * @param $superGlobal array One of the superglobal arrays |
||
704 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' |
||
705 | * @return array $values without sensitive data |
||
706 | */ |
||
707 | private function masked(array $superGlobal, $superGlobalName) |
||
719 | } |
||
720 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.