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 | 1 | protected function renderException($exception) |
|
79 | { |
||
80 | 1 | if (Yii::$app->has('response')) { |
|
81 | 1 | $response = Yii::$app->getResponse(); |
|
82 | // reset parameters of response to avoid interference with partially created response data |
||
83 | // in case the error occurred while sending the response. |
||
84 | 1 | $response->isSent = false; |
|
85 | 1 | $response->stream = null; |
|
86 | 1 | $response->data = null; |
|
87 | 1 | $response->content = null; |
|
88 | 1 | } else { |
|
89 | $response = new Response(); |
||
90 | } |
||
91 | |||
92 | 1 | $response->setStatusCodeByException($exception); |
|
93 | |||
94 | 1 | $useErrorView = $response->format === Response::FORMAT_HTML && (!YII_DEBUG || $exception instanceof UserException); |
|
95 | |||
96 | 1 | if ($useErrorView && $this->errorAction !== null) { |
|
97 | $result = Yii::$app->runAction($this->errorAction); |
||
98 | if ($result instanceof Response) { |
||
99 | $response = $result; |
||
100 | } else { |
||
101 | $response->data = $result; |
||
102 | } |
||
103 | 1 | } elseif ($response->format === Response::FORMAT_HTML) { |
|
104 | 1 | if ($this->shouldRenderSimpleHtml()) { |
|
105 | // AJAX request |
||
106 | $response->data = '<pre>' . $this->htmlEncode(static::convertExceptionToString($exception)) . '</pre>'; |
||
107 | } else { |
||
108 | // if there is an error during error rendering it's useful to |
||
109 | // display PHP error in debug mode instead of a blank screen |
||
110 | 1 | if (YII_DEBUG) { |
|
111 | 1 | ini_set('display_errors', 1); |
|
112 | 1 | } |
|
113 | 1 | $file = $useErrorView ? $this->errorView : $this->exceptionView; |
|
114 | 1 | $response->data = $this->renderFile($file, [ |
|
115 | 1 | 'exception' => $exception, |
|
116 | 1 | ]); |
|
117 | } |
||
118 | 1 | } elseif ($response->format === Response::FORMAT_RAW) { |
|
119 | $response->data = static::convertExceptionToString($exception); |
||
120 | } else { |
||
121 | $response->data = $this->convertExceptionToArray($exception); |
||
122 | } |
||
123 | |||
124 | 1 | $response->send(); |
|
125 | 1 | } |
|
126 | |||
127 | /** |
||
128 | * Converts an exception into an array. |
||
129 | * @param \Exception $exception the exception being converted |
||
130 | * @return array the array representation of the exception. |
||
131 | */ |
||
132 | protected function convertExceptionToArray($exception) |
||
133 | { |
||
134 | if (!YII_DEBUG && !$exception instanceof UserException && !$exception instanceof HttpException) { |
||
135 | $exception = new HttpException(500, Yii::t('yii', 'An internal server error occurred.')); |
||
136 | } |
||
137 | |||
138 | $array = [ |
||
139 | 'name' => ($exception instanceof Exception || $exception instanceof ErrorException) ? $exception->getName() : 'Exception', |
||
140 | 'message' => $exception->getMessage(), |
||
141 | 'code' => $exception->getCode(), |
||
142 | ]; |
||
143 | if ($exception instanceof HttpException) { |
||
144 | $array['status'] = $exception->statusCode; |
||
145 | } |
||
146 | if (YII_DEBUG) { |
||
147 | $array['type'] = get_class($exception); |
||
148 | if (!$exception instanceof UserException) { |
||
149 | $array['file'] = $exception->getFile(); |
||
150 | $array['line'] = $exception->getLine(); |
||
151 | $array['stack-trace'] = explode("\n", $exception->getTraceAsString()); |
||
152 | if ($exception instanceof \yii\db\Exception) { |
||
153 | $array['error-info'] = $exception->errorInfo; |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | if (($prev = $exception->getPrevious()) !== null) { |
||
158 | $array['previous'] = $this->convertExceptionToArray($prev); |
||
159 | } |
||
160 | |||
161 | return $array; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Converts special characters to HTML entities. |
||
166 | * @param string $text to encode. |
||
167 | * @return string encoded original text. |
||
168 | */ |
||
169 | 3 | public function htmlEncode($text) |
|
170 | { |
||
171 | 3 | 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) |
||
180 | { |
||
181 | if (preg_match('/(.*?)::([^(]+)/', $code, $matches)) { |
||
182 | $class = $matches[1]; |
||
183 | $method = $matches[2]; |
||
184 | $text = $this->htmlEncode($class) . '::' . $this->htmlEncode($method); |
||
185 | } else { |
||
186 | $class = $code; |
||
187 | $method = null; |
||
188 | $text = $this->htmlEncode($class); |
||
189 | } |
||
190 | |||
191 | $url = null; |
||
192 | |||
193 | $shouldGenerateLink = true; |
||
194 | if ($method !== null) { |
||
195 | $reflection = new \ReflectionMethod($class, $method); |
||
196 | $shouldGenerateLink = $reflection->isPublic() || $reflection->isProtected(); |
||
197 | } |
||
198 | |||
199 | if ($shouldGenerateLink) { |
||
200 | $url = $this->getTypeUrl($class, $method); |
||
201 | } |
||
202 | |||
203 | if ($url === null) { |
||
204 | return $text; |
||
205 | } |
||
206 | |||
207 | return '<a href="' . $url . '" target="_blank">' . $text . '</a>'; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Returns the informational link URL for a given PHP type/class. |
||
212 | * @param string $class the type or class name. |
||
213 | * @param string|null $method the method name. |
||
214 | * @return string|null the informational link URL. |
||
215 | * @see addTypeLinks() |
||
216 | */ |
||
217 | protected function getTypeUrl($class, $method) |
||
218 | { |
||
219 | if (strpos($class, 'yii\\') !== 0) { |
||
220 | return null; |
||
221 | } |
||
222 | |||
223 | $page = $this->htmlEncode(strtolower(str_replace('\\', '-', $class))); |
||
224 | $url = "http://www.yiiframework.com/doc-2.0/$page.html"; |
||
225 | if ($method) { |
||
|
|||
226 | $url .= "#$method()-detail"; |
||
227 | } |
||
228 | |||
229 | return $url; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Renders a view file as a PHP script. |
||
234 | * @param string $_file_ the view file. |
||
235 | * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
236 | * @return string the rendering result |
||
237 | */ |
||
238 | 4 | public function renderFile($_file_, $_params_) |
|
239 | { |
||
240 | 4 | $_params_['handler'] = $this; |
|
241 | 4 | if ($this->exception instanceof ErrorException || !Yii::$app->has('view')) { |
|
242 | ob_start(); |
||
243 | ob_implicit_flush(false); |
||
244 | extract($_params_, EXTR_OVERWRITE); |
||
245 | require(Yii::getAlias($_file_)); |
||
246 | |||
247 | return ob_get_clean(); |
||
248 | } else { |
||
249 | 4 | return Yii::$app->getView()->renderFile($_file_, $_params_, $this); |
|
250 | } |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Renders the previous exception stack for a given Exception. |
||
255 | * @param \Exception $exception the exception whose precursors should be rendered. |
||
256 | * @return string HTML content of the rendered previous exceptions. |
||
257 | * Empty string if there are none. |
||
258 | */ |
||
259 | public function renderPreviousExceptions($exception) |
||
260 | { |
||
261 | if (($previous = $exception->getPrevious()) !== null) { |
||
262 | return $this->renderFile($this->previousExceptionView, ['exception' => $previous]); |
||
263 | } else { |
||
264 | return ''; |
||
265 | } |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Renders a single call stack element. |
||
270 | * @param string|null $file name where call has happened. |
||
271 | * @param int|null $line number on which call has happened. |
||
272 | * @param string|null $class called class name. |
||
273 | * @param string|null $method called function/method name. |
||
274 | * @param array $args array of method arguments. |
||
275 | * @param int $index number of the call stack element. |
||
276 | * @return string HTML content of the rendered call stack element. |
||
277 | */ |
||
278 | public function renderCallStackItem($file, $line, $class, $method, $args, $index) |
||
279 | { |
||
280 | $lines = []; |
||
281 | $begin = $end = 0; |
||
282 | if ($file !== null && $line !== null) { |
||
283 | $line--; // adjust line number from one-based to zero-based |
||
284 | $lines = @file($file); |
||
285 | if ($line < 0 || $lines === false || ($lineCount = count($lines)) < $line) { |
||
286 | return ''; |
||
287 | } |
||
288 | |||
289 | $half = (int) (($index === 1 ? $this->maxSourceLines : $this->maxTraceSourceLines) / 2); |
||
290 | $begin = $line - $half > 0 ? $line - $half : 0; |
||
291 | $end = $line + $half < $lineCount ? $line + $half : $lineCount - 1; |
||
292 | } |
||
293 | |||
294 | return $this->renderFile($this->callStackItemView, [ |
||
295 | 'file' => $file, |
||
296 | 'line' => $line, |
||
297 | 'class' => $class, |
||
298 | 'method' => $method, |
||
299 | 'index' => $index, |
||
300 | 'lines' => $lines, |
||
301 | 'begin' => $begin, |
||
302 | 'end' => $end, |
||
303 | 'args' => $args, |
||
304 | ]); |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Renders call stack. |
||
309 | * @param \Exception $exception exception to get call stack from |
||
310 | * @return string HTML content of the rendered call stack. |
||
311 | */ |
||
312 | public function renderCallStack(\Exception $exception) |
||
313 | { |
||
314 | $out = '<ul>'; |
||
315 | $out .= $this->renderCallStackItem($exception->getFile(), $exception->getLine(), null, null, [], 1); |
||
316 | for ($i = 0, $trace = $exception->getTrace(), $length = count($trace); $i < $length; ++$i) { |
||
317 | $file = !empty($trace[$i]['file']) ? $trace[$i]['file'] : null; |
||
318 | $line = !empty($trace[$i]['line']) ? $trace[$i]['line'] : null; |
||
319 | $class = !empty($trace[$i]['class']) ? $trace[$i]['class'] : null; |
||
320 | $function = null; |
||
321 | if (!empty($trace[$i]['function']) && $trace[$i]['function'] !== 'unknown') { |
||
322 | $function = $trace[$i]['function']; |
||
323 | } |
||
324 | $args = !empty($trace[$i]['args']) ? $trace[$i]['args'] : []; |
||
325 | $out .= $this->renderCallStackItem($file, $line, $class, $function, $args, $i + 2); |
||
326 | } |
||
327 | $out .= '</ul>'; |
||
328 | return $out; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Renders the global variables of the request. |
||
333 | * List of global variables is defined in [[displayVars]]. |
||
334 | * @return string the rendering result |
||
335 | * @see displayVars |
||
336 | */ |
||
337 | public function renderRequest() |
||
338 | { |
||
339 | $request = ''; |
||
340 | foreach ($this->displayVars as $name) { |
||
341 | if (!empty($GLOBALS[$name])) { |
||
342 | $request .= '$' . $name . ' = ' . VarDumper::export($GLOBALS[$name]) . ";\n\n"; |
||
343 | } |
||
344 | } |
||
345 | |||
346 | return '<pre>' . $this->htmlEncode(rtrim($request, "\n")) . '</pre>'; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Determines whether given name of the file belongs to the framework. |
||
351 | * @param string $file name to be checked. |
||
352 | * @return bool whether given name of the file belongs to the framework. |
||
353 | */ |
||
354 | public function isCoreFile($file) |
||
355 | { |
||
356 | return $file === null || strpos(realpath($file), YII2_PATH . DIRECTORY_SEPARATOR) === 0; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Creates HTML containing link to the page with the information on given HTTP status code. |
||
361 | * @param int $statusCode to be used to generate information link. |
||
362 | * @param string $statusDescription Description to display after the the status code. |
||
363 | * @return string generated HTML with HTTP status code information. |
||
364 | */ |
||
365 | public function createHttpStatusLink($statusCode, $statusDescription) |
||
366 | { |
||
367 | return '<a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#' . (int) $statusCode . '" target="_blank">HTTP ' . (int) $statusCode . ' – ' . $statusDescription . '</a>'; |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Creates string containing HTML link which refers to the home page of determined web-server software |
||
372 | * and its full name. |
||
373 | * @return string server software information hyperlink. |
||
374 | */ |
||
375 | public function createServerInformationLink() |
||
376 | { |
||
377 | $serverUrls = [ |
||
378 | 'http://httpd.apache.org/' => ['apache'], |
||
379 | 'http://nginx.org/' => ['nginx'], |
||
380 | 'http://lighttpd.net/' => ['lighttpd'], |
||
381 | 'http://gwan.com/' => ['g-wan', 'gwan'], |
||
382 | 'http://iis.net/' => ['iis', 'services'], |
||
383 | 'http://php.net/manual/en/features.commandline.webserver.php' => ['development'], |
||
384 | ]; |
||
385 | if (isset($_SERVER['SERVER_SOFTWARE'])) { |
||
386 | foreach ($serverUrls as $url => $keywords) { |
||
387 | foreach ($keywords as $keyword) { |
||
388 | if (stripos($_SERVER['SERVER_SOFTWARE'], $keyword) !== false) { |
||
389 | return '<a href="' . $url . '" target="_blank">' . $this->htmlEncode($_SERVER['SERVER_SOFTWARE']) . '</a>'; |
||
390 | } |
||
391 | } |
||
392 | } |
||
393 | } |
||
394 | |||
395 | return ''; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Creates string containing HTML link which refers to the page with the current version |
||
400 | * of the framework and version number text. |
||
401 | * @return string framework version information hyperlink. |
||
402 | */ |
||
403 | public function createFrameworkVersionLink() |
||
407 | |||
408 | /** |
||
409 | * Converts arguments array to its string representation |
||
410 | * |
||
411 | * @param array $args arguments array to be converted |
||
412 | * @return string string representation of the arguments array |
||
413 | */ |
||
414 | public function argumentsToString($args) |
||
415 | { |
||
416 | $count = 0; |
||
417 | $isAssoc = $args !== array_values($args); |
||
418 | |||
419 | foreach ($args as $key => $value) { |
||
420 | $count++; |
||
461 | |||
462 | /** |
||
463 | * Returns human-readable exception name |
||
464 | * @param \Exception $exception |
||
465 | * @return string human-readable exception name or null if it cannot be determined |
||
466 | */ |
||
467 | public function getExceptionName($exception) |
||
474 | |||
475 | /** |
||
476 | * @return bool if simple HTML should be rendered |
||
477 | */ |
||
478 | protected function shouldRenderSimpleHtml() |
||
482 | } |
||
483 |
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: