diff --git a/src/Client/TrapHandle.php b/src/Client/TrapHandle.php index 8ee17295..003e8e4b 100644 --- a/src/Client/TrapHandle.php +++ b/src/Client/TrapHandle.php @@ -31,7 +31,13 @@ public static function fromArray(array $array): self public function if(bool|callable $condition): self { if (\is_callable($condition)) { - $condition = $condition(); + try { + $condition = (bool)$condition(); + } catch (\Throwable $e) { + $this->values[] = $e; + + return $this; + } } $this->haveToSend = $condition; @@ -106,6 +112,10 @@ private function sendDump(): void } // Dump sequence of values + /** + * @var string|int $key + * @var mixed $value + */ foreach ($this->values as $key => $value) { /** @psalm-suppress TooManyArguments */ VarDumper::dump($value, label: $key, depth: $this->depth); diff --git a/src/Client/TrapHandle/ContextProvider/Source.php b/src/Client/TrapHandle/ContextProvider/Source.php index 001fb68d..146413db 100644 --- a/src/Client/TrapHandle/ContextProvider/Source.php +++ b/src/Client/TrapHandle/ContextProvider/Source.php @@ -1,7 +1,5 @@ * @author Maxime Steinhausser * + * @link https://github.com/symfony/var-dumper/blob/7.0/Dumper/ContextProvider/SourceContextProvider.php + * @link https://github.com/symfony/var-dumper/blob/6.3/Dumper/ContextProvider/SourceContextProvider.php + * + * @psalm-suppress all + * * todo: rewrite and decompose */ final class Source implements ContextProviderInterface @@ -27,6 +30,9 @@ final class Source implements ContextProviderInterface private ?string $projectDir; private ?FileLinkFormatter $fileLinkFormatter; + /** + * @psalm-suppress UndefinedClass + */ public function __construct(string $charset = null, string $projectDir = null, FileLinkFormatter $fileLinkFormatter = null, int $limit = 9) { $this->charset = $charset; diff --git a/src/Client/TrapHandle/Counter.php b/src/Client/TrapHandle/Counter.php index 42920389..9c3f8644 100644 --- a/src/Client/TrapHandle/Counter.php +++ b/src/Client/TrapHandle/Counter.php @@ -6,18 +6,20 @@ final class Counter { - /** @var array> */ + /** @var array> */ private static array $counters = []; /** * Returns true if the counter of related stack trace is less than $times. In this case, the counter is incremented. + * + * @param int<0, max> $times */ public static function checkAndIncrement(string $key, int $times): bool { self::$counters[$key] ??= 0; if (self::$counters[$key] < $times) { - self::$counters[$key]++; + ++self::$counters[$key]; return true; } diff --git a/src/Client/TrapHandle/Dumper.php b/src/Client/TrapHandle/Dumper.php index 22e2ff08..678f2abd 100644 --- a/src/Client/TrapHandle/Dumper.php +++ b/src/Client/TrapHandle/Dumper.php @@ -22,33 +22,35 @@ /** * @internal * @psalm-internal Buggregator\Trap\Client - * @psalm-type DumperHandler = Closure(mixed $var, string|int|null $label, int $depth): void */ final class Dumper { - /** @var DumperHandler|null */ + /** @var null|Closure(mixed, string|null, int): mixed */ private static ?Closure $handler; - public static function dump(mixed $var, string|int|null $label = null, int $depth = 0) + public static function dump(mixed $var, string|int|null $label = null, int $depth = 0): mixed { - return (self::$handler ??= self::registerHandler())($var, $label, $depth); + return (self::$handler ??= self::registerHandler())($var, empty($label) ? null : (string)$label, $depth); } /** - * @return DumperHandler|null The previous handler if any + * @return null|callable(mixed, string|null, int): mixed + * @psalm-suppress MixedInferredReturnType, MixedPropertyTypeCoercion, MismatchingDocblockReturnType */ public static function setHandler(callable $callable = null): ?Closure { - return ([$callable, self::$handler] = [self::$handler, $callable(...)])[0]; + return ([$callable, self::$handler] = [self::$handler, $callable === null ? null : $callable(...)])[0]; } /** - * @return DumperHandler + * @return Closure(mixed, string|null, int): mixed + * * @author Nicolas Grekas */ private static function registerHandler(): Closure { $cloner = new VarCloner(); + /** @psalm-suppress InvalidArgument */ $cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO); $format = $_SERVER['VAR_DUMPER_FORMAT'] ?? null; @@ -73,19 +75,22 @@ private static function registerHandler(): Closure $dumper = new ContextualizedDumper($dumper, [new SourceContextProvider()]); } - return self::$handler = static function ($var, string|int|null $label = null, int $depth = 0) use ($cloner, $dumper): void { + return self::$handler = static function (mixed $var, string|null $label = null, int $depth = 0) + use ($cloner, $dumper): ?string { $var = $cloner->cloneVar($var); $label === null or $var = $var->withContext(['label' => $label]); $depth > 0 and $var = $var->withMaxDepth($depth); - $dumper->dump($var); + return $dumper->dump($var); }; } /** * @return array The context providers * @author Nicolas Grekas + * + * @psalm-suppress UndefinedClass */ private static function getContextProviders(): array { @@ -93,11 +98,15 @@ private static function getContextProviders(): array if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && \class_exists(Request::class)) { $requestStack = new RequestStack(); + /** @psalm-suppress MixedMethodCall */ $requestStack->push(Request::createFromGlobals()); $contextProviders['request'] = new RequestContextProvider($requestStack); } - $fileLinkFormatter = \class_exists(FileLinkFormatter::class) ? new FileLinkFormatter(null, $requestStack ?? null) : null; + /** @var null|FileLinkFormatter $fileLinkFormatter */ + $fileLinkFormatter = \class_exists(FileLinkFormatter::class) + ? new FileLinkFormatter(null, $requestStack ?? null) + : null; return $contextProviders + [ 'cli' => new CliContextProvider(), diff --git a/src/Client/TrapHandle/StackTrace.php b/src/Client/TrapHandle/StackTrace.php index 3d82a554..24e3d613 100644 --- a/src/Client/TrapHandle/StackTrace.php +++ b/src/Client/TrapHandle/StackTrace.php @@ -4,20 +4,18 @@ namespace Buggregator\Trap\Client\TrapHandle; -use Buggregator\Trap\Client\TrapHandle; - final class StackTrace { /** * @param string $baseDir Base directory for relative paths - * @return array, - * file?: non-empty-string, + * @return list * }> */ public static function stackTrace(string $baseDir = ''): array diff --git a/src/functions.php b/src/functions.php index 1850597e..713171b9 100644 --- a/src/functions.php +++ b/src/functions.php @@ -19,6 +19,7 @@ */ function trap(mixed ...$values): TrapHandle { + /** @psalm-suppress InternalMethod */ return TrapHandle::fromArray($values); } } catch (\Throwable $e) {