Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
AtEase
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 suppressWarnings
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
5
 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 * 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
21namespace Wikimedia\AtEase;
22
23class 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                self::$originalLevel = error_reporting( E_ALL & ~(
45                    E_WARNING |
46                    E_NOTICE |
47                    E_USER_WARNING |
48                    E_USER_NOTICE |
49                    E_DEPRECATED |
50                    E_USER_DEPRECATED |
51                    E_STRICT
52                ) );
53            }
54            ++self::$suppressCount;
55        }
56    }
57
58    /**
59     * Restore error level to previous value
60     */
61    public static function restoreWarnings() {
62        self::suppressWarnings( true );
63    }
64
65    /**
66     * Call the callback given by the first parameter, suppressing any warnings.
67     *
68     * @param callable $callback Function to call
69     * @param mixed ...$args Optional arguments for the function call
70     * @return mixed
71     */
72    public static function quietCall( callable $callback, ...$args ) {
73        self::suppressWarnings();
74        $rv = null;
75        try {
76            $rv = $callback( ...$args );
77        } finally {
78            self::restoreWarnings();
79        }
80        return $rv;
81    }
82
83}