PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
ErrorToExceptionHandler.php
1<?php
2
3/*
4 * PocketMine Standard PHP Library
5 * Copyright (C) 2019 PocketMine Team <https://github.com/pmmp/PocketMine-SPL>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16*/
17
18declare(strict_types=1);
19
20namespace pocketmine\errorhandler;
21
22use function error_reporting;
23use function restore_error_handler;
24use function set_error_handler;
25use const E_NOTICE;
26use const E_WARNING;
27
29 private function __construct(){
30
31 }
32
34 private static $lastSilencedError = null;
35
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);
42 }
43
44 self::$lastSilencedError = new ErrorRecord($severity, $message, $file, $line);
45 return true; //stfu operator
46 }
47
48 public static function getLastSilencedError() : ErrorRecord{
49 if(self::$lastSilencedError === null){
50 throw new \LogicException("No error has been generated");
51 }
52 return self::$lastSilencedError;
53 }
54
55 public static function clearLastSilencedError() : void{
56 self::$lastSilencedError = null;
57 }
58
60 public static function getAndClearLastSilencedError() : ErrorRecord{
61 $result = self::getLastSilencedError();
62 self::clearLastSilencedError();
63 return $result;
64 }
65
69 public static function set(int $levels = E_WARNING | E_NOTICE) : void{
70 set_error_handler([self::class, 'handle'], $levels);
71 }
72
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);
83 };
84 }
85
96 public static function trap(\Closure $closure, int $levels = E_WARNING | E_NOTICE){
97 set_error_handler(self::throwAll(), $levels);
98 try{
99 return $closure();
100 }finally{
101 restore_error_handler();
102 }
103 }
104
115 public static function trapAndRemoveFalse(\Closure $closure, int $levels = E_WARNING | E_NOTICE){
116 set_error_handler(self::throwAll(), $levels);
117 try{
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.");
121 }
122 return $result;
123 }finally{
124 restore_error_handler();
125 }
126 }
127}
static trapAndRemoveFalse(\Closure $closure, int $levels=E_WARNING|E_NOTICE)
static handle(int $severity, string $message, string $file, int $line)
static trap(\Closure $closure, int $levels=E_WARNING|E_NOTICE)