Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
74.19% covered (warning)
74.19%
23 / 31
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AtEase
74.19% covered (warning)
74.19%
23 / 31
66.67% covered (warning)
66.67%
2 / 3
9.10
0.00% covered (danger)
0.00%
0 / 1
 suppressWarnings
68.00% covered (warning)
68.00%
17 / 25
0.00% covered (danger)
0.00%
0 / 1
7.18
 restoreWarnings
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 quietCall
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace Wikimedia\AtEase;
8
9class AtEase {
10    private static int $suppressCount = 0;
11
12    /** @var false|int */
13    private static $originalLevel = false;
14
15    /**
16     * Reference-counted warning suppression
17     *
18     * @param bool $end Whether to restore warnings
19     *
20     * @deprecated (T253461) PHP 7+ ensures `@` does not suppress fatal errors. Use the native operator.
21     */
22    public static function suppressWarnings( $end = false ) {
23        if ( $end ) {
24            if ( self::$suppressCount ) {
25                --self::$suppressCount;
26                if ( !self::$suppressCount ) {
27                    error_reporting( self::$originalLevel );
28                }
29            }
30        } else {
31            if ( !self::$suppressCount ) {
32                // T375707 - E_STRICT is deprecated on PHP >= 8.4
33                if ( PHP_VERSION_ID < 80400 ) {
34                    self::$originalLevel = error_reporting( E_ALL & ~(
35                        E_WARNING |
36                        E_NOTICE |
37                        E_USER_WARNING |
38                        E_USER_NOTICE |
39                        E_DEPRECATED |
40                        E_USER_DEPRECATED |
41                        // @phan-suppress-next-line PhanDeprecatedGlobalConstant
42                        E_STRICT
43                    ) );
44                } else {
45                    self::$originalLevel = error_reporting( E_ALL & ~(
46                        E_WARNING |
47                        E_NOTICE |
48                        E_USER_WARNING |
49                        E_USER_NOTICE |
50                        E_DEPRECATED |
51                        E_USER_DEPRECATED
52                    ) );
53                }
54            }
55            ++self::$suppressCount;
56        }
57    }
58
59    /**
60     * Restore error level to previous value
61     *
62     * @deprecated (T253461) PHP 7+ ensures `@` does not suppress fatal errors. Use the native operator.
63     */
64    public static function restoreWarnings() {
65        // @phan-suppress-next-line PhanDeprecatedFunction
66        self::suppressWarnings( true );
67    }
68
69    /**
70     * Call the callback given by the first parameter, suppressing any warnings.
71     *
72     * @param callable $callback Function to call
73     * @param mixed ...$args Optional arguments for the function call
74     * @return mixed
75     *
76     * @deprecated (T253461) PHP 7+ ensures `@` does not suppress fatal errors. Use the native operator.
77     */
78    public static function quietCall( callable $callback, ...$args ) {
79        // @phan-suppress-next-line PhanDeprecatedFunction
80        self::suppressWarnings();
81        $rv = null;
82        try {
83            $rv = $callback( ...$args );
84        } finally {
85            // @phan-suppress-next-line PhanDeprecatedFunction
86            self::restoreWarnings();
87        }
88        return $rv;
89    }
90
91}