| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Jasny\ErrorHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Trait for handling uncaught exceptions using PHP's exception handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | trait HandleUncaughtException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @var callable|false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $chainedExceptionHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * Log the following exception classes (and subclasses) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $logExceptionClasses = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * Wrapper method for `set_error_handler` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param callable $callback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param int      $error_types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @return callable|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     abstract protected function setErrorHandler($callback, $error_types = E_ALL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Wrapper method for `set_exception_handler` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param callable $callback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return callable|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     abstract protected function setExceptionHandler($callback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Log an error or exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param \Exception|\Error $error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     abstract public function log($error); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * Get the error handler that has been replaced. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @return callable|false|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 2 |  |     public function getChainedExceptionHandler() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |         return $this->chainedExceptionHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * Get a list of Exception and other Throwable classes that will be logged | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  |     public function getLoggedExceptionClasses() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 4 |  |         return $this->logExceptionClasses; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * Log these types of errors or exceptions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param string $class  Exception class name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 18 |  |     protected function logUncaughtException($class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 18 |  |         if (!in_array($class, $this->logExceptionClasses)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 18 |  |             $this->logExceptionClasses[] = $class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 9 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 18 |  |         $this->initExceptionHandler(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 18 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Use the global error handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 18 |  |     protected function initExceptionHandler() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 18 |  |         if (!isset($this->chainedExceptionHandler)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 18 |  |             $this->chainedExceptionHandler = $this->setExceptionHandler([$this, 'handleException']) ?: false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 9 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 18 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * Uncaught exception handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @ignore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param \Exception|\Error $exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 18 |  |     public function handleException($exception) | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 18 |  |         $this->setExceptionHandler(null); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 18 |  |         $this->setErrorHandler(null); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 18 |  |         $isInstanceOf = array_map(function($class) use ($exception) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 12 |  |             return is_a($exception, $class); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 | 18 |  |         }, $this->logExceptionClasses); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 108 | 18 |  |         if ($exception instanceof \Error || $exception instanceof \ErrorException) { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 | 4 |  |             $type = $exception instanceof \Error ? $exception->getCode() : $exception->getSeverity(); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 | 4 |  |             $shouldLog = $this->logErrorTypes & $type; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 111 | 2 |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 112 | 14 |  |             $shouldLog = array_sum($isInstanceOf) > 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 115 | 18 |  |         if ($shouldLog) { | 
            
                                                                        
                            
            
                                    
            
            
                | 116 | 12 |  |             $this->log($exception); | 
            
                                                                        
                            
            
                                    
            
            
                | 117 | 6 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 119 | 18 |  |         $this->callOnFatalError($exception); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 121 | 18 |  |         if ($this->chainedExceptionHandler) { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 | 6 |  |             call_user_func($this->chainedExceptionHandler, $exception); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 | 3 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 125 | 18 |  |         throw $exception; // This is now handled by the default exception and error handler | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  | 
            
                        
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@returndoc comment to communicate to implementors of these methods what they are expected to return.