From d5e167519f7f887a4224c9a9ed3ebcb14542ee69 Mon Sep 17 00:00:00 2001 From: Plakhotnikov Vladimir Date: Sat, 29 Jun 2024 21:10:57 +0300 Subject: [PATCH 1/3] style: fix Psalm issues --- psalm-baseline.xml | 38 +------------------ src/Client/Caster/ProtobufCaster.php | 6 ++- src/Sender/Console/Renderer/Profiler.php | 6 +-- .../Console/Renderer/Sentry/Exceptions.php | 11 +++--- src/Sender/Console/Renderer/VarDumper.php | 16 +++++--- src/Sender/Console/Support/Tables.php | 9 +++-- src/Support/Stream/Base64DecodeFilter.php | 2 +- src/Traffic/Message/Multipart/Part.php | 5 ++- src/Traffic/Parser/Http.php | 5 ++- 9 files changed, 39 insertions(+), 59 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 438f66cf..d619aa13 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -92,11 +92,6 @@ - - - - - getCookieParams()]]> @@ -113,11 +108,8 @@ - - - $output->writeln(]]> - + @@ -129,11 +121,6 @@ - - - - - @@ -161,39 +148,19 @@ - - - - - - - - - - - - - - - - - - - - @@ -218,8 +185,5 @@ getName()]]]> - - - diff --git a/src/Client/Caster/ProtobufCaster.php b/src/Client/Caster/ProtobufCaster.php index 05dd8f4b..58393b14 100644 --- a/src/Client/Caster/ProtobufCaster.php +++ b/src/Client/Caster/ProtobufCaster.php @@ -55,8 +55,12 @@ final class ProtobufCaster public static function cast(Message $c, array $a, Stub $stub, bool $isNested): array { + /** + * @var DescriptorPool $pool + */ + $pool = DescriptorPool::getGeneratedPool(); /** @var PublicDescriptor|InternalDescriptor $descriptor */ - $descriptor = DescriptorPool::getGeneratedPool()->getDescriptorByClassName($c::class); + $descriptor = $pool->getDescriptorByClassName($c::class); return self::castMessage($c, $descriptor); } diff --git a/src/Sender/Console/Renderer/Profiler.php b/src/Sender/Console/Renderer/Profiler.php index 7d7cdaf9..866cdc2b 100644 --- a/src/Sender/Console/Renderer/Profiler.php +++ b/src/Sender/Console/Renderer/Profiler.php @@ -61,9 +61,9 @@ public function render(OutputInterface $output, Frame $frame): void Tables::renderKeyValueTable($output, 'Peak values', [ 'Memory usage' => Measure::memory($peaks->mu), 'Peak memory usage' => Measure::memory($peaks->pmu), - 'Wall time' => $peaks->wt, - 'CPU time' => $peaks->cpu, - 'Calls count' => $peaks->ct, + 'Wall time' => (string) $peaks->wt, + 'CPU time' => (string) $peaks->cpu, + 'Calls count' => (string) $peaks->ct, ]); } } diff --git a/src/Sender/Console/Renderer/Sentry/Exceptions.php b/src/Sender/Console/Renderer/Sentry/Exceptions.php index d8fffa7c..138acfcb 100644 --- a/src/Sender/Console/Renderer/Sentry/Exceptions.php +++ b/src/Sender/Console/Renderer/Sentry/Exceptions.php @@ -64,7 +64,8 @@ private static function renderTrace(OutputInterface $output, array $frames, bool if ($frames === []) { return; } - $getValue = static fn(array $frame, string $key, ?string $default = ''): string|int|float|bool|null => + + $getValue = static fn(array $frame, string $key, string $default = ''): string|int|float|bool => isset($frame[$key]) && \is_scalar($frame[$key]) ? $frame[$key] : $default; $i = \count($frames) ; @@ -80,7 +81,7 @@ private static function renderTrace(OutputInterface $output, array $frames, bool } $file = $getValue($frame, 'filename'); - $line = $getValue($frame, 'lineno', null); + $line = $getValue($frame, 'lineno'); $class = $getValue($frame, 'class'); /** @psalm-suppress RiskyTruthyFalsyComparison */ $class = empty($class) ? '' : $class . '::'; @@ -90,11 +91,11 @@ private static function renderTrace(OutputInterface $output, array $frames, bool \sprintf( "%s%s%s\n%s%s%s()", \str_pad("#$i", $numPad, ' '), - $file, - !$line ? '' : ":$line", + (string) $file, + $line !== '' ? ":$line": '', \str_repeat(' ', $numPad), $class, - $function, + (string) $function, ), ); diff --git a/src/Sender/Console/Renderer/VarDumper.php b/src/Sender/Console/Renderer/VarDumper.php index 88c80700..7bc3f390 100644 --- a/src/Sender/Console/Renderer/VarDumper.php +++ b/src/Sender/Console/Renderer/VarDumper.php @@ -23,6 +23,8 @@ */ final class VarDumper implements Renderer { + private static ?DumpDescriptorInterface $describer = null; + public function isSupport(Frame $frame): bool { return $frame->type === ProtoType::VarDumper; @@ -32,6 +34,9 @@ public function render(OutputInterface $output, Frame $frame): void { \assert($frame instanceof Frame\VarDumper); + /** + * @var list{Data, array}|false $payload + */ $payload = @\unserialize(\base64_decode($frame->dump), ['allowed_classes' => [Data::class, Stub::class]]); // Impossible to decode the message, give up. @@ -39,19 +44,18 @@ public function render(OutputInterface $output, Frame $frame): void throw new \RuntimeException("Unable to decode a message."); } - static $describer = null; - $describer ??= $this->getDescriber(); + self::$describer ??= $this->getDescriber(); [$data, $context] = $payload; - $describer->describe(new SymfonyStyle(new ArrayInput([]), $output), $data, $context, 0); + self::$describer->describe(new SymfonyStyle(new ArrayInput([]), $output), $data, $context, 0); } private function getDescriber(): DumpDescriptorInterface { return new class() implements DumpDescriptorInterface { public function __construct( - private CliDumper $dumper = new CliDumper(), + private readonly CliDumper $dumper = new CliDumper(), ) {} /** @@ -72,8 +76,8 @@ public function describe(OutputInterface $output, Data $data, array $context, in \assert(\is_array($source)); $sourceInfo = \sprintf('%s:%d', $source['name'], $source['line']); - if ($fileLink = $source['file_link'] ?? null) { - $sourceInfo = \sprintf('%s', $fileLink, $sourceInfo); + if (isset($source['file_link'])) { + $sourceInfo = \sprintf('%s', $source['file_link'], $sourceInfo); $meta['Source'] = $sourceInfo; } diff --git a/src/Sender/Console/Support/Tables.php b/src/Sender/Console/Support/Tables.php index ae0ad2af..959c4cd0 100644 --- a/src/Sender/Console/Support/Tables.php +++ b/src/Sender/Console/Support/Tables.php @@ -15,6 +15,9 @@ */ final class Tables { + /** + * @param array $data + */ public static function renderKeyValueTable(OutputInterface $output, string $title, array $data): void { $table = (new Table($output))->setHeaderTitle($title); @@ -27,10 +30,10 @@ public static function renderKeyValueTable(OutputInterface $output, string $titl $valueLength = \max(1, (new Terminal())->getWidth() - 7 - $keyLength); $table->setRows([...(static function (array $data) use ($valueLength): iterable { + /** + * @var array $data + */ foreach ($data as $key => $value) { - if (!\is_string($value)) { - $value = Json::encode($value); - } $values = \strlen($value) > $valueLength ? \str_split($value, $valueLength) : [$value]; diff --git a/src/Support/Stream/Base64DecodeFilter.php b/src/Support/Stream/Base64DecodeFilter.php index 3d8cd3ec..fa5c4adc 100644 --- a/src/Support/Stream/Base64DecodeFilter.php +++ b/src/Support/Stream/Base64DecodeFilter.php @@ -52,7 +52,7 @@ public function filter($in, $out, &$consumed, bool $closing): int } // Decode part of the data - $bucket->data = \base64_decode($this->buffer . \substr($bucket->data, 0, -$d)); + $bucket->data = \base64_decode($this->buffer . \substr($bucket->data, 0, -$d), true); $consumed += $bucket->datalen; $this->buffer = \substr($bucket->data, -$d); diff --git a/src/Traffic/Message/Multipart/Part.php b/src/Traffic/Message/Multipart/Part.php index 1a6c64f8..e3ebf0d9 100644 --- a/src/Traffic/Message/Multipart/Part.php +++ b/src/Traffic/Message/Multipart/Part.php @@ -28,10 +28,11 @@ protected function __construct( */ public static function create(array $headers): Part { - $contentDisposition = self::findHeader($headers, 'Content-Disposition')[0] ?? null; + $contentDisposition = self::findHeader($headers, 'Content-Disposition')[0] ?? ''; $name = $fileName = null; - if ((string) $contentDisposition !== '') { + + if ($contentDisposition !== '') { // Get field name and file name $name = \preg_match('/\bname=(?:(?[^" ;,]++)|"(?[^"]++)")/', $contentDisposition, $matches) === 1 ? ($matches['a'] ?: $matches['b']) diff --git a/src/Traffic/Parser/Http.php b/src/Traffic/Parser/Http.php index 3441895e..815223a9 100644 --- a/src/Traffic/Parser/Http.php +++ b/src/Traffic/Parser/Http.php @@ -4,6 +4,7 @@ namespace Buggregator\Trap\Traffic\Parser; +use Buggregator\Trap\Support\Stream\Base64DecodeFilter; use Buggregator\Trap\Support\StreamHelper; use Buggregator\Trap\Traffic\Message\Multipart\Field; use Buggregator\Trap\Traffic\Message\Multipart\File; @@ -111,7 +112,9 @@ public static function parseMultipartBody(StreamInterface $stream, string $bound $writeFilters = []; if ($part->hasHeader('Content-Transfer-Encoding')) { $encoding = $part->getHeaderLine('Content-Transfer-Encoding'); - $encoding === 'base64' and $writeFilters[] = \Buggregator\Trap\Support\Stream\Base64DecodeFilter::FILTER_NAME; + if ($encoding === 'base64') { + $writeFilters[] = Base64DecodeFilter::FILTER_NAME; + } } $fileStream = StreamHelper::createFileStream(writeFilters: $writeFilters); From e41d505c565ad016ed0e3d04bcd723ecfae8a23b Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Sat, 29 Jun 2024 23:03:19 +0400 Subject: [PATCH 2/3] style: fix code style --- src/Client/Caster/ProtobufCaster.php | 4 +--- src/Sender/Console/Renderer/VarDumper.php | 12 +++--------- src/Sender/Console/Support/Tables.php | 4 +--- src/Traffic/Parser/Http.php | 4 +--- 4 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/Client/Caster/ProtobufCaster.php b/src/Client/Caster/ProtobufCaster.php index 58393b14..f55ea27e 100644 --- a/src/Client/Caster/ProtobufCaster.php +++ b/src/Client/Caster/ProtobufCaster.php @@ -55,9 +55,7 @@ final class ProtobufCaster public static function cast(Message $c, array $a, Stub $stub, bool $isNested): array { - /** - * @var DescriptorPool $pool - */ + /** @var DescriptorPool $pool */ $pool = DescriptorPool::getGeneratedPool(); /** @var PublicDescriptor|InternalDescriptor $descriptor */ $descriptor = $pool->getDescriptorByClassName($c::class); diff --git a/src/Sender/Console/Renderer/VarDumper.php b/src/Sender/Console/Renderer/VarDumper.php index 7bc3f390..821e770f 100644 --- a/src/Sender/Console/Renderer/VarDumper.php +++ b/src/Sender/Console/Renderer/VarDumper.php @@ -34,17 +34,11 @@ public function render(OutputInterface $output, Frame $frame): void { \assert($frame instanceof Frame\VarDumper); - /** - * @var list{Data, array}|false $payload - */ - $payload = @\unserialize(\base64_decode($frame->dump), ['allowed_classes' => [Data::class, Stub::class]]); + /** @var array{Data, array}|false $payload */ + $payload = @\unserialize(\base64_decode($frame->dump, true), ['allowed_classes' => [Data::class, Stub::class]]); // Impossible to decode the message, give up. - if ($payload === false) { - throw new \RuntimeException("Unable to decode a message."); - } - - self::$describer ??= $this->getDescriber(); + $payload === false and throw new \RuntimeException("Unable to decode the message."); [$data, $context] = $payload; diff --git a/src/Sender/Console/Support/Tables.php b/src/Sender/Console/Support/Tables.php index 959c4cd0..57bc5c44 100644 --- a/src/Sender/Console/Support/Tables.php +++ b/src/Sender/Console/Support/Tables.php @@ -30,9 +30,7 @@ public static function renderKeyValueTable(OutputInterface $output, string $titl $valueLength = \max(1, (new Terminal())->getWidth() - 7 - $keyLength); $table->setRows([...(static function (array $data) use ($valueLength): iterable { - /** - * @var array $data - */ + /** @var array $data */ foreach ($data as $key => $value) { $values = \strlen($value) > $valueLength ? \str_split($value, $valueLength) diff --git a/src/Traffic/Parser/Http.php b/src/Traffic/Parser/Http.php index 815223a9..18efadda 100644 --- a/src/Traffic/Parser/Http.php +++ b/src/Traffic/Parser/Http.php @@ -112,9 +112,7 @@ public static function parseMultipartBody(StreamInterface $stream, string $bound $writeFilters = []; if ($part->hasHeader('Content-Transfer-Encoding')) { $encoding = $part->getHeaderLine('Content-Transfer-Encoding'); - if ($encoding === 'base64') { - $writeFilters[] = Base64DecodeFilter::FILTER_NAME; - } + $encoding === 'base64' and $writeFilters[] = Base64DecodeFilter::FILTER_NAME; } $fileStream = StreamHelper::createFileStream(writeFilters: $writeFilters); From b3ec1613ad5531c3c0d427e7ddd11bfdec4a9bf1 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Sat, 29 Jun 2024 23:04:34 +0400 Subject: [PATCH 3/3] fix(var-dumper):cli renderer must use provided output that may be buffered --- src/Sender/Console/Renderer/Sentry/Exceptions.php | 2 +- src/Sender/Console/Renderer/VarDumper.php | 13 ++++++------- src/Sender/Console/Support/Tables.php | 1 - 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Sender/Console/Renderer/Sentry/Exceptions.php b/src/Sender/Console/Renderer/Sentry/Exceptions.php index 138acfcb..66b467f6 100644 --- a/src/Sender/Console/Renderer/Sentry/Exceptions.php +++ b/src/Sender/Console/Renderer/Sentry/Exceptions.php @@ -92,7 +92,7 @@ private static function renderTrace(OutputInterface $output, array $frames, bool "%s%s%s\n%s%s%s()", \str_pad("#$i", $numPad, ' '), (string) $file, - $line !== '' ? ":$line": '', + $line !== '' ? ":$line" : '', \str_repeat(' ', $numPad), $class, (string) $function, diff --git a/src/Sender/Console/Renderer/VarDumper.php b/src/Sender/Console/Renderer/VarDumper.php index 821e770f..98a2948f 100644 --- a/src/Sender/Console/Renderer/VarDumper.php +++ b/src/Sender/Console/Renderer/VarDumper.php @@ -23,8 +23,6 @@ */ final class VarDumper implements Renderer { - private static ?DumpDescriptorInterface $describer = null; - public function isSupport(Frame $frame): bool { return $frame->type === ProtoType::VarDumper; @@ -41,15 +39,16 @@ public function render(OutputInterface $output, Frame $frame): void $payload === false and throw new \RuntimeException("Unable to decode the message."); [$data, $context] = $payload; - - self::$describer->describe(new SymfonyStyle(new ArrayInput([]), $output), $data, $context, 0); + $this + ->getDescriber($output) + ->describe(new SymfonyStyle(new ArrayInput([]), $output), $data, $context, 0); } - private function getDescriber(): DumpDescriptorInterface + private function getDescriber(OutputInterface $output): DumpDescriptorInterface { - return new class() implements DumpDescriptorInterface { + return new class(new CliDumper($output)) implements DumpDescriptorInterface { public function __construct( - private readonly CliDumper $dumper = new CliDumper(), + private readonly CliDumper $dumper, ) {} /** diff --git a/src/Sender/Console/Support/Tables.php b/src/Sender/Console/Support/Tables.php index 57bc5c44..42c9a716 100644 --- a/src/Sender/Console/Support/Tables.php +++ b/src/Sender/Console/Support/Tables.php @@ -4,7 +4,6 @@ namespace Buggregator\Trap\Sender\Console\Support; -use Buggregator\Trap\Support\Json; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Terminal;