Completed
Push — master ( 761755...65d5d0 )
by Ron
05:42
created

CoreErrorHandlers::printException()   C

Complexity

Conditions 12
Paths 4

Size

Total Lines 47
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 47
rs 5.1384
cc 12
eloc 33
nc 4
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Logger;
3
4
use ErrorException;
5
use Exception;
6
use Logger\Filters\LogLevelRangeFilter;
7
use Logger\Loggers\LoggerCollection;
8
use Psr\Log\LoggerInterface;
9
use Psr\Log\LogLevel;
10
11
class CoreErrorHandlers {
12
	/**
13
	 * @var array
14
	 */
15
	private static $phpErrorLevels = array(
16
		E_NOTICE => LogLevel::NOTICE,
17
		E_DEPRECATED => LogLevel::NOTICE,
18
		E_USER_DEPRECATED => LogLevel::NOTICE,
19
		E_WARNING => LogLevel::WARNING,
20
		E_STRICT => LogLevel::WARNING,
21
		E_USER_WARNING => LogLevel::WARNING,
22
		E_CORE_WARNING => LogLevel::WARNING,
23
		E_ERROR => LogLevel::ERROR,
24
		E_USER_ERROR => LogLevel::ERROR,
25
	);
26
27
	/**
28
	 * @param int|null $bitmask
29
	 */
30
	public static function enableExceptionsForErrors($bitmask = null) {
31
		set_error_handler(function ($level, $message, $file, $line) use ($bitmask) {
32
			// PHP-7 fix: What once was an E_STRICT is now an E_WARNING:
33
			if(preg_match('/^Declaration of .*? should be compatible with/', $message)) {
34
				$level = E_STRICT;
35
			}
36
			if (0 === error_reporting()) {
37
				return false;
38
			}
39
			if($bitmask & $level) {
40
				throw new ErrorException($message, 0, $level, $file, $line);
41
			}
42
		});
43
	}
44
45
	/**
46
	 * @param LoggerInterface $logger
47
	 * @param string $logLevel PSR-3 Log-Level
48
	 */
49
	public static function registerAssertionHandler(LoggerInterface $logger, $logLevel) {
50
		static $errorLogger = null;
51
		if($errorLogger === null) {
52
			$errorLogger = new LoggerCollection();
53
			assert_options(ASSERT_ACTIVE, true);
54
			assert_options(ASSERT_WARNING, false);
55
			assert_options(ASSERT_CALLBACK, function ($file, $line, $message) use ($errorLogger, $logLevel) {
56
				$errorLogger->log($logLevel, $message, array(
57
					'file' => $file,
58
					'line' => $line
59
				));
60
			});
61
		}
62
		$errorLogger->add($logger);
63
	}
64
65
	/**
66
	 * @param LoggerInterface $logger
67
	 */
68
	public static function registerFatalErrorHandler(LoggerInterface $logger) {
69
		static $errorLogger = null;
70
		if($errorLogger === null) {
71
			$errorLogger = new LoggerCollection();
72
			$errorLevels = self::$phpErrorLevels;
73
			register_shutdown_function(function () use ($errorLogger, $errorLevels) {
74
				$error = error_get_last();
75
				if($error['type'] === E_ERROR) {
76
					$errorLogger = new LogLevelRangeFilter($errorLogger, LogLevel::ERROR);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $errorLogger, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
77
					$errorLogger->log(LogLevel::ALERT, $error['message'], $error);
78
				}
79
			});
80
		}
81
		$errorLogger->add($logger);
82
	}
83
84
	/**
85
	 * @param LoggerInterface $logger
86
	 */
87
	public static function registerExceptionHandler(LoggerInterface $logger) {
88
		static $errorLogger = null;
89
		if($errorLogger === null) {
90
			$errorLogger = new LoggerCollection();
91
			set_exception_handler(function ($exception) use ($errorLogger) {
92
				/** @var \Exception|\Throwable $exception */
93
				$errorLogger = new LogLevelRangeFilter($errorLogger, LogLevel::ERROR);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $errorLogger, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
94
				$errorLogger->log(LogLevel::CRITICAL, $exception->getMessage(), array('exception' => $exception));
95
				if($exception instanceof Exception) {
96
					StackTracePrinter::printException($exception, "PHP Fatal Error: Uncaught exception: ");
97
					die(1);
98
				}
99
			});
100
		}
101
		$errorLogger->add($logger);
102
	}
103
}
104