| 1 |
<?php |
| 2 |
/** |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License as published by |
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 15 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 |
* http://www.gnu.org/copyleft/gpl.html |
| 17 |
* |
| 18 |
* @file |
| 19 |
*/ |
| 20 |
|
| 21 |
namespace Wikimedia\AtEase; |
| 22 |
|
| 23 |
class AtEase { |
| 24 |
private static $suppressCount = 0; |
| 25 |
|
| 26 |
/** @var false|int */ |
| 27 |
private static $originalLevel = false; |
| 28 |
|
| 29 |
/** |
| 30 |
* Reference-counted warning suppression |
| 31 |
* |
| 32 |
* @param bool $end Whether to restore warnings |
| 33 |
*/ |
| 34 |
public static function suppressWarnings( $end = false ) { |
| 35 |
if ( $end ) { |
| 36 |
if ( self::$suppressCount ) { |
| 37 |
--self::$suppressCount; |
| 38 |
if ( !self::$suppressCount ) { |
| 39 |
error_reporting( self::$originalLevel ); |
| 40 |
} |
| 41 |
} |
| 42 |
} else { |
| 43 |
if ( !self::$suppressCount ) { |
| 44 |
// T375707 - E_STRICT is deprecated on PHP >= 8.4 |
| 45 |
if ( PHP_VERSION_ID < 80400 ) { |
| 46 |
self::$originalLevel = error_reporting( E_ALL & ~( |
| 47 |
E_WARNING | |
| 48 |
E_NOTICE | |
| 49 |
E_USER_WARNING | |
| 50 |
E_USER_NOTICE | |
| 51 |
E_DEPRECATED | |
| 52 |
E_USER_DEPRECATED | |
| 53 |
E_STRICT |
| 54 |
) ); |
| 55 |
} else { |
| 56 |
self::$originalLevel = error_reporting( E_ALL & ~( |
| 57 |
E_WARNING | |
| 58 |
E_NOTICE | |
| 59 |
E_USER_WARNING | |
| 60 |
E_USER_NOTICE | |
| 61 |
E_DEPRECATED | |
| 62 |
E_USER_DEPRECATED |
| 63 |
) ); |
| 64 |
} |
| 65 |
} |
| 66 |
++self::$suppressCount; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Restore error level to previous value |
| 72 |
*/ |
| 73 |
public static function restoreWarnings() { |
| 74 |
self::suppressWarnings( true ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Call the callback given by the first parameter, suppressing any warnings. |
| 79 |
* |
| 80 |
* @param callable $callback Function to call |
| 81 |
* @param mixed ...$args Optional arguments for the function call |
| 82 |
* @return mixed |
| 83 |
*/ |
| 84 |
public static function quietCall( callable $callback, ...$args ) { |
| 85 |
self::suppressWarnings(); |
| 86 |
$rv = null; |
| 87 |
try { |
| 88 |
$rv = $callback( ...$args ); |
| 89 |
} finally { |
| 90 |
self::restoreWarnings(); |
| 91 |
} |
| 92 |
return $rv; |
| 93 |
} |
| 94 |
|
| 95 |
} |