Passed
Push — master ( 89369f...5a90e9 )
by Vladimir
03:51
created

Warning::enable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 7
cp 0.7143
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3.2098
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Error;
6
7
use const E_USER_WARNING;
8
use function trigger_error;
9
10
/**
11
 * Encapsulates warnings produced by the library.
12
 *
13
 * Warnings can be suppressed (individually or all) if required.
14
 * Also it is possible to override warning handler (which is **trigger_error()** by default)
15
 */
16
final class Warning
17
{
18
    const WARNING_ASSIGN             = 2;
19
    const WARNING_CONFIG             = 4;
20
    const WARNING_FULL_SCHEMA_SCAN   = 8;
21
    const WARNING_CONFIG_DEPRECATION = 16;
22
    const WARNING_NOT_A_TYPE         = 32;
23
    const ALL                        = 63;
24
25
    /** @var int */
26
    private static $enableWarnings = self::ALL;
27
28
    /** @var mixed[] */
29
    private static $warned = [];
30
31
    /** @var callable|null */
32
    private static $warningHandler;
33
34
    /**
35
     * Sets warning handler which can intercept all system warnings.
36
     * When not set, trigger_error() is used to notify about warnings.
37
     *
38
     * @api
39
     */
40
    public static function setWarningHandler(?callable $warningHandler = null)
41
    {
42
        self::$warningHandler = $warningHandler;
43
    }
44
45
    /**
46
     * Suppress warning by id (has no effect when custom warning handler is set)
47
     *
48
     * Usage example:
49
     * Warning::suppress(Warning::WARNING_NOT_A_TYPE)
50
     *
51
     * When passing true - suppresses all warnings.
52
     *
53
     * @api
54
     * @param bool|int $suppress
55
     */
56 68
    public static function suppress($suppress = true)
57
    {
58 68
        if ($suppress === true) {
59
            self::$enableWarnings = 0;
60 68
        } elseif ($suppress === false) {
61
            self::$enableWarnings = self::ALL;
62
        } else {
63 68
            $suppress = (int) $suppress;
64
65 68
            self::$enableWarnings &= ~$suppress;
66
        }
67 68
    }
68
69
    /**
70
     * Re-enable previously suppressed warning by id
71
     *
72
     * Usage example:
73
     * Warning::suppress(Warning::WARNING_NOT_A_TYPE)
74
     *
75
     * When passing true - re-enables all warnings.
76
     *
77
     * @api
78
     * @param bool|int $enable
79
     */
80 68
    public static function enable($enable = true)
81
    {
82 68
        if ($enable === true) {
83
            self::$enableWarnings = self::ALL;
84 68
        } elseif ($enable === false) {
85
            self::$enableWarnings = 0;
86
        } else {
87 68
            $enable = (int) $enable;
88
89 68
            self::$enableWarnings |= $enable;
90
        }
91 68
    }
92
93 5
    public static function warnOnce($errorMessage, $warningId, $messageLevel = null)
94
    {
95 5
        if (self::$warningHandler) {
96
            $fn = self::$warningHandler;
97
            $fn($errorMessage, $warningId);
98 5
        } elseif ((self::$enableWarnings & $warningId) > 0 && ! isset(self::$warned[$warningId])) {
99 1
            self::$warned[$warningId] = true;
100 1
            trigger_error($errorMessage, $messageLevel ?: E_USER_WARNING);
101
        }
102 5
    }
103
104
    public static function warn($errorMessage, $warningId, $messageLevel = null)
105
    {
106
        if (self::$warningHandler) {
107
            $fn = self::$warningHandler;
108
            $fn($errorMessage, $warningId);
109
        } elseif ((self::$enableWarnings & $warningId) > 0) {
110
            trigger_error($errorMessage, $messageLevel ?: E_USER_WARNING);
111
        }
112
    }
113
}
114