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() |
|
249 | |||
250 | /** |
||
251 | * Get the stack trace frames of the exception that is currently being handled. |
||
252 | * |
||
253 | * @return \Whoops\Exception\FrameCollection; |
||
254 | */ |
||
255 | protected function getExceptionFrames() |
||
272 | |||
273 | /** |
||
274 | * Get the code of the exception that is currently being handled. |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | protected function getExceptionCode() |
||
290 | |||
291 | /** |
||
292 | * Get exception being handled as plain text. |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | protected function getExceptionPlainText() |
||
304 | |||
305 | /** |
||
306 | * @return string |
||
307 | */ |
||
308 | public function contentType() |
||
312 | |||
313 | /** |
||
314 | * Adds an entry to the list of tables displayed in the template. |
||
315 | * The expected data is a simple associative array. Any nested arrays |
||
316 | * will be flattened with print_r |
||
317 | * @param string $label |
||
318 | * @param array $data |
||
319 | */ |
||
320 | 1 | public function addDataTable($label, array $data) |
|
324 | |||
325 | /** |
||
326 | * Lazily adds an entry to the list of tables displayed in the table. |
||
327 | * The supplied callback argument will be called when the error is rendered, |
||
328 | * it should produce a simple associative array. Any nested arrays will |
||
329 | * be flattened with print_r. |
||
330 | * |
||
331 | * @throws InvalidArgumentException If $callback is not callable |
||
332 | * @param string $label |
||
333 | * @param callable $callback Callable returning an associative array |
||
334 | */ |
||
335 | 1 | public function addDataTableCallback($label, /* callable */ $callback) |
|
353 | |||
354 | /** |
||
355 | * Returns all the extra data tables registered with this handler. |
||
356 | * Optionally accepts a 'label' parameter, to only return the data |
||
357 | * table under that label. |
||
358 | * @param string|null $label |
||
359 | * @return array[]|callable |
||
360 | */ |
||
361 | 2 | public function getDataTables($label = null) |
|
370 | |||
371 | /** |
||
372 | * Allows to disable all attempts to dynamically decide whether to |
||
373 | * handle or return prematurely. |
||
374 | * Set this to ensure that the handler will perform no matter what. |
||
375 | * @param bool|null $value |
||
376 | * @return bool|null |
||
377 | */ |
||
378 | 1 | public function handleUnconditionally($value = null) |
|
386 | |||
387 | /** |
||
388 | * Adds an editor resolver, identified by a string |
||
389 | * name, and that may be a string path, or a callable |
||
390 | * resolver. If the callable returns a string, it will |
||
391 | * be set as the file reference's href attribute. |
||
392 | * |
||
393 | * @example |
||
394 | * $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") |
||
395 | * @example |
||
396 | * $run->addEditor('remove-it', function($file, $line) { |
||
397 | * unlink($file); |
||
398 | * return "http://stackoverflow.com"; |
||
399 | * }); |
||
400 | * @param string $identifier |
||
401 | * @param string $resolver |
||
402 | */ |
||
403 | 1 | public function addEditor($identifier, $resolver) |
|
407 | |||
408 | /** |
||
409 | * Set the editor to use to open referenced files, by a string |
||
410 | * identifier, or a callable that will be executed for every |
||
411 | * file reference, with a $file and $line argument, and should |
||
412 | * return a string. |
||
413 | * |
||
414 | * @example |
||
415 | * $run->setEditor(function($file, $line) { return "file:///{$file}"; }); |
||
416 | * @example |
||
417 | * $run->setEditor('sublime'); |
||
418 | * |
||
419 | * @throws InvalidArgumentException If invalid argument identifier provided |
||
420 | * @param string|callable $editor |
||
421 | */ |
||
422 | 4 | public function setEditor($editor) |
|
433 | |||
434 | /** |
||
435 | * Given a string file path, and an integer file line, |
||
436 | * executes the editor resolver and returns, if available, |
||
437 | * a string that may be used as the href property for that |
||
438 | * file reference. |
||
439 | * |
||
440 | * @throws InvalidArgumentException If editor resolver does not return a string |
||
441 | * @param string $filePath |
||
442 | * @param int $line |
||
443 | * @return string|bool |
||
444 | */ |
||
445 | 4 | public function getEditorHref($filePath, $line) |
|
466 | |||
467 | /** |
||
468 | * Given a boolean if the editor link should |
||
469 | * act as an Ajax request. The editor must be a |
||
470 | * valid callable function/closure |
||
471 | * |
||
472 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
473 | * @param string $filePath |
||
474 | * @param int $line |
||
475 | * @return bool |
||
476 | */ |
||
477 | 1 | public function getEditorAjax($filePath, $line) |
|
489 | |||
490 | /** |
||
491 | * Given a boolean if the editor link should |
||
492 | * act as an Ajax request. The editor must be a |
||
493 | * valid callable function/closure |
||
494 | * |
||
495 | * @param string $filePath |
||
496 | * @param int $line |
||
497 | * @return array |
||
498 | */ |
||
499 | 1 | protected function getEditor($filePath, $line) |
|
534 | |||
535 | /** |
||
536 | * @param string $title |
||
537 | * @return void |
||
538 | */ |
||
539 | 1 | public function setPageTitle($title) |
|
543 | |||
544 | /** |
||
545 | * @return string |
||
546 | */ |
||
547 | 1 | public function getPageTitle() |
|
551 | |||
552 | /** |
||
553 | * Adds a path to the list of paths to be searched for |
||
554 | * resources. |
||
555 | * |
||
556 | * @throws InvalidArgumentException If $path is not a valid directory |
||
557 | * |
||
558 | * @param string $path |
||
559 | * @return void |
||
560 | */ |
||
561 | 2 | public function addResourcePath($path) |
|
571 | |||
572 | /** |
||
573 | * Adds a custom css file to be loaded. |
||
574 | * |
||
575 | * @param string $name |
||
576 | * @return void |
||
577 | */ |
||
578 | public function addCustomCss($name) |
||
582 | |||
583 | /** |
||
584 | * @return array |
||
585 | */ |
||
586 | 1 | public function getResourcePaths() |
|
590 | |||
591 | /** |
||
592 | * Finds a resource, by its relative path, in all available search paths. |
||
593 | * The search is performed starting at the last search path, and all the |
||
594 | * way back to the first, enabling a cascading-type system of overrides |
||
595 | * for all resources. |
||
596 | * |
||
597 | * @throws RuntimeException If resource cannot be found in any of the available paths |
||
598 | * |
||
599 | * @param string $resource |
||
600 | * @return string |
||
601 | */ |
||
602 | protected function getResource($resource) |
||
628 | |||
629 | /** |
||
630 | * @deprecated |
||
631 | * |
||
632 | * @return string |
||
633 | */ |
||
634 | public function getResourcesPath() |
||
641 | |||
642 | /** |
||
643 | * @deprecated |
||
644 | * |
||
645 | * @param string $resourcesPath |
||
646 | * @return void |
||
647 | */ |
||
648 | public function setResourcesPath($resourcesPath) |
||
652 | |||
653 | /** |
||
654 | * Return the application paths. |
||
655 | * |
||
656 | * @return array |
||
657 | */ |
||
658 | public function getApplicationPaths() |
||
662 | |||
663 | /** |
||
664 | * Set the application paths. |
||
665 | * |
||
666 | * @param array $applicationPaths |
||
667 | */ |
||
668 | public function setApplicationPaths($applicationPaths) |
||
672 | |||
673 | /** |
||
674 | * Set the application root path. |
||
675 | * |
||
676 | * @param string $applicationRootPath |
||
677 | */ |
||
678 | public function setApplicationRootPath($applicationRootPath) |
||
682 | |||
683 | /** |
||
684 | * blacklist a sensitive value within one of the superglobal arrays. |
||
685 | * |
||
686 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' |
||
687 | * @param $key string the key within the superglobal |
||
688 | */ |
||
689 | 1 | public function blacklist($superGlobalName, $key) { |
|
692 | |||
693 | /** |
||
694 | * Checks all values within the given superGlobal array. |
||
695 | * Blacklisted values will be replaced by a equal length string cointaining only '*' characters. |
||
696 | * |
||
697 | * We intentionally dont rely on $GLOBALS as it depends on 'auto_globals_jit' php.ini setting. |
||
698 | * |
||
699 | * @param $superGlobal array One of the superglobal arrays |
||
700 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' |
||
701 | * @return array $values without sensitive data |
||
702 | */ |
||
703 | private function masked(array $superGlobal, $superGlobalName) { |
||
714 | } |
||
715 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.