Repository navigation
Expand file tree
/
Copy pathErrorHandler.php
More file actions
128 lines (109 loc) · 3.91 KB
/
Copy pathErrorHandler.php
File metadata and controls
128 lines (109 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace Codeception\Subscriber;
use Codeception\Event\SuiteEvent;
use Codeception\Events;
use Codeception\Lib\Notification;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ErrorHandler implements EventSubscriberInterface
{
use Shared\StaticEvents;
public static $events = [
Events::SUITE_BEFORE => 'handle'
];
/**
* @var bool $stopped to keep shutdownHandler from possible looping.
*/
private static $stopped = false;
private $deprecationsInstalled = false;
private $oldHandler;
/**
* @var int stores bitmask for errors
*/
private $errorLevel;
public function __construct()
{
$this->errorLevel = E_ALL & ~E_STRICT & ~E_DEPRECATED;
}
public function handle(SuiteEvent $e)
{
$settings = $e->getSettings();
if ($settings['error_level']) {
$this->errorLevel = eval("return {$settings['error_level']};");
}
error_reporting($this->errorLevel);
// We must register shutdown function before deprecation error handler to restore previous error handler
// and silence DeprecationErrorHandler yelling about 'THE ERROR HANDLER HAS CHANGED!'
register_shutdown_function([$this, 'shutdownHandler']);
$this->registerDeprecationErrorHandler();
$this->oldHandler = set_error_handler([$this, 'errorHandler']);
}
public function errorHandler($errno, $errstr, $errfile, $errline, $context)
{
if (E_USER_DEPRECATED === $errno) {
$this->handleDeprecationError($errno, $errstr, $errfile, $errline, $context);
return;
}
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return false;
}
if (strpos($errstr, 'Cannot modify header information') !== false) {
return false;
}
throw new \PHPUnit_Framework_Exception($errstr, $errno);
}
public function shutdownHandler()
{
if ($this->deprecationsInstalled) {
restore_error_handler();
}
if (self::$stopped) {
return;
}
self::$stopped = true;
$error = error_get_last();
if (!is_array($error)) {
return;
}
if (error_reporting() === 0) {
return;
}
// not fatal
if ($error['type'] > 1) {
return;
}
echo "\n\n\nFATAL ERROR. TESTS NOT FINISHED.\n";
echo sprintf("%s \nin %s:%d\n", $error['message'], $error['file'], $error['line']);
}
private function registerDeprecationErrorHandler()
{
if (class_exists('\Symfony\Bridge\PhpUnit\DeprecationErrorHandler')) {
// DeprecationErrorHandler only will be installed if array('PHPUnit_Util_ErrorHandler', 'handleError')
// is installed or no other error handlers are installed.
// So we will remove Symfony\Component\Debug\ErrorHandler if it's installed.
$old = set_error_handler('var_dump');
restore_error_handler();
if ($old
&& is_array($old)
&& count($old) > 0
&& is_object($old[0])
&& get_class($old[0]) === 'Symfony\Component\Debug\ErrorHandler'
) {
restore_error_handler();
}
$this->deprecationsInstalled = true;
\Symfony\Bridge\PhpUnit\DeprecationErrorHandler::register(getenv('SYMFONY_DEPRECATIONS_HELPER'));
}
}
private function handleDeprecationError($type, $message, $file, $line, $context)
{
if (!($this->errorLevel & $type)) {
return;
}
if ($this->deprecationsInstalled && $this->oldHandler) {
call_user_func($this->oldHandler, $type, $message, $file, $line, $context);
return;
}
Notification::deprecate("$message", "$file:$line");
}
}