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 |
||
29 | class ErrorHandler extends \yii\base\ErrorHandler |
||
30 | { |
||
31 | /** |
||
32 | * @var integer maximum number of source code lines to be displayed. Defaults to 19. |
||
33 | */ |
||
34 | public $maxSourceLines = 19; |
||
35 | /** |
||
36 | * @var integer maximum number of trace source code lines to be displayed. Defaults to 13. |
||
37 | */ |
||
38 | public $maxTraceSourceLines = 13; |
||
39 | /** |
||
40 | * @var string the route (e.g. 'site/error') to the controller action that will be used |
||
41 | * to display external errors. Inside the action, it can retrieve the error information |
||
42 | * using `Yii::$app->errorHandler->exception. This property defaults to null, meaning ErrorHandler |
||
43 | * will handle the error display. |
||
44 | */ |
||
45 | public $errorAction; |
||
46 | /** |
||
47 | * @var string the path of the view file for rendering exceptions without call stack information. |
||
48 | */ |
||
49 | public $errorView = '@yii/views/errorHandler/error.php'; |
||
50 | /** |
||
51 | * @var string the path of the view file for rendering exceptions. |
||
52 | */ |
||
53 | public $exceptionView = '@yii/views/errorHandler/exception.php'; |
||
54 | /** |
||
55 | * @var string the path of the view file for rendering exceptions and errors call stack element. |
||
56 | */ |
||
57 | public $callStackItemView = '@yii/views/errorHandler/callStackItem.php'; |
||
58 | /** |
||
59 | * @var string the path of the view file for rendering previous exceptions. |
||
60 | */ |
||
61 | public $previousExceptionView = '@yii/views/errorHandler/previousException.php'; |
||
62 | /** |
||
63 | * @var array list of the PHP predefined variables that should be displayed on the error page. |
||
64 | * Note that a variable must be accessible via `$GLOBALS`. Otherwise it won't be displayed. |
||
65 | * Defaults to `['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION']`. |
||
66 | * @see renderRequest() |
||
67 | * @since 2.0.7 |
||
68 | */ |
||
69 | public $displayVars = ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION']; |
||
70 | |||
71 | |||
72 | /** |
||
73 | * Renders the exception. |
||
74 | * @param \Exception $exception the exception to be rendered. |
||
75 | */ |
||
76 | protected function renderException($exception) |
||
128 | |||
129 | /** |
||
130 | * Converts an exception into an array. |
||
131 | * @param \Exception $exception the exception being converted |
||
132 | * @return array the array representation of the exception. |
||
133 | */ |
||
134 | protected function convertExceptionToArray($exception) |
||
165 | |||
166 | /** |
||
167 | * Converts special characters to HTML entities. |
||
168 | * @param string $text to encode. |
||
169 | * @return string encoded original text. |
||
170 | */ |
||
171 | public function htmlEncode($text) |
||
175 | |||
176 | /** |
||
177 | * Adds informational links to the given PHP type/class. |
||
178 | * @param string $code type/class name to be linkified. |
||
179 | * @return string linkified with HTML type/class name. |
||
180 | */ |
||
181 | public function addTypeLinks($code) |
||
201 | |||
202 | /** |
||
203 | * Returns the informational link URL for a given PHP type/class. |
||
204 | * @param string $class the type or class name. |
||
205 | * @param string|null $method the method name. |
||
206 | * @return string|null the informational link URL. |
||
207 | * @see addTypeLinks() |
||
208 | */ |
||
209 | protected function getTypeUrl($class, $method) |
||
223 | |||
224 | /** |
||
225 | * Renders a view file as a PHP script. |
||
226 | * @param string $_file_ the view file. |
||
227 | * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
228 | * @return string the rendering result |
||
229 | */ |
||
230 | public function renderFile($_file_, $_params_) |
||
244 | |||
245 | /** |
||
246 | * Renders the previous exception stack for a given Exception. |
||
247 | * @param \Exception $exception the exception whose precursors should be rendered. |
||
248 | * @return string HTML content of the rendered previous exceptions. |
||
249 | * Empty string if there are none. |
||
250 | */ |
||
251 | public function renderPreviousExceptions($exception) |
||
259 | |||
260 | /** |
||
261 | * Renders a single call stack element. |
||
262 | * @param string|null $file name where call has happened. |
||
263 | * @param integer|null $line number on which call has happened. |
||
264 | * @param string|null $class called class name. |
||
265 | * @param string|null $method called function/method name. |
||
266 | * @param array $args array of method arguments. |
||
267 | * @param integer $index number of the call stack element. |
||
268 | * @return string HTML content of the rendered call stack element. |
||
269 | */ |
||
270 | public function renderCallStackItem($file, $line, $class, $method, $args, $index) |
||
298 | |||
299 | /** |
||
300 | * Renders the global variables of the request. |
||
301 | * List of global variables is defined in [[displayVars]]. |
||
302 | * @return string the rendering result |
||
303 | * @see displayVars |
||
304 | */ |
||
305 | public function renderRequest() |
||
316 | |||
317 | /** |
||
318 | * Determines whether given name of the file belongs to the framework. |
||
319 | * @param string $file name to be checked. |
||
320 | * @return boolean whether given name of the file belongs to the framework. |
||
321 | */ |
||
322 | public function isCoreFile($file) |
||
326 | |||
327 | /** |
||
328 | * Creates HTML containing link to the page with the information on given HTTP status code. |
||
329 | * @param integer $statusCode to be used to generate information link. |
||
330 | * @param string $statusDescription Description to display after the the status code. |
||
331 | * @return string generated HTML with HTTP status code information. |
||
332 | */ |
||
333 | public function createHttpStatusLink($statusCode, $statusDescription) |
||
337 | |||
338 | /** |
||
339 | * Creates string containing HTML link which refers to the home page of determined web-server software |
||
340 | * and its full name. |
||
341 | * @return string server software information hyperlink. |
||
342 | */ |
||
343 | public function createServerInformationLink() |
||
365 | |||
366 | /** |
||
367 | * Creates string containing HTML link which refers to the page with the current version |
||
368 | * of the framework and version number text. |
||
369 | * @return string framework version information hyperlink. |
||
370 | */ |
||
371 | public function createFrameworkVersionLink() |
||
375 | |||
376 | /** |
||
377 | * Converts arguments array to its string representation |
||
378 | * |
||
379 | * @param array $args arguments array to be converted |
||
380 | * @return string string representation of the arguments array |
||
381 | */ |
||
382 | public function argumentsToString($args) |
||
430 | |||
431 | /** |
||
432 | * Returns human-readable exception name |
||
433 | * @param \Exception $exception |
||
434 | * @return string human-readable exception name or null if it cannot be determined |
||
435 | */ |
||
436 | public function getExceptionName($exception) |
||
443 | } |
||
444 |
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: