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|\Error $exception the exception to be rendered. |
||
77 | */ |
||
78 | 1 | protected function renderException($exception) |
|
126 | |||
127 | /** |
||
128 | * Converts an exception into an array. |
||
129 | * @param \Exception|\Error $exception the exception being converted |
||
130 | * @return array the array representation of the exception. |
||
131 | */ |
||
132 | protected function convertExceptionToArray($exception) |
||
163 | |||
164 | /** |
||
165 | * Converts special characters to HTML entities. |
||
166 | * @param string $text to encode. |
||
167 | * @return string encoded original text. |
||
168 | */ |
||
169 | public function htmlEncode($text) |
||
170 | { |
||
171 | return htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Adds informational links to the given PHP type/class. |
||
176 | * @param string $code type/class name to be linkified. |
||
177 | * @return string linkified with HTML type/class name. |
||
178 | */ |
||
179 | public function addTypeLinks($code) |
||
214 | |||
215 | /** |
||
216 | * Returns the informational link URL for a given PHP type/class. |
||
217 | * @param string $class the type or class name. |
||
218 | * @param string|null $method the method name. |
||
219 | * @return string|null the informational link URL. |
||
220 | * @see addTypeLinks() |
||
221 | */ |
||
222 | protected function getTypeUrl($class, $method) |
||
236 | |||
237 | /** |
||
238 | * Renders a view file as a PHP script. |
||
239 | * @param string $_file_ the view file. |
||
240 | * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
241 | * @return string the rendering result |
||
242 | */ |
||
243 | 1 | public function renderFile($_file_, $_params_) |
|
244 | { |
||
245 | 1 | $_params_['handler'] = $this; |
|
246 | 1 | if ($this->exception instanceof ErrorException || !Yii::$app->has('view')) { |
|
247 | ob_start(); |
||
248 | ob_implicit_flush(false); |
||
249 | extract($_params_, EXTR_OVERWRITE); |
||
250 | require Yii::getAlias($_file_); |
||
251 | |||
252 | return ob_get_clean(); |
||
253 | } |
||
254 | |||
255 | 1 | return Yii::$app->getView()->renderFile($_file_, $_params_, $this); |
|
256 | } |
||
257 | |||
258 | /** |
||
259 | * Renders the previous exception stack for a given Exception. |
||
260 | * @param \Exception $exception the exception whose precursors should be rendered. |
||
261 | * @return string HTML content of the rendered previous exceptions. |
||
262 | * Empty string if there are none. |
||
263 | */ |
||
264 | public function renderPreviousExceptions($exception) |
||
265 | { |
||
266 | if (($previous = $exception->getPrevious()) !== null) { |
||
267 | return $this->renderFile($this->previousExceptionView, ['exception' => $previous]); |
||
268 | } |
||
269 | |||
270 | return ''; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Renders a single call stack element. |
||
275 | * @param string|null $file name where call has happened. |
||
276 | * @param int|null $line number on which call has happened. |
||
277 | * @param string|null $class called class name. |
||
278 | * @param string|null $method called function/method name. |
||
279 | * @param array $args array of method arguments. |
||
280 | * @param int $index number of the call stack element. |
||
281 | * @return string HTML content of the rendered call stack element. |
||
282 | */ |
||
283 | public function renderCallStackItem($file, $line, $class, $method, $args, $index) |
||
311 | |||
312 | /** |
||
313 | * Renders call stack. |
||
314 | * @param \Exception|\ParseError $exception exception to get call stack from |
||
315 | * @return string HTML content of the rendered call stack. |
||
316 | * @since 2.0.12 |
||
317 | */ |
||
318 | public function renderCallStack($exception) |
||
336 | |||
337 | /** |
||
338 | * Renders the global variables of the request. |
||
339 | * List of global variables is defined in [[displayVars]]. |
||
340 | * @return string the rendering result |
||
341 | * @see displayVars |
||
342 | */ |
||
343 | public function renderRequest() |
||
354 | |||
355 | /** |
||
356 | * Determines whether given name of the file belongs to the framework. |
||
357 | * @param string $file name to be checked. |
||
358 | * @return bool whether given name of the file belongs to the framework. |
||
359 | */ |
||
360 | public function isCoreFile($file) |
||
364 | |||
365 | /** |
||
366 | * Creates HTML containing link to the page with the information on given HTTP status code. |
||
367 | * @param int $statusCode to be used to generate information link. |
||
368 | * @param string $statusDescription Description to display after the the status code. |
||
369 | * @return string generated HTML with HTTP status code information. |
||
370 | */ |
||
371 | public function createHttpStatusLink($statusCode, $statusDescription) |
||
375 | |||
376 | /** |
||
377 | * Creates string containing HTML link which refers to the home page of determined web-server software |
||
378 | * and its full name. |
||
379 | * @return string server software information hyperlink. |
||
380 | */ |
||
381 | public function createServerInformationLink() |
||
403 | |||
404 | /** |
||
405 | * Creates string containing HTML link which refers to the page with the current version |
||
406 | * of the framework and version number text. |
||
407 | * @return string framework version information hyperlink. |
||
408 | */ |
||
409 | public function createFrameworkVersionLink() |
||
413 | |||
414 | /** |
||
415 | * Converts arguments array to its string representation |
||
416 | * |
||
417 | * @param array $args arguments array to be converted |
||
418 | * @return string string representation of the arguments array |
||
419 | */ |
||
420 | public function argumentsToString($args) |
||
467 | |||
468 | /** |
||
469 | * Returns human-readable exception name |
||
470 | * @param \Exception $exception |
||
471 | * @return string human-readable exception name or null if it cannot be determined |
||
472 | */ |
||
473 | public function getExceptionName($exception) |
||
482 | |||
483 | /** |
||
484 | * @return bool if simple HTML should be rendered |
||
485 | * @since 2.0.12 |
||
486 | */ |
||
487 | protected function shouldRenderSimpleHtml() |
||
491 | } |
||
492 |
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: