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 | 1 | protected function renderException($exception) |
|
90 | { |
||
91 | 1 | if (Yii::$app->has('response')) { |
|
92 | 1 | $response = Yii::$app->getResponse(); |
|
93 | // reset parameters of response to avoid interference with partially created response data |
||
94 | // in case the error occurred while sending the response. |
||
95 | 1 | $response->isSent = false; |
|
96 | 1 | $response->bodyRange = null; |
|
97 | 1 | $response->data = null; |
|
98 | 1 | $response->setBody(null); |
|
99 | } else { |
||
100 | $response = new Response(); |
||
101 | } |
||
102 | |||
103 | 1 | $useCustomErrorAction = $this->errorAction !== null && (!YII_DEBUG || $exception instanceof UserException); |
|
104 | 1 | $response->setStatusCodeByException($exception); |
|
105 | |||
106 | 1 | if ($useCustomErrorAction) { |
|
107 | $result = Yii::$app->runAction($this->errorAction); |
||
108 | if ($result instanceof Response) { |
||
109 | $response = $result; |
||
110 | } else { |
||
111 | $response->data = $result; |
||
112 | } |
||
113 | 1 | } elseif ($response->format === Response::FORMAT_HTML) { |
|
114 | 1 | if ($this->shouldRenderSimpleHtml()) { |
|
115 | // AJAX request |
||
116 | $response->data = '<pre>' . $this->htmlEncode(static::convertExceptionToString($exception)) . '</pre>'; |
||
117 | } else { |
||
118 | // if there is an error during error rendering it's useful to |
||
119 | // display PHP error in debug mode instead of a blank screen |
||
120 | 1 | if (YII_DEBUG) { |
|
121 | 1 | ini_set('display_errors', 1); |
|
122 | } |
||
123 | 1 | $useErrorView = $response->format === Response::FORMAT_HTML && (!YII_DEBUG || $exception instanceof UserException); |
|
124 | 1 | $file = $useErrorView ? $this->errorView : $this->exceptionView; |
|
125 | 1 | $response->data = $this->renderFile($file, [ |
|
126 | 1 | 'exception' => $exception, |
|
127 | ]); |
||
128 | } |
||
129 | } elseif ($response->format === Response::FORMAT_RAW) { |
||
130 | $response->data = static::convertExceptionToString($exception); |
||
131 | } else { |
||
132 | $response->data = $this->convertExceptionToArray($exception); |
||
133 | } |
||
134 | |||
135 | 1 | $response->send(); |
|
136 | 1 | } |
|
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 | 5 | public function renderFile($_file_, $_params_) |
|
268 | |||
269 | /** |
||
270 | * Renders the previous exception stack for a given Exception. |
||
271 | * @param \Exception $exception the exception whose precursors should be rendered. |
||
272 | * @return string HTML content of the rendered previous exceptions. |
||
273 | * Empty string if there are none. |
||
274 | */ |
||
275 | public function renderPreviousExceptions($exception) |
||
283 | |||
284 | /** |
||
285 | * Renders a single call stack element. |
||
286 | * @param string|null $file name where call has happened. |
||
287 | * @param int|null $line number on which call has happened. |
||
288 | * @param string|null $class called class name. |
||
289 | * @param string|null $method called function/method name. |
||
290 | * @param array $args array of method arguments. |
||
291 | * @param int $index number of the call stack element. |
||
292 | * @return string HTML content of the rendered call stack element. |
||
293 | */ |
||
294 | 1 | public function renderCallStackItem($file, $line, $class, $method, $args, $index) |
|
322 | |||
323 | /** |
||
324 | * Renders call stack. |
||
325 | * @param \Exception|\ParseError $exception exception to get call stack from |
||
326 | * @return string HTML content of the rendered call stack. |
||
327 | * @since 2.0.12 |
||
328 | */ |
||
329 | public function renderCallStack($exception) |
||
347 | |||
348 | /** |
||
349 | * Renders the global variables of the request. |
||
350 | * List of global variables is defined in [[displayVars]]. |
||
351 | * @return string the rendering result |
||
352 | * @see displayVars |
||
353 | */ |
||
354 | public function renderRequest() |
||
365 | |||
366 | /** |
||
367 | * Determines whether given name of the file belongs to the framework. |
||
368 | * @param string $file name to be checked. |
||
369 | * @return bool whether given name of the file belongs to the framework. |
||
370 | */ |
||
371 | 1 | public function isCoreFile($file) |
|
375 | |||
376 | /** |
||
377 | * Creates HTML containing link to the page with the information on given HTTP status code. |
||
378 | * @param int $statusCode to be used to generate information link. |
||
379 | * @param string $statusDescription Description to display after the the status code. |
||
380 | * @return string generated HTML with HTTP status code information. |
||
381 | */ |
||
382 | public function createHttpStatusLink($statusCode, $statusDescription) |
||
386 | |||
387 | /** |
||
388 | * Creates string containing HTML link which refers to the home page of determined web-server software |
||
389 | * and its full name. |
||
390 | * @return string server software information hyperlink. |
||
391 | */ |
||
392 | public function createServerInformationLink() |
||
414 | |||
415 | /** |
||
416 | * Creates string containing HTML link which refers to the page with the current version |
||
417 | * of the framework and version number text. |
||
418 | * @return string framework version information hyperlink. |
||
419 | */ |
||
420 | public function createFrameworkVersionLink() |
||
424 | |||
425 | /** |
||
426 | * Converts arguments array to its string representation. |
||
427 | * |
||
428 | * @param array $args arguments array to be converted |
||
429 | * @return string string representation of the arguments array |
||
430 | */ |
||
431 | public function argumentsToString($args) |
||
478 | |||
479 | /** |
||
480 | * Returns human-readable exception name. |
||
481 | * @param \Exception $exception |
||
482 | * @return string human-readable exception name or null if it cannot be determined |
||
483 | */ |
||
484 | 3 | public function getExceptionName($exception) |
|
494 | |||
495 | /** |
||
496 | * @return bool if simple HTML should be rendered |
||
497 | * @since 2.0.12 |
||
498 | */ |
||
499 | protected function shouldRenderSimpleHtml() |
||
503 | } |
||
504 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: