From ab6aae1a293e3d4ace6feeb95e7a64a1fe90d88a Mon Sep 17 00:00:00 2001 From: Tobias Weichart Date: Tue, 30 Jul 2019 12:31:40 +0100 Subject: [PATCH] [ErrorHandler] Added type declarations where possible --- .../ErrorHandler/BufferingLogger.php | 4 +- src/Symfony/Component/ErrorHandler/Debug.php | 5 +-- .../ErrorHandler/DebugClassLoader.php | 35 +++++++-------- .../Component/ErrorHandler/ErrorHandler.php | 44 +++++++------------ .../Exception/FatalErrorException.php | 2 +- .../Exception/SilencedErrorContext.php | 10 ++--- .../Fixtures/LoggerThatSetAnErrorHandler.php | 2 +- 7 files changed, 43 insertions(+), 59 deletions(-) diff --git a/src/Symfony/Component/ErrorHandler/BufferingLogger.php b/src/Symfony/Component/ErrorHandler/BufferingLogger.php index fef10d16e5a16..49c838f272cd0 100644 --- a/src/Symfony/Component/ErrorHandler/BufferingLogger.php +++ b/src/Symfony/Component/ErrorHandler/BufferingLogger.php @@ -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 = []; diff --git a/src/Symfony/Component/ErrorHandler/Debug.php b/src/Symfony/Component/ErrorHandler/Debug.php index e3bb55c0a0741..6f75dbf60e38a 100644 --- a/src/Symfony/Component/ErrorHandler/Debug.php +++ b/src/Symfony/Component/ErrorHandler/Debug.php @@ -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; diff --git a/src/Symfony/Component/ErrorHandler/DebugClassLoader.php b/src/Symfony/Component/ErrorHandler/DebugClassLoader.php index e1108a51bbc7a..501be3a604056 100644 --- a/src/Symfony/Component/ErrorHandler/DebugClassLoader.php +++ b/src/Symfony/Component/ErrorHandler/DebugClassLoader.php @@ -75,7 +75,7 @@ public function __construct(callable $classLoader) * * @return callable The wrapped class loader */ - public function getClassLoader() + public function getClassLoader(): callable { return $this->classLoader; } @@ -83,7 +83,7 @@ public function getClassLoader() /** * 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'); @@ -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; @@ -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; @@ -161,7 +156,7 @@ public function loadClass($class) } } else { ($this->classLoader)($class); - $file = false; + $file = ''; } } finally { error_reporting($e); @@ -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); @@ -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 = []; @@ -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)); @@ -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); @@ -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); @@ -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); diff --git a/src/Symfony/Component/ErrorHandler/ErrorHandler.php b/src/Symfony/Component/ErrorHandler/ErrorHandler.php index fa54ac90ac588..af3b6d85a67f0 100644 --- a/src/Symfony/Component/ErrorHandler/ErrorHandler.php +++ b/src/Symfony/Component/ErrorHandler/ErrorHandler.php @@ -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); @@ -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 = []; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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'); @@ -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)) { @@ -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); @@ -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; @@ -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(), @@ -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; diff --git a/src/Symfony/Component/ErrorHandler/Exception/FatalErrorException.php b/src/Symfony/Component/ErrorHandler/Exception/FatalErrorException.php index 4269356d5724d..ab6268a5c0412 100644 --- a/src/Symfony/Component/ErrorHandler/Exception/FatalErrorException.php +++ b/src/Symfony/Component/ErrorHandler/Exception/FatalErrorException.php @@ -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); diff --git a/src/Symfony/Component/ErrorHandler/Exception/SilencedErrorContext.php b/src/Symfony/Component/ErrorHandler/Exception/SilencedErrorContext.php index 2c4ae69db419d..18defc72ce1e8 100644 --- a/src/Symfony/Component/ErrorHandler/Exception/SilencedErrorContext.php +++ b/src/Symfony/Component/ErrorHandler/Exception/SilencedErrorContext.php @@ -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, diff --git a/src/Symfony/Component/ErrorHandler/Tests/Fixtures/LoggerThatSetAnErrorHandler.php b/src/Symfony/Component/ErrorHandler/Tests/Fixtures/LoggerThatSetAnErrorHandler.php index eaf41582a32f0..7938c30e00315 100644 --- a/src/Symfony/Component/ErrorHandler/Tests/Fixtures/LoggerThatSetAnErrorHandler.php +++ b/src/Symfony/Component/ErrorHandler/Tests/Fixtures/LoggerThatSetAnErrorHandler.php @@ -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);