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() |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @return int|null |
||
| 113 | */ |
||
| 114 | 1 | public function handle() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function contentType() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Adds an entry to the list of tables displayed in the template. |
||
| 269 | * The expected data is a simple associative array. Any nested arrays |
||
| 270 | * will be flattened with print_r |
||
| 271 | * @param string $label |
||
| 272 | * @param array $data |
||
| 273 | */ |
||
| 274 | 1 | public function addDataTable($label, array $data) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Lazily adds an entry to the list of tables displayed in the table. |
||
| 281 | * The supplied callback argument will be called when the error is rendered, |
||
| 282 | * it should produce a simple associative array. Any nested arrays will |
||
| 283 | * be flattened with print_r. |
||
| 284 | * |
||
| 285 | * @throws InvalidArgumentException If $callback is not callable |
||
| 286 | * @param string $label |
||
| 287 | * @param callable $callback Callable returning an associative array |
||
| 288 | */ |
||
| 289 | 1 | public function addDataTableCallback($label, /* callable */ $callback) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Returns all the extra data tables registered with this handler. |
||
| 310 | * Optionally accepts a 'label' parameter, to only return the data |
||
| 311 | * table under that label. |
||
| 312 | * @param string|null $label |
||
| 313 | * @return array[]|callable |
||
| 314 | */ |
||
| 315 | 2 | public function getDataTables($label = null) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Allows to disable all attempts to dynamically decide whether to |
||
| 327 | * handle or return prematurely. |
||
| 328 | * Set this to ensure that the handler will perform no matter what. |
||
| 329 | * @param bool|null $value |
||
| 330 | * @return bool|null |
||
| 331 | */ |
||
| 332 | 1 | public function handleUnconditionally($value = null) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Adds an editor resolver, identified by a string |
||
| 343 | * name, and that may be a string path, or a callable |
||
| 344 | * resolver. If the callable returns a string, it will |
||
| 345 | * be set as the file reference's href attribute. |
||
| 346 | * |
||
| 347 | * @example |
||
| 348 | * $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") |
||
| 349 | * @example |
||
| 350 | * $run->addEditor('remove-it', function($file, $line) { |
||
| 351 | * unlink($file); |
||
| 352 | * return "http://stackoverflow.com"; |
||
| 353 | * }); |
||
| 354 | * @param string $identifier |
||
| 355 | * @param string $resolver |
||
| 356 | */ |
||
| 357 | 1 | public function addEditor($identifier, $resolver) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Set the editor to use to open referenced files, by a string |
||
| 364 | * identifier, or a callable that will be executed for every |
||
| 365 | * file reference, with a $file and $line argument, and should |
||
| 366 | * return a string. |
||
| 367 | * |
||
| 368 | * @example |
||
| 369 | * $run->setEditor(function($file, $line) { return "file:///{$file}"; }); |
||
| 370 | * @example |
||
| 371 | * $run->setEditor('sublime'); |
||
| 372 | * |
||
| 373 | * @throws InvalidArgumentException If invalid argument identifier provided |
||
| 374 | * @param string|callable $editor |
||
| 375 | */ |
||
| 376 | 4 | public function setEditor($editor) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Given a string file path, and an integer file line, |
||
| 390 | * executes the editor resolver and returns, if available, |
||
| 391 | * a string that may be used as the href property for that |
||
| 392 | * file reference. |
||
| 393 | * |
||
| 394 | * @throws InvalidArgumentException If editor resolver does not return a string |
||
| 395 | * @param string $filePath |
||
| 396 | * @param int $line |
||
| 397 | * @return string|bool |
||
| 398 | */ |
||
| 399 | 4 | public function getEditorHref($filePath, $line) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Given a boolean if the editor link should |
||
| 423 | * act as an Ajax request. The editor must be a |
||
| 424 | * valid callable function/closure |
||
| 425 | * |
||
| 426 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
| 427 | * @param string $filePath |
||
| 428 | * @param int $line |
||
| 429 | * @return bool |
||
| 430 | */ |
||
| 431 | 1 | public function getEditorAjax($filePath, $line) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Given a boolean if the editor link should |
||
| 446 | * act as an Ajax request. The editor must be a |
||
| 447 | * valid callable function/closure |
||
| 448 | * |
||
| 449 | * @param string $filePath |
||
| 450 | * @param int $line |
||
| 451 | * @return array |
||
| 452 | */ |
||
| 453 | 1 | protected function getEditor($filePath, $line) |
|
| 488 | |||
| 489 | /** |
||
| 490 | * @param string $title |
||
| 491 | * @return void |
||
| 492 | */ |
||
| 493 | 1 | public function setPageTitle($title) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | 1 | public function getPageTitle() |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Adds a path to the list of paths to be searched for |
||
| 508 | * resources. |
||
| 509 | * |
||
| 510 | * @throws InvalidArgumentException If $path is not a valid directory |
||
| 511 | * |
||
| 512 | * @param string $path |
||
| 513 | * @return void |
||
| 514 | */ |
||
| 515 | 2 | public function addResourcePath($path) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Adds a custom css file to be loaded. |
||
| 528 | * |
||
| 529 | * @param string $name |
||
| 530 | * @return void |
||
| 531 | */ |
||
| 532 | public function addCustomCss($name) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @return array |
||
| 539 | */ |
||
| 540 | 1 | public function getResourcePaths() |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Finds a resource, by its relative path, in all available search paths. |
||
| 547 | * The search is performed starting at the last search path, and all the |
||
| 548 | * way back to the first, enabling a cascading-type system of overrides |
||
| 549 | * for all resources. |
||
| 550 | * |
||
| 551 | * @throws RuntimeException If resource cannot be found in any of the available paths |
||
| 552 | * |
||
| 553 | * @param string $resource |
||
| 554 | * @return string |
||
| 555 | */ |
||
| 556 | protected function getResource($resource) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @deprecated |
||
| 585 | * |
||
| 586 | * @return string |
||
| 587 | */ |
||
| 588 | public function getResourcesPath() |
||
| 595 | |||
| 596 | /** |
||
| 597 | * @deprecated |
||
| 598 | * |
||
| 599 | * @param string $resourcesPath |
||
| 600 | * @return void |
||
| 601 | */ |
||
| 602 | public function setResourcesPath($resourcesPath) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Return the application paths. |
||
| 609 | * |
||
| 610 | * @return array |
||
| 611 | */ |
||
| 612 | public function getApplicationPaths() |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Set the application paths. |
||
| 619 | * |
||
| 620 | * @param array $applicationPaths |
||
| 621 | */ |
||
| 622 | public function setApplicationPaths($applicationPaths) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Return the application root path. |
||
| 629 | * |
||
| 630 | * @return string |
||
| 631 | */ |
||
| 632 | public function getApplicationRootPath() |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Set the application root path. |
||
| 639 | * |
||
| 640 | * @param string $applicationRootPath |
||
| 641 | */ |
||
| 642 | public function setApplicationRootPath($applicationRootPath) |
||
| 646 | } |
||
| 647 |
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.