Skip to content

Php 7.4 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea/
vendor/
.php_cs.cache
.phpunit.result.cache
composer.lock
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ sudo: false
matrix:
fast_finish: true
include:
- php: 7.1
env: CS_CHECK=1 STATIC_ANALYSIS=1
- php: 7.2
- php: 7.3
env: CS_CHECK=1 STATIC_ANALYSIS=1
- php: 7.4
env: CODE_COVERAGE=1

cache:
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
}
],
"require": {
"php": "^7.1"
"php": "^7.3"
},
"require-dev": {
"phpstan/phpstan": "^0.11",
"phpstan/phpstan-phpunit": "^0.11",
"phpunit/phpunit": "^7.5",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-phpunit": "^0.12",
"phpunit/phpunit": "^8.5",
"roave/security-advisories": "dev-master",
"slam/php-cs-fixer-extensions": "^1.18",
"slam/php-cs-fixer-extensions": "^1.19",
"slam/php-debug-r": "^1.6",
"slam/phpstan-extensions": "^3.6",
"symfony/console": "^4.3",
"thecodingmachine/phpstan-strict-rules": "^0.11"
"slam/phpstan-extensions": "^4.0",
"symfony/console": "^5.0",
"thecodingmachine/phpstan-strict-rules": "^0.12"
},
"autoload": {
"psr-4": {
Expand Down
81 changes: 73 additions & 8 deletions lib/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,67 @@
namespace Slam\ErrorHandler;

use Doctrine\Common\Util\Debug as DoctrineDebug;
use ErrorException;
use Throwable;

final class ErrorHandler
{
/**
* @var bool
*/
private $autoExit = true;

/**
* @var null|bool
*/
private $cli;

/**
* @var null|int
*/
private $terminalWidth;

/**
* @var null|resource
*/
private $errorOutputStream;

/**
* @var bool
*/
private $hasColorSupport = false;

/**
* @var null|bool
*/
private $logErrors;

/**
* @var bool
*/
private $logVariables = true;

/**
* @var callable
*/
private $emailCallback;

/**
* @var array<int, bool>
*/
private $scream = [];

/**
* @var array<string, string>
*/
private static $colors = [
'<error>' => "\033[37;41m",
'</error>' => "\033[0m",
];

/**
* @var array<int, string>
*/
private static $errors = [
\E_COMPILE_ERROR => 'E_COMPILE_ERROR',
\E_COMPILE_WARNING => 'E_COMPILE_WARNING',
Expand Down Expand Up @@ -65,6 +108,7 @@ public function isCli(): bool
{
if (null === $this->cli) {
$this->setCli(\PHP_SAPI === 'cli');
\assert(null !== $this->cli);
}

return $this->cli;
Expand All @@ -85,11 +129,15 @@ public function getTerminalWidth(): int
}

$this->setTerminalWidth((int) $width ?: 80);
\assert(null !== $this->terminalWidth);
}

return $this->terminalWidth;
}

/**
* @param mixed $errorOutputStream
*/
public function setErrorOutputStream($errorOutputStream): void
{
if (! \is_resource($errorOutputStream)) {
Expand All @@ -100,10 +148,14 @@ public function setErrorOutputStream($errorOutputStream): void
$this->hasColorSupport = (\function_exists('posix_isatty') && @\posix_isatty($errorOutputStream));
}

/**
* @return resource
*/
public function getErrorOutputStream()
{
if (null === $this->errorOutputStream) {
$this->setErrorOutputStream(\STDERR);
\assert(null !== $this->errorOutputStream);
}

return $this->errorOutputStream;
Expand All @@ -118,6 +170,7 @@ public function logErrors(): bool
{
if (null === $this->logErrors) {
$this->setLogErrors(! \interface_exists(\PHPUnit\Framework\Test::class));
\assert(null !== $this->logErrors);
}

return $this->logErrors;
Expand All @@ -133,11 +186,17 @@ public function logVariables(): bool
return $this->logVariables;
}

/**
* @param array<int, bool> $scream
*/
public function setScreamSilencedErrors(array $scream): void
{
$this->scream = $scream;
}

/**
* @return array<int, bool>
*/
public function getScreamSilencedErrors(): array
{
return $this->scream;
Expand All @@ -149,17 +208,23 @@ public function register(): void
\set_exception_handler([$this, 'exceptionHandler']);
}

/**
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
*/
public function errorHandler($errno, $errstr = '', $errfile = '', $errline = 0): void
{
// Mandatory check for @ operator
if (0 === \error_reporting() && ! isset($this->scream[$errno])) {
return;
}

throw new \ErrorException($errstr, $errno, $errno, $errfile, $errline);
throw new ErrorException($errstr, $errno, $errno, $errfile, $errline);
}

public function exceptionHandler(\Throwable $exception): void
public function exceptionHandler(Throwable $exception): void
{
$this->logException($exception);
$this->emailException($exception);
Expand Down Expand Up @@ -256,7 +321,7 @@ private function outputError(string $text): void
\fwrite($this->getErrorOutputStream(), \str_replace(\array_keys(self::$colors), $this->hasColorSupport ? \array_values(self::$colors) : '', $text) . \PHP_EOL);
}

public function logException(\Throwable $exception): void
public function logException(Throwable $exception): void
{
if (! $this->logErrors()) {
return;
Expand All @@ -280,7 +345,7 @@ public function logException(\Throwable $exception): void
} while ($exception = $exception->getPrevious());
}

public function emailException(\Throwable $exception): void
public function emailException(Throwable $exception): void
{
if (! $this->logErrors()) {
return;
Expand Down Expand Up @@ -348,16 +413,16 @@ public function emailException(\Throwable $exception): void

try {
$callback($subject, $bodyText);
} catch (\Throwable $e) {
} catch (Throwable $e) {
$this->logException($e);
}
}

private function getExceptionCode(\Throwable $exception): string
private function getExceptionCode(Throwable $exception): string
{
$code = $exception->getCode();
if ($exception instanceof \ErrorException && isset(static::$errors[$code])) {
$code = static::$errors[$code];
if ($exception instanceof ErrorException && isset(self::$errors[$code])) {
$code = self::$errors[$code];
}

return (string) $code;
Expand Down
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
includes:
- vendor/phpstan/phpstan/conf/config.levelmax.neon
- phar://phpstan.phar/conf/config.levelmax.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/slam/phpstan-extensions/conf/slam-rules.neon
- vendor/slam/phpstan-extensions/conf/thecodingmachine-rules.neon
Expand All @@ -15,3 +15,4 @@ parameters:
- '#Variable \$_\w+ in isset\(\) always exists and is not nullable#'
- '#Parameter \#1 \$error_handler of function set_error_handler expects#'
- '#Offset .(no_exception_thrown.*|undefined_index). does not exist on array#'
- '#Expression .+arrayPerVerificaErrori.+ on a separate line does not do anything#'
Loading