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 | * A string identifier for a known IDE/text editor, or a closure |
||
64 | * that resolves a string that can be used to open a given file |
||
65 | * in an editor. If the string contains the special substrings |
||
66 | * %file or %line, they will be replaced with the correct data. |
||
67 | * |
||
68 | * @example |
||
69 | * "txmt://open?url=%file&line=%line" |
||
70 | * @var mixed $editor |
||
71 | */ |
||
72 | protected $editor; |
||
73 | |||
74 | /** |
||
75 | * A list of known editor strings |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $editors = [ |
||
79 | "sublime" => "subl://open?url=file://%file&line=%line", |
||
80 | "textmate" => "txmt://open?url=file://%file&line=%line", |
||
81 | "emacs" => "emacs://open?url=file://%file&line=%line", |
||
82 | "macvim" => "mvim://open/?url=file://%file&line=%line", |
||
83 | "phpstorm" => "phpstorm://open?file=%file&line=%line", |
||
84 | "idea" => "idea://open?file=%file&line=%line", |
||
85 | ]; |
||
86 | |||
87 | /** |
||
88 | * Constructor. |
||
89 | */ |
||
90 | 1 | public function __construct() |
|
102 | |||
103 | /** |
||
104 | * @return int|null |
||
105 | */ |
||
106 | 1 | public function handle() |
|
248 | |||
249 | /** |
||
250 | * @return string |
||
251 | */ |
||
252 | public function contentType() |
||
256 | |||
257 | /** |
||
258 | * Adds an entry to the list of tables displayed in the template. |
||
259 | * The expected data is a simple associative array. Any nested arrays |
||
260 | * will be flattened with print_r |
||
261 | * @param string $label |
||
262 | * @param array $data |
||
263 | */ |
||
264 | 1 | public function addDataTable($label, array $data) |
|
268 | |||
269 | /** |
||
270 | * Lazily adds an entry to the list of tables displayed in the table. |
||
271 | * The supplied callback argument will be called when the error is rendered, |
||
272 | * it should produce a simple associative array. Any nested arrays will |
||
273 | * be flattened with print_r. |
||
274 | * |
||
275 | * @throws InvalidArgumentException If $callback is not callable |
||
276 | * @param string $label |
||
277 | * @param callable $callback Callable returning an associative array |
||
278 | */ |
||
279 | 1 | public function addDataTableCallback($label, /* callable */ $callback) |
|
297 | |||
298 | /** |
||
299 | * Returns all the extra data tables registered with this handler. |
||
300 | * Optionally accepts a 'label' parameter, to only return the data |
||
301 | * table under that label. |
||
302 | * @param string|null $label |
||
303 | * @return array[]|callable |
||
304 | */ |
||
305 | 2 | public function getDataTables($label = null) |
|
314 | |||
315 | /** |
||
316 | * Allows to disable all attempts to dynamically decide whether to |
||
317 | * handle or return prematurely. |
||
318 | * Set this to ensure that the handler will perform no matter what. |
||
319 | * @param bool|null $value |
||
320 | * @return bool|null |
||
321 | */ |
||
322 | 1 | public function handleUnconditionally($value = null) |
|
330 | |||
331 | /** |
||
332 | * Adds an editor resolver, identified by a string |
||
333 | * name, and that may be a string path, or a callable |
||
334 | * resolver. If the callable returns a string, it will |
||
335 | * be set as the file reference's href attribute. |
||
336 | * |
||
337 | * @example |
||
338 | * $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") |
||
339 | * @example |
||
340 | * $run->addEditor('remove-it', function($file, $line) { |
||
341 | * unlink($file); |
||
342 | * return "http://stackoverflow.com"; |
||
343 | * }); |
||
344 | * @param string $identifier |
||
345 | * @param string $resolver |
||
346 | */ |
||
347 | 1 | public function addEditor($identifier, $resolver) |
|
351 | |||
352 | /** |
||
353 | * Set the editor to use to open referenced files, by a string |
||
354 | * identifier, or a callable that will be executed for every |
||
355 | * file reference, with a $file and $line argument, and should |
||
356 | * return a string. |
||
357 | * |
||
358 | * @example |
||
359 | * $run->setEditor(function($file, $line) { return "file:///{$file}"; }); |
||
360 | * @example |
||
361 | * $run->setEditor('sublime'); |
||
362 | * |
||
363 | * @throws InvalidArgumentException If invalid argument identifier provided |
||
364 | * @param string|callable $editor |
||
365 | */ |
||
366 | 4 | public function setEditor($editor) |
|
377 | |||
378 | /** |
||
379 | * Given a string file path, and an integer file line, |
||
380 | * executes the editor resolver and returns, if available, |
||
381 | * a string that may be used as the href property for that |
||
382 | * file reference. |
||
383 | * |
||
384 | * @throws InvalidArgumentException If editor resolver does not return a string |
||
385 | * @param string $filePath |
||
386 | * @param int $line |
||
387 | * @return string|bool |
||
388 | */ |
||
389 | 4 | public function getEditorHref($filePath, $line) |
|
410 | |||
411 | /** |
||
412 | * Given a boolean if the editor link should |
||
413 | * act as an Ajax request. The editor must be a |
||
414 | * valid callable function/closure |
||
415 | * |
||
416 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
417 | * @param string $filePath |
||
418 | * @param int $line |
||
419 | * @return bool |
||
420 | */ |
||
421 | 1 | public function getEditorAjax($filePath, $line) |
|
433 | |||
434 | /** |
||
435 | * Given a boolean if the editor link should |
||
436 | * act as an Ajax request. The editor must be a |
||
437 | * valid callable function/closure |
||
438 | * |
||
439 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
440 | * @param string $filePath |
||
441 | * @param int $line |
||
442 | * @return mixed |
||
443 | */ |
||
444 | 1 | protected function getEditor($filePath, $line) |
|
476 | |||
477 | /** |
||
478 | * @param string $title |
||
479 | * @return void |
||
480 | */ |
||
481 | 1 | public function setPageTitle($title) |
|
485 | |||
486 | /** |
||
487 | * @return string |
||
488 | */ |
||
489 | 1 | public function getPageTitle() |
|
493 | |||
494 | /** |
||
495 | * Adds a path to the list of paths to be searched for |
||
496 | * resources. |
||
497 | * |
||
498 | * @throws InvalidArgumnetException If $path is not a valid directory |
||
499 | * |
||
500 | * @param string $path |
||
501 | * @return void |
||
502 | */ |
||
503 | 2 | public function addResourcePath($path) |
|
513 | |||
514 | /** |
||
515 | * Adds a custom css file to be loaded. |
||
516 | * |
||
517 | * @param string $name |
||
518 | * @return void |
||
519 | */ |
||
520 | public function addCustomCss($name) |
||
524 | |||
525 | /** |
||
526 | * @return array |
||
527 | */ |
||
528 | 1 | public function getResourcePaths() |
|
532 | |||
533 | /** |
||
534 | * Finds a resource, by its relative path, in all available search paths. |
||
535 | * The search is performed starting at the last search path, and all the |
||
536 | * way back to the first, enabling a cascading-type system of overrides |
||
537 | * for all resources. |
||
538 | * |
||
539 | * @throws RuntimeException If resource cannot be found in any of the available paths |
||
540 | * |
||
541 | * @param string $resource |
||
542 | * @return string |
||
543 | */ |
||
544 | protected function getResource($resource) |
||
570 | |||
571 | /** |
||
572 | * @deprecated |
||
573 | * |
||
574 | * @return string |
||
575 | */ |
||
576 | public function getResourcesPath() |
||
583 | |||
584 | /** |
||
585 | * @deprecated |
||
586 | * |
||
587 | * @param string $resourcesPath |
||
588 | * @return void |
||
589 | */ |
||
590 | public function setResourcesPath($resourcesPath) |
||
594 | |||
595 | /** |
||
596 | * Return the application paths. |
||
597 | * |
||
598 | * @return array |
||
599 | */ |
||
600 | public function getApplicationPaths() |
||
604 | |||
605 | /** |
||
606 | * Set the application paths. |
||
607 | * |
||
608 | * @param array $applicationPaths |
||
609 | */ |
||
610 | public function setApplicationPaths($applicationPaths) |
||
614 | } |
||
615 |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.