Skip to content

Commit ab6aae1

Browse files
Tobias Weichartnicolas-grekas
Tobias Weichart
authored andcommitted
[ErrorHandler] Added type declarations where possible
1 parent b2dadc1 commit ab6aae1

File tree

7 files changed

+43
-59
lines changed

7 files changed

+43
-59
lines changed

src/Symfony/Component/ErrorHandler/BufferingLogger.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class BufferingLogger extends AbstractLogger
2222
{
2323
private $logs = [];
2424

25-
public function log($level, $message, array $context = [])
25+
public function log($level, $message, array $context = []): void
2626
{
2727
$this->logs[] = [$level, $message, $context];
2828
}
2929

30-
public function cleanLogs()
30+
public function cleanLogs(): array
3131
{
3232
$logs = $this->logs;
3333
$this->logs = [];

src/Symfony/Component/ErrorHandler/Debug.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ class Debug
2424
* Enables the debug tools.
2525
*
2626
* This method registers an error handler and an exception handler.
27-
*
28-
* @param int $errorReportingLevel The level of error reporting you want
29-
* @param bool $displayErrors Whether to display errors (for development) or just log them (for production)
3027
*/
31-
public static function enable($errorReportingLevel = E_ALL, $displayErrors = true)
28+
public static function enable(int $errorReportingLevel = E_ALL, bool $displayErrors = true): void
3229
{
3330
if (static::$enabled) {
3431
return;

src/Symfony/Component/ErrorHandler/DebugClassLoader.php

+16-19
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ public function __construct(callable $classLoader)
7575
*
7676
* @return callable The wrapped class loader
7777
*/
78-
public function getClassLoader()
78+
public function getClassLoader(): callable
7979
{
8080
return $this->classLoader;
8181
}
8282

8383
/**
8484
* Wraps all autoloaders.
8585
*/
86-
public static function enable()
86+
public static function enable(): void
8787
{
8888
// Ensures we don't hit https://bugs.php.net/42098
8989
class_exists('Symfony\Component\ErrorHandler\ErrorHandler');
@@ -109,7 +109,7 @@ class_exists('Psr\Log\LogLevel');
109109
/**
110110
* Disables the wrapping.
111111
*/
112-
public static function disable()
112+
public static function disable(): void
113113
{
114114
if (!\is_array($functions = spl_autoload_functions())) {
115115
return;
@@ -128,29 +128,24 @@ public static function disable()
128128
}
129129
}
130130

131-
/**
132-
* @return string|null
133-
*/
134-
public function findFile($class)
131+
public function findFile(string $class): ?string
135132
{
136-
return $this->isFinder ? $this->classLoader[0]->findFile($class) ?: null : null;
133+
return $this->isFinder ? ($this->classLoader[0]->findFile($class) ?: null) : null;
137134
}
138135

139136
/**
140137
* Loads the given class or interface.
141138
*
142-
* @param string $class The name of the class
143-
*
144139
* @throws \RuntimeException
145140
*/
146-
public function loadClass($class)
141+
public function loadClass(string $class): void
147142
{
148143
$e = error_reporting(error_reporting() | E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR);
149144

150145
try {
151146
if ($this->isFinder && !isset($this->loaded[$class])) {
152147
$this->loaded[$class] = true;
153-
if (!$file = $this->classLoader[0]->findFile($class) ?: false) {
148+
if (!$file = $this->classLoader[0]->findFile($class) ?: '') {
154149
// no-op
155150
} elseif (\function_exists('opcache_is_script_cached') && @opcache_is_script_cached($file)) {
156151
include $file;
@@ -161,7 +156,7 @@ public function loadClass($class)
161156
}
162157
} else {
163158
($this->classLoader)($class);
164-
$file = false;
159+
$file = '';
165160
}
166161
} finally {
167162
error_reporting($e);
@@ -170,7 +165,7 @@ public function loadClass($class)
170165
$this->checkClass($class, $file);
171166
}
172167

173-
private function checkClass($class, $file = null)
168+
private function checkClass(string $class, string $file = null): void
174169
{
175170
$exists = null === $file || class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false);
176171

@@ -218,7 +213,7 @@ private function checkClass($class, $file = null)
218213
}
219214
}
220215

221-
public function checkAnnotations(\ReflectionClass $refl, $class)
216+
public function checkAnnotations(\ReflectionClass $refl, string $class): array
222217
{
223218
$deprecations = [];
224219

@@ -395,7 +390,7 @@ public function checkAnnotations(\ReflectionClass $refl, $class)
395390
return $deprecations;
396391
}
397392

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

413408
if (!$tail) {
414-
return;
409+
return null;
415410
}
416411

417412
$tail = \DIRECTORY_SEPARATOR.implode(\DIRECTORY_SEPARATOR, $tail);
@@ -427,12 +422,14 @@ public function checkCase(\ReflectionClass $refl, $file, $class)
427422
) {
428423
return [substr($tail, -$tailLen + 1), substr($real, -$tailLen + 1), substr($real, 0, -$tailLen + 1)];
429424
}
425+
426+
return null;
430427
}
431428

432429
/**
433430
* `realpath` on MacOSX doesn't normalize the case of characters.
434431
*/
435-
private function darwinRealpath($real)
432+
private function darwinRealpath(string $real): string
436433
{
437434
$i = 1 + strrpos($real, '/');
438435
$file = substr($real, $i);
@@ -504,7 +501,7 @@ private function darwinRealpath($real)
504501
*
505502
* @return string[]
506503
*/
507-
private function getOwnInterfaces($class, $parent)
504+
private function getOwnInterfaces(string $class, $parent): array
508505
{
509506
$ownInterfaces = class_implements($class, false);
510507

src/Symfony/Component/ErrorHandler/ErrorHandler.php

+17-27
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,15 @@ class ErrorHandler
9898
private $bootstrappingLogger;
9999

100100
private static $reservedMemory;
101-
private static $toStringException = null;
101+
private static $toStringException;
102102
private static $silencedErrorCache = [];
103103
private static $silencedErrorCount = 0;
104104
private static $exitCode = 0;
105105

106106
/**
107107
* Registers the error handler.
108-
*
109-
* @param self|null $handler The handler to register
110-
* @param bool $replace Whether to replace or not any existing handler
111-
*
112-
* @return self The registered error handler
113108
*/
114-
public static function register(self $handler = null, $replace = true)
109+
public static function register(self $handler = null, bool $replace = true): self
115110
{
116111
if (null === self::$reservedMemory) {
117112
self::$reservedMemory = str_repeat('x', 10240);
@@ -175,7 +170,7 @@ public function __construct(BufferingLogger $bootstrappingLogger = null)
175170
* @param array|int $levels An array map of E_* to LogLevel::* or an integer bit field of E_* constants
176171
* @param bool $replace Whether to replace or not any existing logger
177172
*/
178-
public function setDefaultLogger(LoggerInterface $logger, $levels = E_ALL, $replace = false)
173+
public function setDefaultLogger(LoggerInterface $logger, $levels = E_ALL, bool $replace = false): void
179174
{
180175
$loggers = [];
181176

@@ -209,7 +204,7 @@ public function setDefaultLogger(LoggerInterface $logger, $levels = E_ALL, $repl
209204
*
210205
* @throws \InvalidArgumentException
211206
*/
212-
public function setLoggers(array $loggers)
207+
public function setLoggers(array $loggers): array
213208
{
214209
$prevLogged = $this->loggedErrors;
215210
$prev = $this->loggers;
@@ -256,11 +251,11 @@ public function setLoggers(array $loggers)
256251
/**
257252
* Sets a user exception handler.
258253
*
259-
* @param callable $handler A handler that will be called on Exception
254+
* @param callable|null $handler A handler that will be called on Exception
260255
*
261256
* @return callable|null The previous exception handler
262257
*/
263-
public function setExceptionHandler(callable $handler = null)
258+
public function setExceptionHandler(?callable $handler): ?callable
264259
{
265260
$prev = $this->exceptionHandler;
266261
$this->exceptionHandler = $handler;
@@ -276,7 +271,7 @@ public function setExceptionHandler(callable $handler = null)
276271
*
277272
* @return int The previous value
278273
*/
279-
public function throwAt($levels, $replace = false)
274+
public function throwAt(int $levels, bool $replace = false): int
280275
{
281276
$prev = $this->thrownErrors;
282277
$this->thrownErrors = ($levels | E_RECOVERABLE_ERROR | E_USER_ERROR) & ~E_USER_DEPRECATED & ~E_DEPRECATED;
@@ -296,7 +291,7 @@ public function throwAt($levels, $replace = false)
296291
*
297292
* @return int The previous value
298293
*/
299-
public function scopeAt($levels, $replace = false)
294+
public function scopeAt(int $levels, bool $replace = false): int
300295
{
301296
$prev = $this->scopedErrors;
302297
$this->scopedErrors = (int) $levels;
@@ -315,7 +310,7 @@ public function scopeAt($levels, $replace = false)
315310
*
316311
* @return int The previous value
317312
*/
318-
public function traceAt($levels, $replace = false)
313+
public function traceAt(int $levels, bool $replace = false): int
319314
{
320315
$prev = $this->tracedErrors;
321316
$this->tracedErrors = (int) $levels;
@@ -334,7 +329,7 @@ public function traceAt($levels, $replace = false)
334329
*
335330
* @return int The previous value
336331
*/
337-
public function screamAt($levels, $replace = false)
332+
public function screamAt(int $levels, bool $replace = false): int
338333
{
339334
$prev = $this->screamedErrors;
340335
$this->screamedErrors = (int) $levels;
@@ -348,7 +343,7 @@ public function screamAt($levels, $replace = false)
348343
/**
349344
* Re-registers as a PHP error handler if levels changed.
350345
*/
351-
private function reRegister($prev)
346+
private function reRegister(int $prev): void
352347
{
353348
if ($prev !== $this->thrownErrors | $this->loggedErrors) {
354349
$handler = set_error_handler('var_dump');
@@ -368,18 +363,13 @@ private function reRegister($prev)
368363
/**
369364
* Handles errors by filtering then logging them according to the configured bit fields.
370365
*
371-
* @param int $type One of the E_* constants
372-
* @param string $message
373-
* @param string $file
374-
* @param int $line
375-
*
376366
* @return bool Returns false when no handling happens so that the PHP engine can handle the error itself
377367
*
378368
* @throws \ErrorException When $this->thrownErrors requests so
379369
*
380370
* @internal
381371
*/
382-
public function handleError($type, $message, $file, $line)
372+
public function handleError(int $type, string $message, string $file, int $line): bool
383373
{
384374
// @deprecated to be removed in Symfony 5.0
385375
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)
442432
self::$silencedErrorCache[$id][$message] = $errorAsException;
443433
}
444434
if (null === $lightTrace) {
445-
return;
435+
return true;
446436
}
447437
} else {
448438
$errorAsException = new \ErrorException($logMessage, 0, $type, $file, $line);
@@ -590,11 +580,11 @@ public function handleException($exception, array $error = null)
590580
/**
591581
* Shutdown registered function for handling PHP fatal errors.
592582
*
593-
* @param array $error An array as returned by error_get_last()
583+
* @param array|null $error An array as returned by error_get_last()
594584
*
595585
* @internal
596586
*/
597-
public static function handleFatalError(array $error = null)
587+
public static function handleFatalError(array $error = null): void
598588
{
599589
if (null === self::$reservedMemory) {
600590
return;
@@ -674,7 +664,7 @@ public static function handleFatalError(array $error = null)
674664
*
675665
* @return FatalErrorHandlerInterface[] An array of FatalErrorHandlerInterface
676666
*/
677-
protected function getFatalErrorHandlers()
667+
protected function getFatalErrorHandlers(): array
678668
{
679669
return [
680670
new UndefinedFunctionFatalErrorHandler(),
@@ -686,7 +676,7 @@ protected function getFatalErrorHandlers()
686676
/**
687677
* Cleans the trace by removing function arguments and the frames added by the error handler and DebugClassLoader.
688678
*/
689-
private function cleanTrace($backtrace, $type, $file, $line, $throw)
679+
private function cleanTrace(array $backtrace, int $type, string $file, int $line, bool $throw): array
690680
{
691681
$lightTrace = $backtrace;
692682

src/Symfony/Component/ErrorHandler/Exception/FatalErrorException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function __construct(string $message, int $code, int $severity, string $f
6868
}
6969
}
7070

71-
protected function setTrace($trace)
71+
protected function setTrace(array $trace): void
7272
{
7373
$traceReflector = new \ReflectionProperty('Exception', 'trace');
7474
$traceReflector->setAccessible(true);

src/Symfony/Component/ErrorHandler/Exception/SilencedErrorContext.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,27 @@ public function __construct(int $severity, string $file, int $line, array $trace
3434
$this->count = $count;
3535
}
3636

37-
public function getSeverity()
37+
public function getSeverity(): int
3838
{
3939
return $this->severity;
4040
}
4141

42-
public function getFile()
42+
public function getFile(): string
4343
{
4444
return $this->file;
4545
}
4646

47-
public function getLine()
47+
public function getLine(): int
4848
{
4949
return $this->line;
5050
}
5151

52-
public function getTrace()
52+
public function getTrace(): array
5353
{
5454
return $this->trace;
5555
}
5656

57-
public function JsonSerialize()
57+
public function jsonSerialize(): array
5858
{
5959
return [
6060
'severity' => $this->severity,

src/Symfony/Component/ErrorHandler/Tests/Fixtures/LoggerThatSetAnErrorHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class LoggerThatSetAnErrorHandler extends BufferingLogger
88
{
9-
public function log($level, $message, array $context = [])
9+
public function log($level, $message, array $context = []): void
1010
{
1111
set_error_handler('is_string');
1212
parent::log($level, $message, $context);

0 commit comments

Comments
 (0)