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 string |
||
| 64 | */ |
||
| 65 | private $applicationRootPath; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * A string identifier for a known IDE/text editor, or a closure |
||
| 69 | * that resolves a string that can be used to open a given file |
||
| 70 | * in an editor. If the string contains the special substrings |
||
| 71 | * %file or %line, they will be replaced with the correct data. |
||
| 72 | * |
||
| 73 | * @example |
||
| 74 | * "txmt://open?url=%file&line=%line" |
||
| 75 | * @var mixed $editor |
||
| 76 | */ |
||
| 77 | protected $editor; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * A list of known editor strings |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $editors = [ |
||
| 84 | "sublime" => "subl://open?url=file://%file&line=%line", |
||
| 85 | "textmate" => "txmt://open?url=file://%file&line=%line", |
||
| 86 | "emacs" => "emacs://open?url=file://%file&line=%line", |
||
| 87 | "macvim" => "mvim://open/?url=file://%file&line=%line", |
||
| 88 | "phpstorm" => "phpstorm://open?file=%file&line=%line", |
||
| 89 | "idea" => "idea://open?file=%file&line=%line", |
||
| 90 | ]; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Constructor. |
||
| 94 | */ |
||
| 95 | 1 | public function __construct() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @return int|null |
||
| 110 | */ |
||
| 111 | 1 | public function handle() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function contentType() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Adds an entry to the list of tables displayed in the template. |
||
| 270 | * The expected data is a simple associative array. Any nested arrays |
||
| 271 | * will be flattened with print_r |
||
| 272 | * @param string $label |
||
| 273 | * @param array $data |
||
| 274 | */ |
||
| 275 | 1 | public function addDataTable($label, array $data) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Lazily adds an entry to the list of tables displayed in the table. |
||
| 282 | * The supplied callback argument will be called when the error is rendered, |
||
| 283 | * it should produce a simple associative array. Any nested arrays will |
||
| 284 | * be flattened with print_r. |
||
| 285 | * |
||
| 286 | * @throws InvalidArgumentException If $callback is not callable |
||
| 287 | * @param string $label |
||
| 288 | * @param callable $callback Callable returning an associative array |
||
| 289 | */ |
||
| 290 | 1 | public function addDataTableCallback($label, /* callable */ $callback) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Returns all the extra data tables registered with this handler. |
||
| 311 | * Optionally accepts a 'label' parameter, to only return the data |
||
| 312 | * table under that label. |
||
| 313 | * @param string|null $label |
||
| 314 | * @return array[]|callable |
||
| 315 | */ |
||
| 316 | 2 | public function getDataTables($label = null) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Allows to disable all attempts to dynamically decide whether to |
||
| 328 | * handle or return prematurely. |
||
| 329 | * Set this to ensure that the handler will perform no matter what. |
||
| 330 | * @param bool|null $value |
||
| 331 | * @return bool|null |
||
| 332 | */ |
||
| 333 | 1 | public function handleUnconditionally($value = null) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Adds an editor resolver, identified by a string |
||
| 344 | * name, and that may be a string path, or a callable |
||
| 345 | * resolver. If the callable returns a string, it will |
||
| 346 | * be set as the file reference's href attribute. |
||
| 347 | * |
||
| 348 | * @example |
||
| 349 | * $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") |
||
| 350 | * @example |
||
| 351 | * $run->addEditor('remove-it', function($file, $line) { |
||
| 352 | * unlink($file); |
||
| 353 | * return "http://stackoverflow.com"; |
||
| 354 | * }); |
||
| 355 | * @param string $identifier |
||
| 356 | * @param string $resolver |
||
| 357 | */ |
||
| 358 | 1 | public function addEditor($identifier, $resolver) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Set the editor to use to open referenced files, by a string |
||
| 365 | * identifier, or a callable that will be executed for every |
||
| 366 | * file reference, with a $file and $line argument, and should |
||
| 367 | * return a string. |
||
| 368 | * |
||
| 369 | * @example |
||
| 370 | * $run->setEditor(function($file, $line) { return "file:///{$file}"; }); |
||
| 371 | * @example |
||
| 372 | * $run->setEditor('sublime'); |
||
| 373 | * |
||
| 374 | * @throws InvalidArgumentException If invalid argument identifier provided |
||
| 375 | * @param string|callable $editor |
||
| 376 | */ |
||
| 377 | 4 | public function setEditor($editor) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Given a string file path, and an integer file line, |
||
| 391 | * executes the editor resolver and returns, if available, |
||
| 392 | * a string that may be used as the href property for that |
||
| 393 | * file reference. |
||
| 394 | * |
||
| 395 | * @throws InvalidArgumentException If editor resolver does not return a string |
||
| 396 | * @param string $filePath |
||
| 397 | * @param int $line |
||
| 398 | * @return string|bool |
||
| 399 | */ |
||
| 400 | 4 | public function getEditorHref($filePath, $line) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Given a boolean if the editor link should |
||
| 424 | * act as an Ajax request. The editor must be a |
||
| 425 | * valid callable function/closure |
||
| 426 | * |
||
| 427 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
| 428 | * @param string $filePath |
||
| 429 | * @param int $line |
||
| 430 | * @return bool |
||
| 431 | */ |
||
| 432 | 1 | public function getEditorAjax($filePath, $line) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Given a boolean if the editor link should |
||
| 447 | * act as an Ajax request. The editor must be a |
||
| 448 | * valid callable function/closure |
||
| 449 | * |
||
| 450 | * @param string $filePath |
||
| 451 | * @param int $line |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | 1 | protected function getEditor($filePath, $line) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * @param string $title |
||
| 492 | * @return void |
||
| 493 | */ |
||
| 494 | 1 | public function setPageTitle($title) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * @return string |
||
| 501 | */ |
||
| 502 | 1 | public function getPageTitle() |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Adds a path to the list of paths to be searched for |
||
| 509 | * resources. |
||
| 510 | * |
||
| 511 | * @throws InvalidArgumentException If $path is not a valid directory |
||
| 512 | * |
||
| 513 | * @param string $path |
||
| 514 | * @return void |
||
| 515 | */ |
||
| 516 | 2 | public function addResourcePath($path) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Adds a custom css file to be loaded. |
||
| 529 | * |
||
| 530 | * @param string $name |
||
| 531 | * @return void |
||
| 532 | */ |
||
| 533 | public function addCustomCss($name) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @return array |
||
| 540 | */ |
||
| 541 | 1 | public function getResourcePaths() |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Finds a resource, by its relative path, in all available search paths. |
||
| 548 | * The search is performed starting at the last search path, and all the |
||
| 549 | * way back to the first, enabling a cascading-type system of overrides |
||
| 550 | * for all resources. |
||
| 551 | * |
||
| 552 | * @throws RuntimeException If resource cannot be found in any of the available paths |
||
| 553 | * |
||
| 554 | * @param string $resource |
||
| 555 | * @return string |
||
| 556 | */ |
||
| 557 | protected function getResource($resource) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @deprecated |
||
| 586 | * |
||
| 587 | * @return string |
||
| 588 | */ |
||
| 589 | public function getResourcesPath() |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @deprecated |
||
| 599 | * |
||
| 600 | * @param string $resourcesPath |
||
| 601 | * @return void |
||
| 602 | */ |
||
| 603 | public function setResourcesPath($resourcesPath) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Return the application paths. |
||
| 610 | * |
||
| 611 | * @return array |
||
| 612 | */ |
||
| 613 | public function getApplicationPaths() |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Set the application paths. |
||
| 620 | * |
||
| 621 | * @param array $applicationPaths |
||
| 622 | */ |
||
| 623 | public function setApplicationPaths($applicationPaths) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Return the application root path. |
||
| 630 | * |
||
| 631 | * @return string |
||
| 632 | */ |
||
| 633 | public function getApplicationRootPath() |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Set the application root path. |
||
| 640 | * |
||
| 641 | * @param string $applicationRootPath |
||
| 642 | */ |
||
| 643 | public function setApplicationRootPath($applicationRootPath) |
||
| 647 | } |
||
| 648 |
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.