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() |
|
7 ignored issues
–
show
|
|||
99 | { |
||
100 | 1 | if (!$this->handleUnconditionally()) { |
|
101 | // Check conditions for outputting HTML: |
||
102 | // @todo: Make this more robust |
||
103 | 1 | if (php_sapi_name() === 'cli') { |
|
104 | // Help users who have been relying on an internal test value |
||
105 | // fix their code to the proper method |
||
106 | 1 | if (isset($_ENV['whoops-test'])) { |
|
107 | throw new \Exception( |
||
108 | 'Use handleUnconditionally instead of whoops-test' |
||
109 | .' environment variable' |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | 1 | return Handler::DONE; |
|
114 | } |
||
115 | } |
||
116 | |||
117 | // @todo: Make this more dynamic |
||
118 | $helper = new TemplateHelper(); |
||
119 | |||
120 | $templateFile = $this->getResource("views/layout.html.php"); |
||
121 | $cssFile = $this->getResource("css/whoops.base.css"); |
||
122 | $zeptoFile = $this->getResource("js/zepto.min.js"); |
||
123 | $clipboard = $this->getResource("js/clipboard.min.js"); |
||
124 | $jsFile = $this->getResource("js/whoops.base.js"); |
||
125 | |||
126 | if ($this->customCss) { |
||
127 | $customCssFile = $this->getResource($this->customCss); |
||
128 | } |
||
129 | |||
130 | $inspector = $this->getInspector(); |
||
131 | $frames = $inspector->getFrames(); |
||
132 | |||
133 | $code = $inspector->getException()->getCode(); |
||
134 | |||
135 | if ($inspector->getException() instanceof \ErrorException) { |
||
136 | // ErrorExceptions wrap the php-error types within the "severity" property |
||
137 | $code = Misc::translateErrorCode($inspector->getException()->getSeverity()); |
||
138 | } |
||
139 | |||
140 | // List of variables that will be passed to the layout template. |
||
141 | $vars = array( |
||
142 | "page_title" => $this->getPageTitle(), |
||
143 | |||
144 | // @todo: Asset compiler |
||
145 | "stylesheet" => file_get_contents($cssFile), |
||
146 | "zepto" => file_get_contents($zeptoFile), |
||
147 | "clipboard" => file_get_contents($clipboard), |
||
148 | "javascript" => file_get_contents($jsFile), |
||
149 | |||
150 | // Template paths: |
||
151 | "header" => $this->getResource("views/header.html.php"), |
||
152 | "frame_list" => $this->getResource("views/frame_list.html.php"), |
||
153 | "frame_code" => $this->getResource("views/frame_code.html.php"), |
||
154 | "env_details" => $this->getResource("views/env_details.html.php"), |
||
155 | |||
156 | "title" => $this->getPageTitle(), |
||
157 | "name" => explode("\\", $inspector->getExceptionName()), |
||
158 | "message" => $inspector->getException()->getMessage(), |
||
159 | "code" => $code, |
||
160 | "plain_exception" => Formatter::formatExceptionPlain($inspector), |
||
161 | "frames" => $frames, |
||
162 | "has_frames" => !!count($frames), |
||
163 | "handler" => $this, |
||
164 | "handlers" => $this->getRun()->getHandlers(), |
||
165 | |||
166 | "tables" => array( |
||
167 | "GET Data" => $_GET, |
||
168 | "POST Data" => $_POST, |
||
169 | "Files" => $_FILES, |
||
170 | "Cookies" => $_COOKIE, |
||
171 | "Session" => isset($_SESSION) ? $_SESSION : array(), |
||
172 | "Server/Request Data" => $_SERVER, |
||
173 | "Environment Variables" => $_ENV, |
||
174 | ), |
||
175 | ); |
||
176 | |||
177 | if (isset($customCssFile)) { |
||
178 | $vars["stylesheet"] .= file_get_contents($customCssFile); |
||
179 | } |
||
180 | |||
181 | // Add extra entries list of data tables: |
||
182 | // @todo: Consolidate addDataTable and addDataTableCallback |
||
183 | $extraTables = array_map(function ($table) { |
||
184 | return $table instanceof \Closure ? $table() : $table; |
||
185 | }, $this->getDataTables()); |
||
186 | $vars["tables"] = array_merge($extraTables, $vars["tables"]); |
||
187 | |||
188 | if (\Whoops\Util\Misc::canSendHeaders()) { |
||
189 | header('Content-Type: text/html'); |
||
190 | } |
||
191 | |||
192 | $helper->setVariables($vars); |
||
193 | $helper->render($templateFile); |
||
194 | |||
195 | return Handler::QUIT; |
||
196 | } |
||
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 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: