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 |
||
| 16 | class PrettyPageHandler extends Handler |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Search paths to be scanned for resources, in the reverse |
||
| 20 | * order they're declared. |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | private $searchPaths = array(); |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Fast lookup cache for known resource locations. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $resourceCache = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The name of the custom css file. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $customCss = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array[] |
||
| 42 | */ |
||
| 43 | private $extraTables = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | private $handleUnconditionally = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $pageTitle = "Whoops! There was an error."; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * A string identifier for a known IDE/text editor, or a closure |
||
| 57 | * that resolves a string that can be used to open a given file |
||
| 58 | * in an editor. If the string contains the special substrings |
||
| 59 | * %file or %line, they will be replaced with the correct data. |
||
| 60 | * |
||
| 61 | * @example |
||
| 62 | * "txmt://open?url=%file&line=%line" |
||
| 63 | * @var mixed $editor |
||
| 64 | */ |
||
| 65 | protected $editor; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * A list of known editor strings |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $editors = array( |
||
| 72 | "sublime" => "subl://open?url=file://%file&line=%line", |
||
| 73 | "textmate" => "txmt://open?url=file://%file&line=%line", |
||
| 74 | "emacs" => "emacs://open?url=file://%file&line=%line", |
||
| 75 | "macvim" => "mvim://open/?url=file://%file&line=%line", |
||
| 76 | "phpstorm" => "phpstorm://open?file=%file&line=%line", |
||
| 77 | ); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Constructor. |
||
| 81 | */ |
||
| 82 | 1 | public function __construct() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @return int|null |
||
| 97 | */ |
||
| 98 | 1 | public function handle() |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Adds an entry to the list of tables displayed in the template. |
||
| 200 | * The expected data is a simple associative array. Any nested arrays |
||
| 201 | * will be flattened with print_r |
||
| 202 | * @param string $label |
||
| 203 | * @param array $data |
||
| 204 | */ |
||
| 205 | 1 | public function addDataTable($label, array $data) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Lazily adds an entry to the list of tables displayed in the table. |
||
| 212 | * The supplied callback argument will be called when the error is rendered, |
||
| 213 | * it should produce a simple associative array. Any nested arrays will |
||
| 214 | * be flattened with print_r. |
||
| 215 | * |
||
| 216 | * @throws InvalidArgumentException If $callback is not callable |
||
| 217 | * @param string $label |
||
| 218 | * @param callable $callback Callable returning an associative array |
||
| 219 | */ |
||
| 220 | 1 | public function addDataTableCallback($label, /* callable */ $callback) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Returns all the extra data tables registered with this handler. |
||
| 241 | * Optionally accepts a 'label' parameter, to only return the data |
||
| 242 | * table under that label. |
||
| 243 | * @param string|null $label |
||
| 244 | * @return array[]|callable |
||
| 245 | */ |
||
| 246 | 2 | public function getDataTables($label = null) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Allows to disable all attempts to dynamically decide whether to |
||
| 258 | * handle or return prematurely. |
||
| 259 | * Set this to ensure that the handler will perform no matter what. |
||
| 260 | * @param bool|null $value |
||
| 261 | * @return bool|null |
||
| 262 | */ |
||
| 263 | 1 | public function handleUnconditionally($value = null) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Adds an editor resolver, identified by a string |
||
| 274 | * name, and that may be a string path, or a callable |
||
| 275 | * resolver. If the callable returns a string, it will |
||
| 276 | * be set as the file reference's href attribute. |
||
| 277 | * |
||
| 278 | * @example |
||
| 279 | * $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") |
||
| 280 | * @example |
||
| 281 | * $run->addEditor('remove-it', function($file, $line) { |
||
| 282 | * unlink($file); |
||
| 283 | * return "http://stackoverflow.com"; |
||
| 284 | * }); |
||
| 285 | * @param string $identifier |
||
| 286 | * @param string $resolver |
||
| 287 | */ |
||
| 288 | 1 | public function addEditor($identifier, $resolver) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Set the editor to use to open referenced files, by a string |
||
| 295 | * identifier, or a callable that will be executed for every |
||
| 296 | * file reference, with a $file and $line argument, and should |
||
| 297 | * return a string. |
||
| 298 | * |
||
| 299 | * @example |
||
| 300 | * $run->setEditor(function($file, $line) { return "file:///{$file}"; }); |
||
| 301 | * @example |
||
| 302 | * $run->setEditor('sublime'); |
||
| 303 | * |
||
| 304 | * @throws InvalidArgumentException If invalid argument identifier provided |
||
| 305 | * @param string|callable $editor |
||
| 306 | */ |
||
| 307 | 4 | public function setEditor($editor) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Given a string file path, and an integer file line, |
||
| 321 | * executes the editor resolver and returns, if available, |
||
| 322 | * a string that may be used as the href property for that |
||
| 323 | * file reference. |
||
| 324 | * |
||
| 325 | * @throws InvalidArgumentException If editor resolver does not return a string |
||
| 326 | * @param string $filePath |
||
| 327 | * @param int $line |
||
| 328 | * @return string|bool |
||
| 329 | */ |
||
| 330 | 4 | public function getEditorHref($filePath, $line) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Given a boolean if the editor link should |
||
| 354 | * act as an Ajax request. The editor must be a |
||
| 355 | * valid callable function/closure |
||
| 356 | * |
||
| 357 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
| 358 | * @param string $filePath |
||
| 359 | * @param int $line |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | 1 | public function getEditorAjax($filePath, $line) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Given a boolean if the editor link should |
||
| 377 | * act as an Ajax request. The editor must be a |
||
| 378 | * valid callable function/closure |
||
| 379 | * |
||
| 380 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
| 381 | * @param string $filePath |
||
| 382 | * @param int $line |
||
| 383 | * @return mixed |
||
| 384 | */ |
||
| 385 | 1 | protected function getEditor($filePath, $line) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * @param string $title |
||
| 420 | * @return void |
||
| 421 | */ |
||
| 422 | 1 | public function setPageTitle($title) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | 1 | public function getPageTitle() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Adds a path to the list of paths to be searched for |
||
| 437 | * resources. |
||
| 438 | * |
||
| 439 | * @throws InvalidArgumnetException If $path is not a valid directory |
||
| 440 | * |
||
| 441 | * @param string $path |
||
| 442 | * @return void |
||
| 443 | */ |
||
| 444 | 2 | public function addResourcePath($path) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Adds a custom css file to be loaded. |
||
| 457 | * |
||
| 458 | * @param string $name |
||
| 459 | * @return void |
||
| 460 | */ |
||
| 461 | public function addCustomCss($name) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @return array |
||
| 468 | */ |
||
| 469 | 1 | public function getResourcePaths() |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Finds a resource, by its relative path, in all available search paths. |
||
| 476 | * The search is performed starting at the last search path, and all the |
||
| 477 | * way back to the first, enabling a cascading-type system of overrides |
||
| 478 | * for all resources. |
||
| 479 | * |
||
| 480 | * @throws RuntimeException If resource cannot be found in any of the available paths |
||
| 481 | * |
||
| 482 | * @param string $resource |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | protected function getResource($resource) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @deprecated |
||
| 514 | * |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | public function getResourcesPath() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @deprecated |
||
| 527 | * |
||
| 528 | * @param string $resourcesPath |
||
| 529 | * @return void |
||
| 530 | */ |
||
| 531 | public function setResourcesPath($resourcesPath) |
||
| 535 | } |
||
| 536 |
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.