| Total Complexity | 55 |
| Total Lines | 412 |
| 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() |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * error provider handle |
||
| 95 | * |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | public function handle() |
||
| 118 | |||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * set error handler |
||
| 123 | * |
||
| 124 | * @param null|string $errNo |
||
| 125 | * @param null|string $errStr |
||
| 126 | * @param null|string $errFile |
||
| 127 | * @param null|string $errLine |
||
| 128 | * @param null|string $errContext |
||
| 129 | */ |
||
| 130 | public function setErrorHandler($errNo=null, $errStr=null, $errFile=null, $errLine=null, $errContext=null) |
||
| 131 | {
|
||
| 132 | // in case of a deficiency, |
||
| 133 | // we need to boot our general needs to be needed for the exception. |
||
| 134 | Dependencies::loadBootstrapperNeedsForException(); |
||
| 135 | |||
| 136 | // in general we will use the exception class |
||
| 137 | // in the store/config directory to make it possible |
||
| 138 | // to change the user-based exceptions. |
||
| 139 | $this->data['exception'] = $this->exception; |
||
| 140 | $this->data['errType'] = 'Undefined'; |
||
| 141 | $this->data['errStrReal'] = $errStr; |
||
| 142 | $this->data['errorClassNamespace'] = null; |
||
| 143 | $this->data['errFile'] = $errFile; |
||
| 144 | $this->data['errLine'] = $errLine; |
||
| 145 | $this->data['errNo'] = $errNo; |
||
| 146 | |||
| 147 | // catch exception via preg match |
||
| 148 | // and then clear the Uncaught statement from inside. |
||
| 149 | $this->getUncaughtProcess(); |
||
| 150 | |||
| 151 | //get status from context |
||
| 152 | $this->getStatusFromContext(); |
||
| 153 | |||
| 154 | //get lang message for exception |
||
| 155 | $this->getLangMessageForException(); |
||
| 156 | |||
| 157 | if($this->app->has('exceptiontrace')){
|
||
| 158 | |||
| 159 | $customExceptionTrace = $this->app['exceptiontrace']; |
||
| 160 | $this->data['errFile'] = $customExceptionTrace['file']; |
||
| 161 | $this->data['errLine'] = $customExceptionTrace['line']; |
||
| 162 | } |
||
| 163 | |||
| 164 | $environment = $this->app->environment(); |
||
| 165 | |||
| 166 | $vendorDirectory = str_replace(root.''.DIRECTORY_SEPARATOR.'','',$this->data['errFile']); |
||
| 167 | |||
| 168 | if(preg_match('@vendor.*\.php@is',$vendorDirectory,$vd)){
|
||
| 169 | $vendorDirectory = $vd[0]; |
||
| 170 | } |
||
| 171 | |||
| 172 | if(Str::startsWith($vendorDirectory,'vendor') |
||
| 173 | && Str::startsWith($vendorDirectory,'vendor/php-resta')===false) |
||
| 174 | {
|
||
| 175 | |||
| 176 | $externalMessage = ($environment==="production") ? |
||
| 177 | 'An unexpected external error has occurred' : |
||
| 178 | $this->data['errStrReal']; |
||
| 179 | |||
| 180 | $this->result = $this->getAppException($environment,$externalMessage); |
||
| 181 | |||
| 182 | |||
| 183 | //Get or Set the HTTP response code |
||
| 184 | http_response_code(500); |
||
| 185 | $this->app->terminate('responseStatus');
|
||
| 186 | $this->app->register('responseStatus',500);
|
||
| 187 | |||
| 188 | |||
| 189 | } |
||
| 190 | else{
|
||
| 191 | |||
| 192 | if($this->data['status']=='500' && $environment=='production'){
|
||
| 193 | $externalMessage = 'An unexpected external error has occurred'; |
||
| 194 | } |
||
| 195 | else{
|
||
| 196 | $externalMessage = $this->data['errStrReal']; |
||
| 197 | } |
||
| 198 | |||
| 199 | $this->result = $this->getAppException($environment,$externalMessage); |
||
| 200 | |||
| 201 | //Get or Set the HTTP response code |
||
| 202 | http_response_code($this->data['status']); |
||
| 203 | } |
||
| 204 | |||
| 205 | // exception extender The exception information |
||
| 206 | // that is presented as an extra to the exception result set. |
||
| 207 | $this->result = $this->getExceptionExtender(); |
||
| 208 | |||
| 209 | if($environment==="production"){
|
||
| 210 | $productionLogMessage = $this->getAppException('local',$this->data['errStrReal']);
|
||
| 211 | $productionLogMessage = array_merge($this->result,$productionLogMessage); |
||
| 212 | $this->app->register('productionLogMessage',$this->app->get('out')->outputFormatter($productionLogMessage));
|
||
| 213 | } |
||
| 214 | |||
| 215 | //set json app exception |
||
| 216 | $this->app->register('routerResult',$this->result);
|
||
| 217 | |||
| 218 | if($this->app->has('exceptionResponse')){
|
||
| 219 | $exceptionResponse = $this->app->get('exceptionResponse');
|
||
| 220 | $exceptionResponse((isset($productionLogMessage)) ? $productionLogMessage : $this->result,$this->data['status']); |
||
| 221 | } |
||
| 222 | |||
| 223 | $restaOutHandle = null; |
||
| 224 | |||
| 225 | if(!defined('responseApp')){
|
||
| 226 | $restaOutHandle = $this->app->get('out')->handle();
|
||
| 227 | } |
||
| 228 | |||
| 229 | header("HTTP/1.1 ".$this->data['status']);
|
||
| 230 | |||
| 231 | if($restaOutHandle===null){
|
||
| 232 | |||
| 233 | //header set and symfony response call |
||
| 234 | $lastResult = $this->app->get('out')->outputFormatter($this->result);
|
||
| 235 | |||
| 236 | if($this->app->has('clientResponseType')){
|
||
| 237 | |||
| 238 | $responseType = $this->app->get('clientResponseType');
|
||
| 239 | echo app()->resolve($this->app->get('out')->formatter())->{$responseType}($lastResult);
|
||
| 240 | } |
||
| 241 | else{
|
||
| 242 | |||
| 243 | $defaultResponseType = (is_null(config('app.response'))) ? 'json' : config('app.response');
|
||
| 244 | echo app()->resolve($this->app->get('out')->formatter())->{$defaultResponseType}($lastResult);
|
||
| 245 | } |
||
| 246 | |||
| 247 | |||
| 248 | } |
||
| 249 | else{
|
||
| 250 | echo $restaOutHandle; |
||
| 251 | } |
||
| 252 | |||
| 253 | exit(); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param $environment |
||
| 258 | * @return mixed |
||
| 259 | */ |
||
| 260 | private function getAppException($environment,$message) |
||
| 261 | {
|
||
| 262 | return $this->data['appExceptionSuccess']+$this->data['exception']::$environment( |
||
| 263 | $this->data['errNo'], |
||
| 264 | $message, |
||
| 265 | $this->data['errFile'], |
||
| 266 | $this->data['errLine'], |
||
| 267 | $this->data['errType'], |
||
| 268 | $this->data['lang'] |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * get exception extender object |
||
| 274 | * |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | private function getExceptionExtender() |
||
| 278 | {
|
||
| 279 | return $this->app->resolve( |
||
| 280 | $this->app->get('macro')->call('exceptionExtender',function(){
|
||
| 281 | return ExceptionExtender::class; |
||
| 282 | }), |
||
| 283 | ['result'=>$this->result])->getResult(); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * fatal error shutdown handler |
||
| 288 | * |
||
| 289 | * @return mixed |
||
| 290 | */ |
||
| 291 | public function fatalErrorShutdownHandler() |
||
| 292 | {
|
||
| 293 | //get fatal error |
||
| 294 | $last_error = error_get_last(); |
||
| 295 | |||
| 296 | $this->inStackTrace($last_error); |
||
| 297 | |||
| 298 | if(!is_null($last_error)){
|
||
| 299 | |||
| 300 | if(!defined('methodName')){
|
||
| 301 | define('methodName',null);
|
||
| 302 | } |
||
| 303 | |||
| 304 | if($this->app->has('exceptionFile')){
|
||
| 305 | $last_error['file'] = $this->app['exceptionFile']; |
||
| 306 | $last_error['line'] = $this->app['exceptionLine']; |
||
| 307 | } |
||
| 308 | |||
| 309 | $this->setErrorHandler( |
||
| 310 | E_ERROR, |
||
| 311 | $last_error['message'], |
||
| 312 | $last_error['file'], |
||
| 313 | $last_error['line'], |
||
| 314 | [] |
||
| 315 | ); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param $error |
||
| 321 | */ |
||
| 322 | public function inStackTrace($error) |
||
| 323 | {
|
||
| 324 | if($this->app->has('urlComponent') && isset($error['file'])){
|
||
| 325 | if(!preg_match('@'.$this->app['urlComponent']['project'].'@',$error['file'])
|
||
| 326 | && !$this->app->has('exceptionFile')){
|
||
| 327 | if(preg_match('@ in\s(.*?)\n@is',$error['message'],$result)){
|
||
| 328 | $errorMessage = explode(":",$result[1]);
|
||
| 329 | $this->app->register('exceptionFile',$errorMessage[0]);
|
||
| 330 | $this->app->register('exceptionLine',$errorMessage[1]);
|
||
| 331 | } |
||
| 332 | } |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return void|mixed |
||
| 338 | */ |
||
| 339 | private function getLangMessageForException() |
||
| 340 | {
|
||
| 341 | $clone = clone $this; |
||
| 342 | |||
| 343 | if($this->app->has('exceptionTranslate')){
|
||
| 344 | |||
| 345 | $langMessage = trans('exception.'.$this->app->get('exceptionTranslate'));
|
||
| 346 | |||
| 347 | if(!is_null($langMessage) && $this->app->has('exceptionTranslateParams')){
|
||
| 348 | |||
| 349 | if(count($this->app['exceptionTranslateParams'][$this->app['exceptionTranslate']])){
|
||
| 350 | foreach ($this->app['exceptionTranslateParams'][$this->app['exceptionTranslate']] as $key=>$value){
|
||
| 351 | |||
| 352 | $valueLangName = !is_null(trans('default.'.$value)) ? trans('default.'.$value) : $value;
|
||
| 353 | $langMessage = preg_replace('@\('.$key.'\)@is',$valueLangName,$langMessage);
|
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | if($langMessage!==null){
|
||
| 359 | $this->data['errStrReal'] = $langMessage; |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | if(class_exists($this->data['errorClassNamespace']) |
||
| 364 | && |
||
| 365 | (Str::startsWith($this->data['errorClassNamespace'],'App') |
||
| 366 | || Str::startsWith($this->data['errorClassNamespace'],__NAMESPACE__))){
|
||
| 367 | |||
| 368 | ClosureDispatcher::bind($this->data['errorClassNamespace'])->call(function() use ($clone) {
|
||
| 369 | if(property_exists($this,'lang')){
|
||
| 370 | $clone->setLang($this->lang); |
||
| 371 | } |
||
| 372 | }); |
||
| 373 | } |
||
| 374 | |||
| 375 | $this->data['lang'] = $clone->lang; |
||
| 376 | |||
| 377 | $langMessage = (!is_null($this->data['lang'])) ? trans('exception.'.$this->data['lang']) : null;
|
||
| 378 | |||
| 379 | if($langMessage!==null){
|
||
| 380 | $this->data['errStrReal'] = $langMessage; |
||
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * get uncaught process |
||
| 386 | * |
||
| 387 | * @return void|mixed |
||
| 388 | */ |
||
| 389 | private function getUncaughtProcess() |
||
| 390 | {
|
||
| 391 | // catch exception via preg match |
||
| 392 | // and then clear the Uncaught statement from inside. |
||
| 393 | if(preg_match('@(.*?):@is',$this->data['errStrReal'],$errArr)){
|
||
| 394 | |||
| 395 | $this->data['errType'] = trim(str_replace('Uncaught','',$errArr[1]));
|
||
| 396 | $this->data['errorClassNamespace'] = $this->data['errType']; |
||
| 397 | } |
||
| 398 | |||
| 399 | if(preg_match('@Uncaught@is',$this->data['errStrReal'])
|
||
| 400 | && preg_match('@(.*?):(.*?)\sin\s@is',$this->data['errStrReal'],$errStrRealArray)){
|
||
| 401 | $this->data['errStrReal'] = trim($errStrRealArray[2]); |
||
| 402 | } |
||
| 403 | |||
| 404 | $this->data['errContext']['trace'] = $this->data['errStrReal']; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * get result for exception |
||
| 409 | * |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | public function getResult() |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param null|string $lang |
||
| 419 | */ |
||
| 420 | public function setLang($lang=null) |
||
| 423 | } |
||
| 424 | |||
| 425 | } |
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.