Total Complexity | 47 |
Total Lines | 379 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ErrorProvider 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.
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 ErrorProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class ErrorProvider extends ApplicationProvider |
||
12 | { |
||
13 | /** |
||
14 | * @var null|string |
||
15 | */ |
||
16 | protected $lang; |
||
17 | |||
18 | /** |
||
19 | * @var null|object |
||
20 | */ |
||
21 | protected $exception; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $data = array(); |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $result = []; |
||
32 | |||
33 | /** |
||
34 | * get status according to exception trace |
||
35 | * |
||
36 | * @return void |
||
37 | */ |
||
38 | private function getStatusAccordingToExceptionTrace() |
||
39 | { |
||
40 | if($this->app->has('exceptiontrace')) { |
||
41 | $this->data['status'] = (int)$this->app['exceptiontrace']['callNamespace']->getCode(); |
||
42 | } |
||
43 | else { |
||
44 | $this->data['status'] = (int)$this->exception::exceptionTypeCodes($this->data['errType']); |
||
|
|||
45 | } |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @return void|mixed |
||
50 | */ |
||
51 | private function getStatusFromContext() |
||
52 | { |
||
53 | $this->getStatusAccordingToExceptionTrace(); |
||
54 | |||
55 | $this->app->terminate('responseSuccess'); |
||
56 | $this->app->terminate('responseStatus'); |
||
57 | $this->app->register('responseSuccess',(bool)false); |
||
58 | $this->app->register('responseStatus',$this->data['status']); |
||
59 | |||
60 | |||
61 | $optionalException = str_replace("\\","\\\\",$this->app->namespace()->exception()); |
||
62 | |||
63 | if(preg_match('@'.$optionalException.'@is',$this->data['errType'])){ |
||
64 | |||
65 | //trace pattern |
||
66 | $trace = $this->data['errContext']['trace']; |
||
67 | if(preg_match('@Stack trace:\n#0(.*)\n#1@is',$trace,$traceArray)){ |
||
68 | |||
69 | $traceFile = str_replace(root,'',$traceArray[1]); |
||
70 | |||
71 | if(preg_match('@(.*)\((\d+)\)@is',$traceFile,$traceResolve)){ |
||
72 | $this->data['errFile'] = $traceResolve[1]; |
||
73 | $this->data['errLine'] = (int)$traceResolve[2]; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | |||
78 | $this->data['errType'] = class_basename($this->data['errType']); |
||
79 | } |
||
80 | |||
81 | if(is_array($meta = config('response.meta'))){ |
||
82 | |||
83 | //set as the success object is false |
||
84 | $this->data['appExceptionSuccess'] = []; |
||
85 | } |
||
86 | else{ |
||
87 | |||
88 | //set as the success object is false |
||
89 | $this->data['appExceptionSuccess'] = ['success'=>(bool)false,'status'=>$this->data['status']]; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * error provider handle |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | public function handle() |
||
99 | { |
||
100 | //sets which php errors are reported |
||
101 | error_reporting(0); |
||
102 | |||
103 | // in general we will use the exception class |
||
104 | // in the store/config directory to make it possible |
||
105 | // to change the user-based exceptions. |
||
106 | $this->exception = StaticPathModel::$store.'\Config\Exception'; |
||
107 | |||
108 | //This function can be used for defining your own way of handling errors during runtime, |
||
109 | //for example in applications in which you need to do cleanup of data/files when a critical error happens, |
||
110 | //or when you need to trigger an error under certain conditions (using trigger_error()). |
||
111 | set_error_handler([$this,'setErrorHandler']); |
||
112 | |||
113 | //Registers a callback to be executed after script execution finishes or exit() is called. |
||
114 | //Multiple calls to register_shutdown_function() can be made, and each will be called in the same order as |
||
115 | //they were registered. If you call exit() within one registered shutdown function, |
||
116 | //processing will stop completely and no other registered shutdown functions will be called. |
||
117 | register_shutdown_function([$this,'fatalErrorShutdownHandler']); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * set error handler |
||
122 | * |
||
123 | * @param null|string $errNo |
||
124 | * @param null|string $errStr |
||
125 | * @param null|string $errFile |
||
126 | * @param null|string $errLine |
||
127 | * @param null|string $errContext |
||
128 | */ |
||
129 | public function setErrorHandler($errNo=null, $errStr=null, $errFile=null, $errLine=null, $errContext=null) |
||
223 | } |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @param $environment |
||
228 | * @return mixed |
||
229 | */ |
||
230 | private function getAppException($environment,$message) |
||
231 | { |
||
232 | return $this->data['appExceptionSuccess']+$this->data['exception']::$environment( |
||
233 | $this->data['errNo'], |
||
234 | $message, |
||
235 | $this->data['errFile'], |
||
236 | $this->data['errLine'], |
||
237 | $this->data['errType'], |
||
238 | $this->data['lang'] |
||
239 | ); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * get exception extender object |
||
244 | * |
||
245 | * @return mixed |
||
246 | */ |
||
247 | private function getExceptionExtender() |
||
248 | { |
||
249 | return $this->app->resolve(ExceptionExtender::class, |
||
250 | ['result'=>$this->result])->getResult(); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * fatal error shutdown handler |
||
255 | * |
||
256 | * @return mixed |
||
257 | */ |
||
258 | public function fatalErrorShutdownHandler() |
||
259 | { |
||
260 | //get fatal error |
||
261 | $last_error = error_get_last(); |
||
262 | |||
263 | $this->inStackTrace($last_error); |
||
264 | |||
265 | if(!is_null($last_error)){ |
||
266 | |||
267 | if(!defined('methodName')){ |
||
268 | define('methodName',null); |
||
269 | } |
||
270 | |||
271 | if($this->app->has('exceptionFile')){ |
||
272 | $last_error['file'] = $this->app['exceptionFile']; |
||
273 | $last_error['line'] = $this->app['exceptionLine']; |
||
274 | } |
||
275 | |||
276 | $this->setErrorHandler( |
||
277 | E_ERROR, |
||
278 | $last_error['message'], |
||
279 | $last_error['file'], |
||
280 | $last_error['line'], |
||
281 | [] |
||
282 | ); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @param $error |
||
288 | */ |
||
289 | public function inStackTrace($error) |
||
290 | { |
||
291 | if($this->app->has('urlComponent')){ |
||
292 | if(!preg_match('@'.$this->app['urlComponent']['project'].'@',$error['file']) |
||
293 | && !$this->app->has('exceptionFile')){ |
||
294 | if(preg_match('@ in\s(.*?)\n@is',$error['message'],$result)){ |
||
295 | $errorMessage = explode(":",$result[1]); |
||
296 | $this->app->register('exceptionFile',$errorMessage[0]); |
||
297 | $this->app->register('exceptionLine',$errorMessage[1]); |
||
298 | } |
||
299 | } |
||
300 | } |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @return void|mixed |
||
305 | */ |
||
306 | private function getLangMessageForException() |
||
307 | { |
||
308 | $clone = clone $this; |
||
309 | |||
310 | if($this->app->has('exceptionTranslate')){ |
||
311 | |||
312 | $langMessage = trans('exception.'.$this->app->get('exceptionTranslate')); |
||
313 | |||
314 | if(!is_null($langMessage) && $this->app->has('exceptionTranslateParams')){ |
||
315 | |||
316 | if(count($this->app['exceptionTranslateParams'][$this->app['exceptionTranslate']])){ |
||
317 | foreach ($this->app['exceptionTranslateParams'][$this->app['exceptionTranslate']] as $key=>$value){ |
||
318 | |||
319 | $valueLangName = !is_null(trans('default.'.$value)) ? trans('default.'.$value) : $value; |
||
320 | $langMessage = preg_replace('@\('.$key.'\)@is',$valueLangName,$langMessage); |
||
321 | } |
||
322 | } |
||
323 | } |
||
324 | |||
325 | if($langMessage!==null){ |
||
326 | $this->data['errStrReal'] = $langMessage; |
||
327 | } |
||
328 | } |
||
329 | |||
330 | if(class_exists($this->data['errorClassNamespace']) |
||
331 | && |
||
332 | (Str::startsWith($this->data['errorClassNamespace'],'App') |
||
333 | || Str::startsWith($this->data['errorClassNamespace'],__NAMESPACE__))){ |
||
334 | |||
335 | ClosureDispatcher::bind($this->data['errorClassNamespace'])->call(function() use ($clone) { |
||
336 | if(property_exists($this,'lang')){ |
||
337 | $clone->setLang($this->lang); |
||
338 | } |
||
339 | }); |
||
340 | } |
||
341 | |||
342 | $this->data['lang'] = $clone->lang; |
||
343 | |||
344 | $langMessage = (!is_null($this->data['lang'])) ? trans('exception.'.$this->data['lang']) : null; |
||
345 | |||
346 | if($langMessage!==null){ |
||
347 | $this->data['errStrReal'] = $langMessage; |
||
348 | } |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * get uncaught process |
||
353 | * |
||
354 | * @return void|mixed |
||
355 | */ |
||
356 | private function getUncaughtProcess() |
||
357 | { |
||
358 | // catch exception via preg match |
||
359 | // and then clear the Uncaught statement from inside. |
||
360 | if(preg_match('@(.*?):@is',$this->data['errStrReal'],$errArr)){ |
||
361 | |||
362 | $this->data['errType'] = trim(str_replace('Uncaught','',$errArr[1])); |
||
363 | $this->data['errorClassNamespace'] = $this->data['errType']; |
||
364 | } |
||
365 | |||
366 | if(preg_match('@Uncaught@is',$this->data['errStrReal']) |
||
367 | && preg_match('@(.*?):(.*?)\sin\s@is',$this->data['errStrReal'],$errStrRealArray)){ |
||
368 | $this->data['errStrReal'] = trim($errStrRealArray[2]); |
||
369 | } |
||
370 | |||
371 | $this->data['errContext']['trace'] = $this->data['errStrReal']; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * get result for exception |
||
376 | * |
||
377 | * @return array |
||
378 | */ |
||
379 | public function getResult() |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * @param null|string $lang |
||
386 | */ |
||
387 | public function setLang($lang=null) |
||
388 | { |
||
389 | $this->lang = $lang; |
||
390 | } |
||
391 | |||
392 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.