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 | * A string identifier for a known IDE/text editor, or a closure |
||
| 59 | * that resolves a string that can be used to open a given file |
||
| 60 | * in an editor. If the string contains the special substrings |
||
| 61 | * %file or %line, they will be replaced with the correct data. |
||
| 62 | * |
||
| 63 | * @example |
||
| 64 | * "txmt://open?url=%file&line=%line" |
||
| 65 | * @var mixed $editor |
||
| 66 | */ |
||
| 67 | protected $editor; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * A list of known editor strings |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $editors = [ |
||
| 74 | "sublime" => "subl://open?url=file://%file&line=%line", |
||
| 75 | "textmate" => "txmt://open?url=file://%file&line=%line", |
||
| 76 | "emacs" => "emacs://open?url=file://%file&line=%line", |
||
| 77 | "macvim" => "mvim://open/?url=file://%file&line=%line", |
||
| 78 | "phpstorm" => "phpstorm://open?file=%file&line=%line", |
||
| 79 | ]; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Constructor. |
||
| 83 | */ |
||
| 84 | 1 | public function __construct() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @return int|null |
||
| 99 | */ |
||
| 100 | 1 | public function handle() |
|
| 101 | { |
||
| 102 | 1 | if (!$this->handleUnconditionally()) { |
|
|
|
|||
| 103 | // Check conditions for outputting HTML: |
||
| 104 | // @todo: Make this more robust |
||
| 105 | 1 | if (php_sapi_name() === 'cli') { |
|
| 106 | // Help users who have been relying on an internal test value |
||
| 107 | // fix their code to the proper method |
||
| 108 | 1 | if (isset($_ENV['whoops-test'])) { |
|
| 109 | throw new \Exception( |
||
| 110 | 'Use handleUnconditionally instead of whoops-test' |
||
| 111 | .' environment variable' |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | 1 | return Handler::DONE; |
|
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | // @todo: Make this more dynamic |
||
| 120 | $helper = new TemplateHelper(); |
||
| 121 | |||
| 122 | if (class_exists('Symfony\Component\VarDumper\Cloner\VarCloner')) { |
||
| 123 | $cloner = new VarCloner(); |
||
| 124 | // Only dump object internals if a custom caster exists. |
||
| 125 | $cloner->addCasters(['*' => function ($obj, $a, $stub, $isNested, $filter = 0) { |
||
| 126 | $class = $stub->class; |
||
| 127 | $classes = [$class => $class] + class_parents($class) + class_implements($class); |
||
| 128 | |||
| 129 | foreach ($classes as $class) { |
||
| 130 | if (isset(AbstractCloner::$defaultCasters[$class])) { |
||
| 131 | return $a; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | // Remove all internals |
||
| 136 | return []; |
||
| 137 | }]); |
||
| 138 | $helper->setCloner($cloner); |
||
| 139 | } |
||
| 140 | |||
| 141 | $templateFile = $this->getResource("views/layout.html.php"); |
||
| 142 | $cssFile = $this->getResource("css/whoops.base.css"); |
||
| 143 | $zeptoFile = $this->getResource("js/zepto.min.js"); |
||
| 144 | $clipboard = $this->getResource("js/clipboard.min.js"); |
||
| 145 | $jsFile = $this->getResource("js/whoops.base.js"); |
||
| 146 | |||
| 147 | if ($this->customCss) { |
||
| 148 | $customCssFile = $this->getResource($this->customCss); |
||
| 149 | } |
||
| 150 | |||
| 151 | $inspector = $this->getInspector(); |
||
| 152 | $frames = $inspector->getFrames(); |
||
| 153 | |||
| 154 | $code = $inspector->getException()->getCode(); |
||
| 155 | |||
| 156 | if ($inspector->getException() instanceof \ErrorException) { |
||
| 157 | // ErrorExceptions wrap the php-error types within the "severity" property |
||
| 158 | $code = Misc::translateErrorCode($inspector->getException()->getSeverity()); |
||
| 159 | } |
||
| 160 | |||
| 161 | // List of variables that will be passed to the layout template. |
||
| 162 | $vars = [ |
||
| 163 | "page_title" => $this->getPageTitle(), |
||
| 164 | |||
| 165 | // @todo: Asset compiler |
||
| 166 | "stylesheet" => file_get_contents($cssFile), |
||
| 167 | "zepto" => file_get_contents($zeptoFile), |
||
| 168 | "clipboard" => file_get_contents($clipboard), |
||
| 169 | "javascript" => file_get_contents($jsFile), |
||
| 170 | |||
| 171 | // Template paths: |
||
| 172 | "header" => $this->getResource("views/header.html.php"), |
||
| 173 | "frame_list" => $this->getResource("views/frame_list.html.php"), |
||
| 174 | "frame_code" => $this->getResource("views/frame_code.html.php"), |
||
| 175 | "env_details" => $this->getResource("views/env_details.html.php"), |
||
| 176 | |||
| 177 | "title" => $this->getPageTitle(), |
||
| 178 | "name" => explode("\\", $inspector->getExceptionName()), |
||
| 179 | "message" => $inspector->getException()->getMessage(), |
||
| 180 | "code" => $code, |
||
| 181 | "plain_exception" => Formatter::formatExceptionPlain($inspector), |
||
| 182 | "frames" => $frames, |
||
| 183 | "has_frames" => !!count($frames), |
||
| 184 | "handler" => $this, |
||
| 185 | "handlers" => $this->getRun()->getHandlers(), |
||
| 186 | |||
| 187 | "tables" => [ |
||
| 188 | "GET Data" => $_GET, |
||
| 189 | "POST Data" => $_POST, |
||
| 190 | "Files" => $_FILES, |
||
| 191 | "Cookies" => $_COOKIE, |
||
| 192 | "Session" => isset($_SESSION) ? $_SESSION : [], |
||
| 193 | "Server/Request Data" => $_SERVER, |
||
| 194 | "Environment Variables" => $_ENV, |
||
| 195 | ], |
||
| 196 | ]; |
||
| 197 | |||
| 198 | if (isset($customCssFile)) { |
||
| 199 | $vars["stylesheet"] .= file_get_contents($customCssFile); |
||
| 200 | } |
||
| 201 | |||
| 202 | // Add extra entries list of data tables: |
||
| 203 | // @todo: Consolidate addDataTable and addDataTableCallback |
||
| 204 | $extraTables = array_map(function ($table) use ($inspector) { |
||
| 205 | return $table instanceof \Closure ? $table($inspector) : $table; |
||
| 206 | }, $this->getDataTables()); |
||
| 207 | $vars["tables"] = array_merge($extraTables, $vars["tables"]); |
||
| 208 | |||
| 209 | if (\Whoops\Util\Misc::canSendHeaders()) { |
||
| 210 | header('Content-Type: text/html'); |
||
| 211 | } |
||
| 212 | |||
| 213 | $plainTextHandler = new PlainTextHandler(); |
||
| 214 | $plainTextHandler->setException($this->getException()); |
||
| 215 | $plainTextHandler->setInspector($this->getInspector()); |
||
| 216 | $vars["preface"] = "<!--\n\n\n" . $plainTextHandler->generateResponse() . "\n\n\n\n\n\n\n\n\n\n\n-->"; |
||
| 217 | |||
| 218 | $helper->setVariables($vars); |
||
| 219 | $helper->render($templateFile); |
||
| 220 | |||
| 221 | return Handler::QUIT; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Adds an entry to the list of tables displayed in the template. |
||
| 226 | * The expected data is a simple associative array. Any nested arrays |
||
| 227 | * will be flattened with print_r |
||
| 228 | * @param string $label |
||
| 229 | * @param array $data |
||
| 230 | */ |
||
| 231 | 1 | public function addDataTable($label, array $data) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Lazily adds an entry to the list of tables displayed in the table. |
||
| 238 | * The supplied callback argument will be called when the error is rendered, |
||
| 239 | * it should produce a simple associative array. Any nested arrays will |
||
| 240 | * be flattened with print_r. |
||
| 241 | * |
||
| 242 | * @throws InvalidArgumentException If $callback is not callable |
||
| 243 | * @param string $label |
||
| 244 | * @param callable $callback Callable returning an associative array |
||
| 245 | */ |
||
| 246 | 1 | public function addDataTableCallback($label, /* callable */ $callback) |
|
| 247 | { |
||
| 248 | 1 | if (!is_callable($callback)) { |
|
| 249 | throw new InvalidArgumentException('Expecting callback argument to be callable'); |
||
| 250 | } |
||
| 251 | |||
| 252 | 1 | $this->extraTables[$label] = function (\Whoops\Exception\Inspector $inspector = null) use ($callback) { |
|
| 253 | try { |
||
| 254 | 1 | $result = call_user_func($callback, $inspector); |
|
| 255 | |||
| 256 | // Only return the result if it can be iterated over by foreach(). |
||
| 257 | 1 | return is_array($result) || $result instanceof \Traversable ? $result : []; |
|
| 258 | } catch (\Exception $e) { |
||
| 259 | // Don't allow failure to break the rendering of the original exception. |
||
| 260 | return []; |
||
| 261 | } |
||
| 262 | }; |
||
| 263 | 1 | } |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Returns all the extra data tables registered with this handler. |
||
| 267 | * Optionally accepts a 'label' parameter, to only return the data |
||
| 268 | * table under that label. |
||
| 269 | * @param string|null $label |
||
| 270 | * @return array[]|callable |
||
| 271 | */ |
||
| 272 | 2 | public function getDataTables($label = null) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Allows to disable all attempts to dynamically decide whether to |
||
| 284 | * handle or return prematurely. |
||
| 285 | * Set this to ensure that the handler will perform no matter what. |
||
| 286 | * @param bool|null $value |
||
| 287 | * @return bool|null |
||
| 288 | */ |
||
| 289 | 1 | public function handleUnconditionally($value = null) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Adds an editor resolver, identified by a string |
||
| 300 | * name, and that may be a string path, or a callable |
||
| 301 | * resolver. If the callable returns a string, it will |
||
| 302 | * be set as the file reference's href attribute. |
||
| 303 | * |
||
| 304 | * @example |
||
| 305 | * $run->addEditor('macvim', "mvim://open?url=file://%file&line=%line") |
||
| 306 | * @example |
||
| 307 | * $run->addEditor('remove-it', function($file, $line) { |
||
| 308 | * unlink($file); |
||
| 309 | * return "http://stackoverflow.com"; |
||
| 310 | * }); |
||
| 311 | * @param string $identifier |
||
| 312 | * @param string $resolver |
||
| 313 | */ |
||
| 314 | 1 | public function addEditor($identifier, $resolver) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Set the editor to use to open referenced files, by a string |
||
| 321 | * identifier, or a callable that will be executed for every |
||
| 322 | * file reference, with a $file and $line argument, and should |
||
| 323 | * return a string. |
||
| 324 | * |
||
| 325 | * @example |
||
| 326 | * $run->setEditor(function($file, $line) { return "file:///{$file}"; }); |
||
| 327 | * @example |
||
| 328 | * $run->setEditor('sublime'); |
||
| 329 | * |
||
| 330 | * @throws InvalidArgumentException If invalid argument identifier provided |
||
| 331 | * @param string|callable $editor |
||
| 332 | */ |
||
| 333 | 4 | public function setEditor($editor) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Given a string file path, and an integer file line, |
||
| 347 | * executes the editor resolver and returns, if available, |
||
| 348 | * a string that may be used as the href property for that |
||
| 349 | * file reference. |
||
| 350 | * |
||
| 351 | * @throws InvalidArgumentException If editor resolver does not return a string |
||
| 352 | * @param string $filePath |
||
| 353 | * @param int $line |
||
| 354 | * @return string|bool |
||
| 355 | */ |
||
| 356 | 4 | public function getEditorHref($filePath, $line) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Given a boolean if the editor link should |
||
| 380 | * act as an Ajax request. The editor must be a |
||
| 381 | * valid callable function/closure |
||
| 382 | * |
||
| 383 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
| 384 | * @param string $filePath |
||
| 385 | * @param int $line |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | 1 | public function getEditorAjax($filePath, $line) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Given a boolean if the editor link should |
||
| 403 | * act as an Ajax request. The editor must be a |
||
| 404 | * valid callable function/closure |
||
| 405 | * |
||
| 406 | * @throws UnexpectedValueException If editor resolver does not return a boolean |
||
| 407 | * @param string $filePath |
||
| 408 | * @param int $line |
||
| 409 | * @return mixed |
||
| 410 | */ |
||
| 411 | 1 | protected function getEditor($filePath, $line) |
|
| 412 | { |
||
| 413 | 1 | if ($this->editor === null && !is_string($this->editor) && !is_callable($this->editor)) |
|
| 414 | 1 | { |
|
| 415 | return false; |
||
| 416 | } |
||
| 417 | 1 | else if(is_string($this->editor) && isset($this->editors[$this->editor]) && !is_callable($this->editors[$this->editor])) |
|
| 418 | 1 | { |
|
| 419 | return [ |
||
| 420 | 'ajax' => false, |
||
| 421 | 'url' => $this->editors[$this->editor], |
||
| 422 | ]; |
||
| 423 | } |
||
| 424 | 1 | else if(is_callable($this->editor) || (isset($this->editors[$this->editor]) && is_callable($this->editors[$this->editor]))) |
|
| 425 | 1 | { |
|
| 426 | 1 | if(is_callable($this->editor)) |
|
| 427 | 1 | { |
|
| 428 | $callback = call_user_func($this->editor, $filePath, $line); |
||
| 429 | } |
||
| 430 | else |
||
| 431 | { |
||
| 432 | 1 | $callback = call_user_func($this->editors[$this->editor], $filePath, $line); |
|
| 433 | } |
||
| 434 | |||
| 435 | return [ |
||
| 436 | 1 | 'ajax' => isset($callback['ajax']) ? $callback['ajax'] : false, |
|
| 437 | 1 | 'url' => (is_array($callback) ? $callback['url'] : $callback), |
|
| 438 | 1 | ]; |
|
| 439 | } |
||
| 440 | |||
| 441 | return false; |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param string $title |
||
| 446 | * @return void |
||
| 447 | */ |
||
| 448 | 1 | public function setPageTitle($title) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | 1 | public function getPageTitle() |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Adds a path to the list of paths to be searched for |
||
| 463 | * resources. |
||
| 464 | * |
||
| 465 | * @throws InvalidArgumnetException If $path is not a valid directory |
||
| 466 | * |
||
| 467 | * @param string $path |
||
| 468 | * @return void |
||
| 469 | */ |
||
| 470 | 2 | public function addResourcePath($path) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Adds a custom css file to be loaded. |
||
| 483 | * |
||
| 484 | * @param string $name |
||
| 485 | * @return void |
||
| 486 | */ |
||
| 487 | public function addCustomCss($name) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @return array |
||
| 494 | */ |
||
| 495 | 1 | public function getResourcePaths() |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Finds a resource, by its relative path, in all available search paths. |
||
| 502 | * The search is performed starting at the last search path, and all the |
||
| 503 | * way back to the first, enabling a cascading-type system of overrides |
||
| 504 | * for all resources. |
||
| 505 | * |
||
| 506 | * @throws RuntimeException If resource cannot be found in any of the available paths |
||
| 507 | * |
||
| 508 | * @param string $resource |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | protected function getResource($resource) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @deprecated |
||
| 540 | * |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | public function getResourcesPath() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @deprecated |
||
| 553 | * |
||
| 554 | * @param string $resourcesPath |
||
| 555 | * @return void |
||
| 556 | */ |
||
| 557 | public function setResourcesPath($resourcesPath) |
||
| 561 | } |
||
| 562 |
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.