Complex classes like ErrorHandler 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 ErrorHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class ErrorHandler extends \yii\base\ErrorHandler |
||
32 | { |
||
33 | /** |
||
34 | * @var int maximum number of source code lines to be displayed. Defaults to 19. |
||
35 | */ |
||
36 | public $maxSourceLines = 19; |
||
37 | /** |
||
38 | * @var int maximum number of trace source code lines to be displayed. Defaults to 13. |
||
39 | */ |
||
40 | public $maxTraceSourceLines = 13; |
||
41 | /** |
||
42 | * @var string the route (e.g. `site/error`) to the controller action that will be used |
||
43 | * to display external errors. Inside the action, it can retrieve the error information |
||
44 | * using `Yii::$app->errorHandler->exception`. This property defaults to null, meaning ErrorHandler |
||
45 | * will handle the error display. |
||
46 | */ |
||
47 | public $errorAction; |
||
48 | /** |
||
49 | * @var string the path of the view file for rendering exceptions without call stack information. |
||
50 | */ |
||
51 | public $errorView = '@yii/views/errorHandler/error.php'; |
||
52 | /** |
||
53 | * @var string the path of the view file for rendering exceptions. |
||
54 | */ |
||
55 | public $exceptionView = '@yii/views/errorHandler/exception.php'; |
||
56 | /** |
||
57 | * @var string the path of the view file for rendering exceptions and errors call stack element. |
||
58 | */ |
||
59 | public $callStackItemView = '@yii/views/errorHandler/callStackItem.php'; |
||
60 | /** |
||
61 | * @var string the path of the view file for rendering previous exceptions. |
||
62 | */ |
||
63 | public $previousExceptionView = '@yii/views/errorHandler/previousException.php'; |
||
64 | /** |
||
65 | * @var array list of the PHP predefined variables that should be displayed on the error page. |
||
66 | * Note that a variable must be accessible via `$GLOBALS`. Otherwise it won't be displayed. |
||
67 | * Defaults to `['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION']`. |
||
68 | * @see renderRequest() |
||
69 | * @since 2.0.7 |
||
70 | */ |
||
71 | public $displayVars = ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION']; |
||
72 | |||
73 | |||
74 | /** |
||
75 | * Renders the exception. |
||
76 | * @param \Exception $exception the exception to be rendered. |
||
77 | */ |
||
78 | protected function renderException($exception) |
||
130 | |||
131 | /** |
||
132 | * Converts an exception into an array. |
||
133 | * @param \Exception $exception the exception being converted |
||
134 | * @return array the array representation of the exception. |
||
135 | */ |
||
136 | protected function convertExceptionToArray($exception) |
||
167 | |||
168 | /** |
||
169 | * Converts special characters to HTML entities. |
||
170 | * @param string $text to encode. |
||
171 | * @return string encoded original text. |
||
172 | */ |
||
173 | 3 | public function htmlEncode($text) |
|
177 | |||
178 | /** |
||
179 | * Adds informational links to the given PHP type/class. |
||
180 | * @param string $code type/class name to be linkified. |
||
181 | * @return string linkified with HTML type/class name. |
||
182 | */ |
||
183 | public function addTypeLinks($code) |
||
213 | |||
214 | /** |
||
215 | * Returns the informational link URL for a given PHP type/class. |
||
216 | * @param string $class the type or class name. |
||
217 | * @param string|null $method the method name. |
||
218 | * @return string|null the informational link URL. |
||
219 | * @see addTypeLinks() |
||
220 | */ |
||
221 | protected function getTypeUrl($class, $method) |
||
235 | |||
236 | /** |
||
237 | * Renders a view file as a PHP script. |
||
238 | * @param string $_file_ the view file. |
||
239 | * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
240 | * @return string the rendering result |
||
241 | */ |
||
242 | 3 | public function renderFile($_file_, $_params_) |
|
256 | |||
257 | /** |
||
258 | * Renders the previous exception stack for a given Exception. |
||
259 | * @param \Exception $exception the exception whose precursors should be rendered. |
||
260 | * @return string HTML content of the rendered previous exceptions. |
||
261 | * Empty string if there are none. |
||
262 | */ |
||
263 | public function renderPreviousExceptions($exception) |
||
271 | |||
272 | /** |
||
273 | * Renders a single call stack element. |
||
274 | * @param string|null $file name where call has happened. |
||
275 | * @param int|null $line number on which call has happened. |
||
276 | * @param string|null $class called class name. |
||
277 | * @param string|null $method called function/method name. |
||
278 | * @param array $args array of method arguments. |
||
279 | * @param int $index number of the call stack element. |
||
280 | * @return string HTML content of the rendered call stack element. |
||
281 | */ |
||
282 | public function renderCallStackItem($file, $line, $class, $method, $args, $index) |
||
310 | |||
311 | /** |
||
312 | * Renders call stack. |
||
313 | * @param \Exception $exception exception to get call stack from |
||
314 | * @return string HTML content of the rendered call stack. |
||
315 | */ |
||
316 | public function renderCallStack(\Exception $exception) |
||
317 | { |
||
318 | $out = '<ul>'; |
||
319 | $out .= $this->renderCallStackItem($exception->getFile(), $exception->getLine(), null, null, [], 1); |
||
320 | for ($i = 0, $trace = $exception->getTrace(), $length = count($trace); $i < $length; ++$i) { |
||
321 | $file = !empty($trace[$i]['file']) ? $trace[$i]['file'] : null; |
||
322 | $line = !empty($trace[$i]['line']) ? $trace[$i]['line'] : null; |
||
323 | $class = !empty($trace[$i]['class']) ? $trace[$i]['class'] : null; |
||
324 | $function = null; |
||
325 | if (!empty($trace[$i]['function']) && $trace[$i]['function'] !== 'unknown') { |
||
326 | $function = $trace[$i]['function']; |
||
327 | } |
||
328 | $args = !empty($trace[$i]['args']) ? $trace[$i]['args'] : []; |
||
329 | $out .= $this->renderCallStackItem($file, $line, $class, $function, $args, $i + 2); |
||
330 | } |
||
331 | $out .= '</ul>'; |
||
332 | return $out; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Renders the global variables of the request. |
||
337 | * List of global variables is defined in [[displayVars]]. |
||
338 | * @return string the rendering result |
||
339 | * @see displayVars |
||
340 | */ |
||
341 | public function renderRequest() |
||
342 | { |
||
343 | $request = ''; |
||
344 | foreach ($this->displayVars as $name) { |
||
345 | if (!empty($GLOBALS[$name])) { |
||
346 | $request .= '$' . $name . ' = ' . VarDumper::export($GLOBALS[$name]) . ";\n\n"; |
||
347 | } |
||
348 | } |
||
349 | |||
350 | return '<pre>' . $this->htmlEncode(rtrim($request, "\n")) . '</pre>'; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Determines whether given name of the file belongs to the framework. |
||
355 | * @param string $file name to be checked. |
||
356 | * @return bool whether given name of the file belongs to the framework. |
||
357 | */ |
||
358 | public function isCoreFile($file) |
||
359 | { |
||
360 | return $file === null || strpos(realpath($file), YII2_PATH . DIRECTORY_SEPARATOR) === 0; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Creates HTML containing link to the page with the information on given HTTP status code. |
||
365 | * @param int $statusCode to be used to generate information link. |
||
366 | * @param string $statusDescription Description to display after the the status code. |
||
367 | * @return string generated HTML with HTTP status code information. |
||
368 | */ |
||
369 | public function createHttpStatusLink($statusCode, $statusDescription) |
||
370 | { |
||
371 | return '<a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#' . (int) $statusCode . '" target="_blank">HTTP ' . (int) $statusCode . ' – ' . $statusDescription . '</a>'; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Creates string containing HTML link which refers to the home page of determined web-server software |
||
376 | * and its full name. |
||
377 | * @return string server software information hyperlink. |
||
378 | */ |
||
379 | public function createServerInformationLink() |
||
380 | { |
||
381 | $serverUrls = [ |
||
382 | 'http://httpd.apache.org/' => ['apache'], |
||
383 | 'http://nginx.org/' => ['nginx'], |
||
384 | 'http://lighttpd.net/' => ['lighttpd'], |
||
385 | 'http://gwan.com/' => ['g-wan', 'gwan'], |
||
386 | 'http://iis.net/' => ['iis', 'services'], |
||
387 | 'http://php.net/manual/en/features.commandline.webserver.php' => ['development'], |
||
388 | ]; |
||
389 | if (isset($_SERVER['SERVER_SOFTWARE'])) { |
||
390 | foreach ($serverUrls as $url => $keywords) { |
||
391 | foreach ($keywords as $keyword) { |
||
392 | if (stripos($_SERVER['SERVER_SOFTWARE'], $keyword) !== false) { |
||
393 | return '<a href="' . $url . '" target="_blank">' . $this->htmlEncode($_SERVER['SERVER_SOFTWARE']) . '</a>'; |
||
394 | } |
||
395 | } |
||
396 | } |
||
397 | } |
||
398 | |||
399 | return ''; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Creates string containing HTML link which refers to the page with the current version |
||
404 | * of the framework and version number text. |
||
405 | * @return string framework version information hyperlink. |
||
406 | */ |
||
407 | public function createFrameworkVersionLink() |
||
411 | |||
412 | /** |
||
413 | * Converts arguments array to its string representation |
||
414 | * |
||
415 | * @param array $args arguments array to be converted |
||
416 | * @return string string representation of the arguments array |
||
417 | */ |
||
418 | public function argumentsToString($args) |
||
465 | |||
466 | /** |
||
467 | * Returns human-readable exception name |
||
468 | * @param \Exception $exception |
||
469 | * @return string human-readable exception name or null if it cannot be determined |
||
470 | */ |
||
471 | 3 | public function getExceptionName($exception) |
|
478 | } |
||
479 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: