29 private function __construct(){
34 private static $lastSilencedError =
null;
39 public static function handle(
int $severity,
string $message,
string $file,
int $line) : bool{
40 if((error_reporting() & $severity) !== 0){
41 throw new \ErrorException($message, 0, $severity, $file, $line);
44 self::$lastSilencedError =
new ErrorRecord($severity, $message, $file, $line);
48 public static function getLastSilencedError() : ErrorRecord{
49 if(self::$lastSilencedError === null){
50 throw new \LogicException(
"No error has been generated");
52 return self::$lastSilencedError;
55 public static function clearLastSilencedError() : void{
56 self::$lastSilencedError = null;
61 $result = self::getLastSilencedError();
62 self::clearLastSilencedError();
69 public static function set(
int $levels = E_WARNING | E_NOTICE) : void{
70 set_error_handler([self::class,
'handle'], $levels);
80 private static function throwAll() : \Closure{
81 return function(int $severity, string $message, string $file, int $line): bool{
82 throw new \ErrorException($message, 0, $severity, $file, $line);
96 public static function trap(\Closure $closure,
int $levels = E_WARNING | E_NOTICE){
97 set_error_handler(self::throwAll(), $levels);
101 restore_error_handler();
116 set_error_handler(self::throwAll(), $levels);
118 $result = $closure();
119 if($result ===
false){
120 throw new \LogicException(
"Block must not return false when no error occurred. Use trap() if the block may return false.");
124 restore_error_handler();