Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\Util;
11
 
12
use const E_DEPRECATED;
13
use const E_NOTICE;
14
use const E_STRICT;
15
use const E_USER_DEPRECATED;
16
use const E_USER_NOTICE;
17
use const E_USER_WARNING;
18
use const E_WARNING;
19
use function error_reporting;
20
use function restore_error_handler;
21
use function set_error_handler;
22
use PHPUnit\Framework\Error\Deprecated;
23
use PHPUnit\Framework\Error\Error;
24
use PHPUnit\Framework\Error\Notice;
25
use PHPUnit\Framework\Error\Warning;
26
 
27
/**
28
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
29
 */
30
final class ErrorHandler
31
{
32
    /**
33
     * @var bool
34
     */
35
    private $convertDeprecationsToExceptions;
36
 
37
    /**
38
     * @var bool
39
     */
40
    private $convertErrorsToExceptions;
41
 
42
    /**
43
     * @var bool
44
     */
45
    private $convertNoticesToExceptions;
46
 
47
    /**
48
     * @var bool
49
     */
50
    private $convertWarningsToExceptions;
51
 
52
    /**
53
     * @var bool
54
     */
55
    private $registered = false;
56
 
57
    public static function invokeIgnoringWarnings(callable $callable)
58
    {
59
        set_error_handler(
60
            static function ($errorNumber, $errorString)
61
            {
62
                if ($errorNumber === E_WARNING) {
63
                    return;
64
                }
65
 
66
                return false;
67
            }
68
        );
69
 
70
        $result = $callable();
71
 
72
        restore_error_handler();
73
 
74
        return $result;
75
    }
76
 
77
    public function __construct(bool $convertDeprecationsToExceptions, bool $convertErrorsToExceptions, bool $convertNoticesToExceptions, bool $convertWarningsToExceptions)
78
    {
79
        $this->convertDeprecationsToExceptions = $convertDeprecationsToExceptions;
80
        $this->convertErrorsToExceptions       = $convertErrorsToExceptions;
81
        $this->convertNoticesToExceptions      = $convertNoticesToExceptions;
82
        $this->convertWarningsToExceptions     = $convertWarningsToExceptions;
83
    }
84
 
85
    public function __invoke(int $errorNumber, string $errorString, string $errorFile, int $errorLine): bool
86
    {
87
        /*
88
         * Do not raise an exception when the error suppression operator (@) was used.
89
         *
90
         * @see https://github.com/sebastianbergmann/phpunit/issues/3739
91
         */
92
        if (!($errorNumber & error_reporting())) {
93
            return false;
94
        }
95
 
96
        switch ($errorNumber) {
97
            case E_NOTICE:
98
            case E_USER_NOTICE:
99
            case E_STRICT:
100
                if (!$this->convertNoticesToExceptions) {
101
                    return false;
102
                }
103
 
104
                throw new Notice($errorString, $errorNumber, $errorFile, $errorLine);
105
 
106
            case E_WARNING:
107
            case E_USER_WARNING:
108
                if (!$this->convertWarningsToExceptions) {
109
                    return false;
110
                }
111
 
112
                throw new Warning($errorString, $errorNumber, $errorFile, $errorLine);
113
 
114
            case E_DEPRECATED:
115
            case E_USER_DEPRECATED:
116
                if (!$this->convertDeprecationsToExceptions) {
117
                    return false;
118
                }
119
 
120
                throw new Deprecated($errorString, $errorNumber, $errorFile, $errorLine);
121
 
122
            default:
123
                if (!$this->convertErrorsToExceptions) {
124
                    return false;
125
                }
126
 
127
                throw new Error($errorString, $errorNumber, $errorFile, $errorLine);
128
        }
129
    }
130
 
131
    public function register(): void
132
    {
133
        if ($this->registered) {
134
            return;
135
        }
136
 
137
        $oldErrorHandler = set_error_handler($this);
138
 
139
        if ($oldErrorHandler !== null) {
140
            restore_error_handler();
141
 
142
            return;
143
        }
144
 
145
        $this->registered = true;
146
    }
147
 
148
    public function unregister(): void
149
    {
150
        if (!$this->registered) {
151
            return;
152
        }
153
 
154
        restore_error_handler();
155
    }
156
}