Skip to content

[ErrorHandler] Added type declarations where possible #32812

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 1 commit into from
Jul 31, 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
4 changes: 2 additions & 2 deletions src/Symfony/Component/ErrorHandler/BufferingLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ class BufferingLogger extends AbstractLogger
{
private $logs = [];

public function log($level, $message, array $context = [])
public function log($level, $message, array $context = []): void
{
$this->logs[] = [$level, $message, $context];
}

public function cleanLogs()
public function cleanLogs(): array
{
$logs = $this->logs;
$this->logs = [];
Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/Component/ErrorHandler/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ class Debug
* Enables the debug tools.
*
* This method registers an error handler and an exception handler.
*
* @param int $errorReportingLevel The level of error reporting you want
* @param bool $displayErrors Whether to display errors (for development) or just log them (for production)
*/
public static function enable($errorReportingLevel = E_ALL, $displayErrors = true)
public static function enable(int $errorReportingLevel = E_ALL, bool $displayErrors = true): void
{
if (static::$enabled) {
return;
Expand Down
35 changes: 16 additions & 19 deletions src/Symfony/Component/ErrorHandler/DebugClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ public function __construct(callable $classLoader)
*
* @return callable The wrapped class loader
*/
public function getClassLoader()
public function getClassLoader(): callable
{
return $this->classLoader;
}

/**
* Wraps all autoloaders.
*/
public static function enable()
public static function enable(): void
{
// Ensures we don't hit https://bugs.php.net/42098
class_exists('Symfony\Component\ErrorHandler\ErrorHandler');
Expand All @@ -109,7 +109,7 @@ class_exists('Psr\Log\LogLevel');
/**
* Disables the wrapping.
*/
public static function disable()
public static function disable(): void
{
if (!\is_array($functions = spl_autoload_functions())) {
return;
Expand All @@ -128,29 +128,24 @@ public static function disable()
}
}

/**
* @return string|null
*/
public function findFile($class)
public function findFile(string $class): ?string
{
return $this->isFinder ? $this->classLoader[0]->findFile($class) ?: null : null;
return $this->isFinder ? ($this->classLoader[0]->findFile($class) ?: null) : null;
}

/**
* Loads the given class or interface.
*
* @param string $class The name of the class
*
* @throws \RuntimeException
*/
public function loadClass($class)
public function loadClass(string $class): void
{
$e = error_reporting(error_reporting() | E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR);

try {
if ($this->isFinder && !isset($this->loaded[$class])) {
$this->loaded[$class] = true;
if (!$file = $this->classLoader[0]->findFile($class) ?: false) {
if (!$file = $this->classLoader[0]->findFile($class) ?: '') {
// no-op
} elseif (\function_exists('opcache_is_script_cached') && @opcache_is_script_cached($file)) {
include $file;
Expand All @@ -161,7 +156,7 @@ public function loadClass($class)
}
} else {
($this->classLoader)($class);
$file = false;
$file = '';
}
} finally {
error_reporting($e);
Expand All @@ -170,7 +165,7 @@ public function loadClass($class)
$this->checkClass($class, $file);
}

private function checkClass($class, $file = null)
private function checkClass(string $class, string $file = null): void
{
$exists = null === $file || class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false);

Expand Down Expand Up @@ -218,7 +213,7 @@ private function checkClass($class, $file = null)
}
}

public function checkAnnotations(\ReflectionClass $refl, $class)
public function checkAnnotations(\ReflectionClass $refl, string $class): array
{
$deprecations = [];

Expand Down Expand Up @@ -395,7 +390,7 @@ public function checkAnnotations(\ReflectionClass $refl, $class)
return $deprecations;
}

public function checkCase(\ReflectionClass $refl, $file, $class)
public function checkCase(\ReflectionClass $refl, string $file, string $class): ?array
{
$real = explode('\\', $class.strrchr($file, '.'));
$tail = explode(\DIRECTORY_SEPARATOR, str_replace('/', \DIRECTORY_SEPARATOR, $file));
Expand All @@ -411,7 +406,7 @@ public function checkCase(\ReflectionClass $refl, $file, $class)
array_splice($tail, 0, $i + 1);

if (!$tail) {
return;
return null;
}

$tail = \DIRECTORY_SEPARATOR.implode(\DIRECTORY_SEPARATOR, $tail);
Expand All @@ -427,12 +422,14 @@ public function checkCase(\ReflectionClass $refl, $file, $class)
) {
return [substr($tail, -$tailLen + 1), substr($real, -$tailLen + 1), substr($real, 0, -$tailLen + 1)];
}

return null;
}

/**
* `realpath` on MacOSX doesn't normalize the case of characters.
*/
private function darwinRealpath($real)
private function darwinRealpath(string $real): string
{
$i = 1 + strrpos($real, '/');
$file = substr($real, $i);
Expand Down Expand Up @@ -504,7 +501,7 @@ private function darwinRealpath($real)
*
* @return string[]
*/
private function getOwnInterfaces($class, $parent)
private function getOwnInterfaces(string $class, $parent): array
{
$ownInterfaces = class_implements($class, false);

Expand Down
44 changes: 17 additions & 27 deletions src/Symfony/Component/ErrorHandler/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,15 @@ class ErrorHandler
private $bootstrappingLogger;

private static $reservedMemory;
private static $toStringException = null;
private static $toStringException;
private static $silencedErrorCache = [];
private static $silencedErrorCount = 0;
private static $exitCode = 0;

/**
* Registers the error handler.
*
* @param self|null $handler The handler to register
* @param bool $replace Whether to replace or not any existing handler
*
* @return self The registered error handler
*/
public static function register(self $handler = null, $replace = true)
public static function register(self $handler = null, bool $replace = true): self
{
if (null === self::$reservedMemory) {
self::$reservedMemory = str_repeat('x', 10240);
Expand Down Expand Up @@ -175,7 +170,7 @@ public function __construct(BufferingLogger $bootstrappingLogger = null)
* @param array|int $levels An array map of E_* to LogLevel::* or an integer bit field of E_* constants
* @param bool $replace Whether to replace or not any existing logger
*/
public function setDefaultLogger(LoggerInterface $logger, $levels = E_ALL, $replace = false)
public function setDefaultLogger(LoggerInterface $logger, $levels = E_ALL, bool $replace = false): void
{
$loggers = [];

Expand Down Expand Up @@ -209,7 +204,7 @@ public function setDefaultLogger(LoggerInterface $logger, $levels = E_ALL, $repl
*
* @throws \InvalidArgumentException
*/
public function setLoggers(array $loggers)
public function setLoggers(array $loggers): array
{
$prevLogged = $this->loggedErrors;
$prev = $this->loggers;
Expand Down Expand Up @@ -256,11 +251,11 @@ public function setLoggers(array $loggers)
/**
* Sets a user exception handler.
*
* @param callable $handler A handler that will be called on Exception
* @param callable|null $handler A handler that will be called on Exception
*
* @return callable|null The previous exception handler
*/
public function setExceptionHandler(callable $handler = null)
public function setExceptionHandler(?callable $handler): ?callable
{
$prev = $this->exceptionHandler;
$this->exceptionHandler = $handler;
Expand All @@ -276,7 +271,7 @@ public function setExceptionHandler(callable $handler = null)
*
* @return int The previous value
*/
public function throwAt($levels, $replace = false)
public function throwAt(int $levels, bool $replace = false): int
{
$prev = $this->thrownErrors;
$this->thrownErrors = ($levels | E_RECOVERABLE_ERROR | E_USER_ERROR) & ~E_USER_DEPRECATED & ~E_DEPRECATED;
Expand All @@ -296,7 +291,7 @@ public function throwAt($levels, $replace = false)
*
* @return int The previous value
*/
public function scopeAt($levels, $replace = false)
public function scopeAt(int $levels, bool $replace = false): int
{
$prev = $this->scopedErrors;
$this->scopedErrors = (int) $levels;
Expand All @@ -315,7 +310,7 @@ public function scopeAt($levels, $replace = false)
*
* @return int The previous value
*/
public function traceAt($levels, $replace = false)
public function traceAt(int $levels, bool $replace = false): int
{
$prev = $this->tracedErrors;
$this->tracedErrors = (int) $levels;
Expand All @@ -334,7 +329,7 @@ public function traceAt($levels, $replace = false)
*
* @return int The previous value
*/
public function screamAt($levels, $replace = false)
public function screamAt(int $levels, bool $replace = false): int
{
$prev = $this->screamedErrors;
$this->screamedErrors = (int) $levels;
Expand All @@ -348,7 +343,7 @@ public function screamAt($levels, $replace = false)
/**
* Re-registers as a PHP error handler if levels changed.
*/
private function reRegister($prev)
private function reRegister(int $prev): void
{
if ($prev !== $this->thrownErrors | $this->loggedErrors) {
$handler = set_error_handler('var_dump');
Expand All @@ -368,18 +363,13 @@ private function reRegister($prev)
/**
* Handles errors by filtering then logging them according to the configured bit fields.
*
* @param int $type One of the E_* constants
* @param string $message
* @param string $file
* @param int $line
*
* @return bool Returns false when no handling happens so that the PHP engine can handle the error itself
*
* @throws \ErrorException When $this->thrownErrors requests so
*
* @internal
*/
public function handleError($type, $message, $file, $line)
public function handleError(int $type, string $message, string $file, int $line): bool
{
// @deprecated to be removed in Symfony 5.0
if (\PHP_VERSION_ID >= 70300 && $message && '"' === $message[0] && 0 === strpos($message, '"continue') && preg_match('/^"continue(?: \d++)?" targeting switch is equivalent to "break(?: \d++)?"\. Did you mean to use "continue(?: \d++)?"\?$/', $message)) {
Expand Down Expand Up @@ -442,7 +432,7 @@ public function handleError($type, $message, $file, $line)
self::$silencedErrorCache[$id][$message] = $errorAsException;
}
if (null === $lightTrace) {
return;
return true;
}
} else {
$errorAsException = new \ErrorException($logMessage, 0, $type, $file, $line);
Expand Down Expand Up @@ -590,11 +580,11 @@ public function handleException($exception, array $error = null)
/**
* Shutdown registered function for handling PHP fatal errors.
*
* @param array $error An array as returned by error_get_last()
* @param array|null $error An array as returned by error_get_last()
*
* @internal
*/
public static function handleFatalError(array $error = null)
public static function handleFatalError(array $error = null): void
{
if (null === self::$reservedMemory) {
return;
Expand Down Expand Up @@ -674,7 +664,7 @@ public static function handleFatalError(array $error = null)
*
* @return FatalErrorHandlerInterface[] An array of FatalErrorHandlerInterface
*/
protected function getFatalErrorHandlers()
protected function getFatalErrorHandlers(): array
{
return [
new UndefinedFunctionFatalErrorHandler(),
Expand All @@ -686,7 +676,7 @@ protected function getFatalErrorHandlers()
/**
* Cleans the trace by removing function arguments and the frames added by the error handler and DebugClassLoader.
*/
private function cleanTrace($backtrace, $type, $file, $line, $throw)
private function cleanTrace(array $backtrace, int $type, string $file, int $line, bool $throw): array
{
$lightTrace = $backtrace;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct(string $message, int $code, int $severity, string $f
}
}

protected function setTrace($trace)
protected function setTrace(array $trace): void
{
$traceReflector = new \ReflectionProperty('Exception', 'trace');
$traceReflector->setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,27 @@ public function __construct(int $severity, string $file, int $line, array $trace
$this->count = $count;
}

public function getSeverity()
public function getSeverity(): int
{
return $this->severity;
}

public function getFile()
public function getFile(): string
{
return $this->file;
}

public function getLine()
public function getLine(): int
{
return $this->line;
}

public function getTrace()
public function getTrace(): array
{
return $this->trace;
}

public function JsonSerialize()
public function jsonSerialize(): array
{
return [
'severity' => $this->severity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class LoggerThatSetAnErrorHandler extends BufferingLogger
{
public function log($level, $message, array $context = [])
public function log($level, $message, array $context = []): void
{
set_error_handler('is_string');
parent::log($level, $message, $context);
Expand Down