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 | * @var string trace line with placeholders to be be substituted. |
||
74 | * The placeholders are {file}, {line} and {text} and the string should be as follows. |
||
75 | * |
||
76 | * `File: {file} - Line: {line} - Text: {text}` |
||
77 | * |
||
78 | * @example <a href="ide://open?file={file}&line={line}">{html}</a> |
||
79 | * @see https://github.com/yiisoft/yii2-debug#open-files-in-ide |
||
80 | * @since 2.0.14 |
||
81 | */ |
||
82 | public $traceLine = '{html}'; |
||
83 | |||
84 | |||
85 | /** |
||
86 | * Renders the exception. |
||
87 | * @param \Exception|\Error $exception the exception to be rendered. |
||
88 | */ |
||
89 | 2 | protected function renderException($exception) |
|
137 | |||
138 | /** |
||
139 | * Converts an exception into an array. |
||
140 | * @param \Exception|\Error $exception the exception being converted |
||
141 | * @return array the array representation of the exception. |
||
142 | */ |
||
143 | protected function convertExceptionToArray($exception) |
||
174 | |||
175 | /** |
||
176 | * Converts special characters to HTML entities. |
||
177 | * @param string $text to encode. |
||
178 | * @return string encoded original text. |
||
179 | */ |
||
180 | 4 | public function htmlEncode($text) |
|
184 | |||
185 | /** |
||
186 | * Adds informational links to the given PHP type/class. |
||
187 | * @param string $code type/class name to be linkified. |
||
188 | * @return string linkified with HTML type/class name. |
||
189 | */ |
||
190 | public function addTypeLinks($code) |
||
225 | |||
226 | /** |
||
227 | * Returns the informational link URL for a given PHP type/class. |
||
228 | * @param string $class the type or class name. |
||
229 | * @param string|null $method the method name. |
||
230 | * @return string|null the informational link URL. |
||
231 | * @see addTypeLinks() |
||
232 | */ |
||
233 | protected function getTypeUrl($class, $method) |
||
247 | |||
248 | /** |
||
249 | * Renders a view file as a PHP script. |
||
250 | * @param string $_file_ the view file. |
||
251 | * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
252 | * @return string the rendering result |
||
253 | */ |
||
254 | 6 | public function renderFile($_file_, $_params_) |
|
271 | |||
272 | /** |
||
273 | * Renders the previous exception stack for a given Exception. |
||
274 | * @param \Exception $exception the exception whose precursors should be rendered. |
||
275 | * @return string HTML content of the rendered previous exceptions. |
||
276 | * Empty string if there are none. |
||
277 | */ |
||
278 | public function renderPreviousExceptions($exception) |
||
286 | |||
287 | /** |
||
288 | * Renders a single call stack element. |
||
289 | * @param string|null $file name where call has happened. |
||
290 | * @param int|null $line number on which call has happened. |
||
291 | * @param string|null $class called class name. |
||
292 | * @param string|null $method called function/method name. |
||
293 | * @param array $args array of method arguments. |
||
294 | * @param int $index number of the call stack element. |
||
295 | * @return string HTML content of the rendered call stack element. |
||
296 | */ |
||
297 | 1 | public function renderCallStackItem($file, $line, $class, $method, $args, $index) |
|
325 | |||
326 | /** |
||
327 | * Renders call stack. |
||
328 | * @param \Exception|\ParseError $exception exception to get call stack from |
||
329 | * @return string HTML content of the rendered call stack. |
||
330 | * @since 2.0.12 |
||
331 | */ |
||
332 | public function renderCallStack($exception) |
||
350 | |||
351 | /** |
||
352 | * Renders the global variables of the request. |
||
353 | * List of global variables is defined in [[displayVars]]. |
||
354 | * @return string the rendering result |
||
355 | * @see displayVars |
||
356 | */ |
||
357 | public function renderRequest() |
||
368 | |||
369 | /** |
||
370 | * Determines whether given name of the file belongs to the framework. |
||
371 | * @param string $file name to be checked. |
||
372 | * @return bool whether given name of the file belongs to the framework. |
||
373 | */ |
||
374 | 1 | public function isCoreFile($file) |
|
378 | |||
379 | /** |
||
380 | * Creates HTML containing link to the page with the information on given HTTP status code. |
||
381 | * @param int $statusCode to be used to generate information link. |
||
382 | * @param string $statusDescription Description to display after the the status code. |
||
383 | * @return string generated HTML with HTTP status code information. |
||
384 | */ |
||
385 | public function createHttpStatusLink($statusCode, $statusDescription) |
||
389 | |||
390 | /** |
||
391 | * Creates string containing HTML link which refers to the home page of determined web-server software |
||
392 | * and its full name. |
||
393 | * @return string server software information hyperlink. |
||
394 | */ |
||
395 | public function createServerInformationLink() |
||
417 | |||
418 | /** |
||
419 | * Creates string containing HTML link which refers to the page with the current version |
||
420 | * of the framework and version number text. |
||
421 | * @return string framework version information hyperlink. |
||
422 | */ |
||
423 | public function createFrameworkVersionLink() |
||
427 | |||
428 | /** |
||
429 | * Converts arguments array to its string representation. |
||
430 | * |
||
431 | * @param array $args arguments array to be converted |
||
432 | * @return string string representation of the arguments array |
||
433 | */ |
||
434 | public function argumentsToString($args) |
||
481 | |||
482 | /** |
||
483 | * Returns human-readable exception name. |
||
484 | * @param \Exception $exception |
||
485 | * @return string human-readable exception name or null if it cannot be determined |
||
486 | */ |
||
487 | 3 | public function getExceptionName($exception) |
|
495 | |||
496 | /** |
||
497 | * @return bool if simple HTML should be rendered |
||
498 | * @since 2.0.12 |
||
499 | */ |
||
500 | protected function shouldRenderSimpleHtml() |
||
504 | } |
||
505 |
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: