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 | ]; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Constructor. |
||
| 102 | */ |
||
| 103 | 1 | public function __construct() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @return int|null |
||
| 122 | */ |
||
| 123 | 1 | public function handle() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function contentType() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Adds an entry to the list of tables displayed in the template. |
||
| 276 | * The expected data is a simple associative array. Any nested arrays |
||
| 277 | * will be flattened with print_r |
||
| 278 | * @param string $label |
||
| 279 | * @param array $data |
||
| 280 | */ |
||
| 281 | 1 | public function addDataTable($label, array $data) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Lazily adds an entry to the list of tables displayed in the table. |
||
| 288 | * The supplied callback argument will be called when the error is rendered, |
||
| 289 | * it should produce a simple associative array. Any nested arrays will |
||
| 290 | * be flattened with print_r. |
||
| 291 | * |
||
| 292 | * @throws InvalidArgumentException If $callback is not callable |
||
| 293 | * @param string $label |
||
| 294 | * @param callable $callback Callable returning an associative array |
||
| 295 | */ |
||
| 296 | 1 | public function addDataTableCallback($label, /* callable */ $callback) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Returns all the extra data tables registered with this handler. |
||
| 317 | * Optionally accepts a 'label' parameter, to only return the data |
||
| 318 | * table under that label. |
||
| 319 | * @param string|null $label |
||
| 320 | * @return array[]|callable |
||
| 321 | */ |
||
| 322 | 2 | public function getDataTables($label = null) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Allows to disable all attempts to dynamically decide whether to |
||
| 334 | * handle or return prematurely. |
||
| 335 | * Set this to ensure that the handler will perform no matter what. |
||
| 336 | * @param bool|null $value |
||
| 337 | * @return bool|null |
||
| 338 | */ |
||
| 339 | 1 | public function handleUnconditionally($value = null) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Adds an editor resolver, identified by a string |
||
| 350 | * name, and that may be a string path, or a callable |
||
| 351 | * resolver. If the callable returns a string, it will |
||
| 352 | * be set as the file reference's href attribute. |
||
| 353 | * |
||
| 354 | * @example |
||
| 355 | * $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") |
||
| 356 | * @example |
||
| 357 | * $run->addEditor('remove-it', function($file, $line) { |
||
| 358 | * unlink($file); |
||
| 359 | * return "http://stackoverflow.com"; |
||
| 360 | * }); |
||
| 361 | * @param string $identifier |
||
| 362 | * @param string $resolver |
||
| 363 | */ |
||
| 364 | 1 | public function addEditor($identifier, $resolver) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Set the editor to use to open referenced files, by a string |
||
| 371 | * identifier, or a callable that will be executed for every |
||
| 372 | * file reference, with a $file and $line argument, and should |
||
| 373 | * return a string. |
||
| 374 | * |
||
| 375 | * @example |
||
| 376 | * $run->setEditor(function($file, $line) { return "file:///{$file}"; }); |
||
| 377 | * @example |
||
| 378 | * $run->setEditor('sublime'); |
||
| 379 | * |
||
| 380 | * @throws InvalidArgumentException If invalid argument identifier provided |
||
| 381 | * @param string|callable $editor |
||
| 382 | */ |
||
| 383 | 4 | public function setEditor($editor) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Given a string file path, and an integer file line, |
||
| 397 | * executes the editor resolver and returns, if available, |
||
| 398 | * a string that may be used as the href property for that |
||
| 399 | * file reference. |
||
| 400 | * |
||
| 401 | * @throws InvalidArgumentException If editor resolver does not return a string |
||
| 402 | * @param string $filePath |
||
| 403 | * @param int $line |
||
| 404 | * @return string|bool |
||
| 405 | */ |
||
| 406 | 4 | public function getEditorHref($filePath, $line) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Given a boolean if the editor link should |
||
| 430 | * act as an Ajax request. The editor must be a |
||
| 431 | * valid callable function/closure |
||
| 432 | * |
||
| 433 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
| 434 | * @param string $filePath |
||
| 435 | * @param int $line |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | 1 | public function getEditorAjax($filePath, $line) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Given a boolean if the editor link should |
||
| 453 | * act as an Ajax request. The editor must be a |
||
| 454 | * valid callable function/closure |
||
| 455 | * |
||
| 456 | * @param string $filePath |
||
| 457 | * @param int $line |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | 1 | protected function getEditor($filePath, $line) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * @param string $title |
||
| 498 | * @return void |
||
| 499 | */ |
||
| 500 | 1 | public function setPageTitle($title) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | 1 | public function getPageTitle() |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Adds a path to the list of paths to be searched for |
||
| 515 | * resources. |
||
| 516 | * |
||
| 517 | * @throws InvalidArgumentException If $path is not a valid directory |
||
| 518 | * |
||
| 519 | * @param string $path |
||
| 520 | * @return void |
||
| 521 | */ |
||
| 522 | 2 | public function addResourcePath($path) |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Adds a custom css file to be loaded. |
||
| 535 | * |
||
| 536 | * @param string $name |
||
| 537 | * @return void |
||
| 538 | */ |
||
| 539 | public function addCustomCss($name) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @return array |
||
| 546 | */ |
||
| 547 | 1 | public function getResourcePaths() |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Finds a resource, by its relative path, in all available search paths. |
||
| 554 | * The search is performed starting at the last search path, and all the |
||
| 555 | * way back to the first, enabling a cascading-type system of overrides |
||
| 556 | * for all resources. |
||
| 557 | * |
||
| 558 | * @throws RuntimeException If resource cannot be found in any of the available paths |
||
| 559 | * |
||
| 560 | * @param string $resource |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | protected function getResource($resource) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @deprecated |
||
| 592 | * |
||
| 593 | * @return string |
||
| 594 | */ |
||
| 595 | public function getResourcesPath() |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @deprecated |
||
| 605 | * |
||
| 606 | * @param string $resourcesPath |
||
| 607 | * @return void |
||
| 608 | */ |
||
| 609 | public function setResourcesPath($resourcesPath) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Return the application paths. |
||
| 616 | * |
||
| 617 | * @return array |
||
| 618 | */ |
||
| 619 | public function getApplicationPaths() |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Set the application paths. |
||
| 626 | * |
||
| 627 | * @param array $applicationPaths |
||
| 628 | */ |
||
| 629 | public function setApplicationPaths($applicationPaths) |
||
| 633 | |||
| 634 | /** |
||
| 635 | * blacklist a sensitive value within one of the superglobal arrays. |
||
| 636 | * |
||
| 637 | * @param $superGlobalName string the name of the superglobal array, e.g. '_GET' |
||
| 638 | * @param $key string the key within the superglobal |
||
| 639 | */ |
||
| 640 | 1 | public function blacklist($superGlobalName, $key) { |
|
| 643 | |||
| 644 | /** |
||
| 645 | * Checks all given values against the given list of blacklisted values. |
||
| 646 | * Blacklisted values will be replaced by a equal length string cointaining only '*' characters. |
||
| 647 | * |
||
| 648 | * @param array $values a array of values |
||
| 649 | * @param string[] $blacklisted |
||
| 650 | * @return array $values without sensitive data |
||
| 651 | */ |
||
| 652 | private function maskBlacklisted(array $values, array $blacklisted) { |
||
| 660 | } |
||
| 661 |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.