| Total Complexity | 46 |
| Total Lines | 352 |
| 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 |
||
| 12 | class ErrorProvider extends ApplicationProvider {
|
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var $lang |
||
|
|
|||
| 16 | */ |
||
| 17 | public $lang = null; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var $exception |
||
| 21 | */ |
||
| 22 | protected $exception; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var $data array |
||
| 26 | */ |
||
| 27 | protected $data=array(); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @return mixed|string |
||
| 31 | */ |
||
| 32 | private function getEnvironmentStatus(){
|
||
| 33 | |||
| 34 | // application key, but if it has a null value |
||
| 35 | // then we move the environment value to the production environment. |
||
| 36 | return $this->app->environment(); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return void|mixed |
||
| 41 | */ |
||
| 42 | private function getStatusFromContext(){
|
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @method handle |
||
| 82 | * return void |
||
| 83 | */ |
||
| 84 | public function handle() |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param null $errNo |
||
| 108 | * @param null $errStr |
||
| 109 | * @param null $errFile |
||
| 110 | * @param null $errLine |
||
| 111 | * @param null $errContext |
||
| 112 | */ |
||
| 113 | public function setErrorHandler($errNo=null, $errStr=null, $errFile=null, $errLine=null, $errContext=null) |
||
| 221 | } |
||
| 222 | |||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param $environment |
||
| 227 | * @return mixed |
||
| 228 | */ |
||
| 229 | private function getAppException($environment,$message) |
||
| 230 | {
|
||
| 231 | return $this->data['appExceptionSuccess']+$this->data['exception']::$environment( |
||
| 232 | $this->data['errNo'], |
||
| 233 | $message, |
||
| 234 | $this->data['errFile'], |
||
| 235 | $this->data['errLine'], |
||
| 236 | $this->data['errType'], |
||
| 237 | $this->data['lang'] |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @method fatalErrorShutdownHandler |
||
| 243 | */ |
||
| 244 | public function fatalErrorShutdownHandler() |
||
| 245 | {
|
||
| 246 | //get fatal error |
||
| 247 | $last_error =error_get_last(); |
||
| 248 | |||
| 249 | $this->inStactTrace($last_error); |
||
| 250 | |||
| 251 | if($last_error!==null){
|
||
| 252 | |||
| 253 | if(!defined('methodName')){
|
||
| 254 | |||
| 255 | define('methodName',null);
|
||
| 256 | } |
||
| 257 | |||
| 258 | if(isset(core()->exceptionFile)){
|
||
| 259 | $last_error['file'] = core()->exceptionFile; |
||
| 260 | $last_error['line'] = core()->exceptionLine; |
||
| 261 | } |
||
| 262 | |||
| 263 | $this->setErrorHandler( |
||
| 264 | E_ERROR, |
||
| 265 | $last_error['message'], |
||
| 266 | $last_error['file'], |
||
| 267 | $last_error['line'], |
||
| 268 | [] |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param $error |
||
| 275 | */ |
||
| 276 | public function inStactTrace($error) |
||
| 284 | } |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return void|mixed |
||
| 291 | */ |
||
| 292 | private function getLangMessageForException(){
|
||
| 293 | |||
| 294 | $clone = clone $this; |
||
| 295 | |||
| 296 | if(property_exists(core(),'exceptionTranslate')){
|
||
| 297 | |||
| 298 | $langMessage=trans('exception.'.core()->exceptionTranslate);
|
||
| 299 | |||
| 300 | if(!is_null($langMessage) && property_exists(core(),'exceptionTranslateParams')){
|
||
| 301 | |||
| 302 | if(count(core()->exceptionTranslateParams[core()->exceptionTranslate])){
|
||
| 303 | foreach (core()->exceptionTranslateParams[core()->exceptionTranslate] as $key=>$value){
|
||
| 304 | $langMessage=preg_replace('@\('.$key.'\)@is',$value,$langMessage);
|
||
| 305 | } |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | if($langMessage!==null){
|
||
| 310 | $this->data['errStrReal']=$langMessage; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | if(class_exists($this->data['errorClassNamespace']) |
||
| 315 | && |
||
| 316 | (Str::startsWith($this->data['errorClassNamespace'],'App') |
||
| 317 | || Str::startsWith($this->data['errorClassNamespace'],__NAMESPACE__))){
|
||
| 318 | |||
| 319 | ClosureDispatcher::bind($this->data['errorClassNamespace'])->call(function() use ($clone) {
|
||
| 320 | if(property_exists($this,'lang')){
|
||
| 321 | $clone->lang=$this->lang; |
||
| 322 | } |
||
| 323 | }); |
||
| 324 | } |
||
| 325 | |||
| 326 | $this->data['lang']=$lang=$clone->lang; |
||
| 327 | |||
| 328 | if($lang!==null){
|
||
| 329 | $langMessage=trans('exception.'.$lang);
|
||
| 330 | } |
||
| 331 | else{
|
||
| 332 | $langMessage=null; |
||
| 333 | } |
||
| 334 | |||
| 335 | |||
| 336 | if($langMessage!==null){
|
||
| 337 | $this->data['errStrReal']=$langMessage; |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @return void|mixed |
||
| 343 | */ |
||
| 344 | private function getUncaughtProcess(){
|
||
| 364 | } |
||
| 365 | } |
||
| 367 | } |