Skip to content
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

Fix Psalm issues #131

Merged
merged 3 commits into from
Jun 29, 2024
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
38 changes: 1 addition & 37 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@
<code><![CDATA[\strpos($payload, '[')]]></code>
</PossiblyFalseOperand>
</file>
<file src="src/Sender/Console/Renderer/Binary.php">
<PossiblyNullOperand>
<code><![CDATA[Files::normalizeSize($size)]]></code>
</PossiblyNullOperand>
</file>
<file src="src/Sender/Console/Renderer/Http.php">
<MixedArgumentTypeCoercion>
<code><![CDATA[$request->getCookieParams()]]></code>
Expand All @@ -113,11 +108,8 @@
<ArgumentTypeCoercion>
<code><![CDATA[$file]]></code>
<code><![CDATA[$file]]></code>
<code><![CDATA[$function]]></code>
</ArgumentTypeCoercion>
<MissingClosureReturnType>
<code><![CDATA[static fn() => $output->writeln(]]></code>
</MissingClosureReturnType>
<MissingClosureReturnType/>
<MixedArgument>
<code><![CDATA[$exception['type'] ?? 'Exception']]></code>
<code><![CDATA[$exception['value']]]></code>
Expand All @@ -129,11 +121,6 @@
<PossiblyInvalidArgument>
<code><![CDATA[\str_replace('\\', '/', $file)]]></code>
</PossiblyInvalidArgument>
<PossiblyNullArgument>
<code><![CDATA[$file]]></code>
<code><![CDATA[$file]]></code>
<code><![CDATA[$function]]></code>
</PossiblyNullArgument>
</file>
<file src="src/Sender/Console/Renderer/Sentry/Header.php">
<MixedArgumentTypeCoercion>
Expand Down Expand Up @@ -161,39 +148,19 @@
</MixedAssignment>
</file>
<file src="src/Sender/Console/Renderer/VarDumper.php">
<MixedArgument>
<code><![CDATA[$context]]></code>
<code><![CDATA[$data]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$context]]></code>
<code><![CDATA[$data]]></code>
</MixedArrayAccess>
<MixedAssignment>
<code><![CDATA[$controller]]></code>
<code><![CDATA[$describer]]></code>
<code><![CDATA[$fileLink]]></code>
<code><![CDATA[$meta['Command']]]></code>
<code><![CDATA[$meta['File']]]></code>
<code><![CDATA[$meta['Method']]]></code>
<code><![CDATA[$meta['URI']]]></code>
<code><![CDATA[$payload]]></code>
<code><![CDATA[[$data, $context]]]></code>
</MixedAssignment>
<MixedMethodCall>
<code><![CDATA[describe]]></code>
</MixedMethodCall>
</file>
<file src="src/Sender/Console/Support/Common.php">
<MixedAssignment>
<code><![CDATA[$item]]></code>
</MixedAssignment>
</file>
<file src="src/Sender/Console/Support/Tables.php">
<MixedAssignment>
<code><![CDATA[$value]]></code>
</MixedAssignment>
</file>
<file src="src/Sender/SocketSender.php">
<MixedPropertyTypeCoercion>
<code><![CDATA[new \SplQueue()]]></code>
Expand All @@ -218,8 +185,5 @@
<code><![CDATA[$value]]></code>
<code><![CDATA[$values[$fd->getName()]]]></code>
</MixedAssignment>
<MixedMethodCall>
<code><![CDATA[getDescriptorByClassName]]></code>
</MixedMethodCall>
</file>
</files>
4 changes: 3 additions & 1 deletion src/Client/Caster/ProtobufCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ 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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Sender/Console/Renderer/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}
11 changes: 6 additions & 5 deletions src/Sender/Console/Renderer/Sentry/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
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) ;
Expand All @@ -80,21 +81,21 @@
}

$file = $getValue($frame, 'filename');
$line = $getValue($frame, 'lineno', null);
$line = $getValue($frame, 'lineno');
$class = $getValue($frame, 'class');
/** @psalm-suppress RiskyTruthyFalsyComparison */
$class = empty($class) ? '' : $class . '::';
$function = $getValue($frame, 'function');

$renderer = static fn() => $output->writeln(

Check failure on line 90 in src/Sender/Console/Renderer/Sentry/Exceptions.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

MissingClosureReturnType

src/Sender/Console/Renderer/Sentry/Exceptions.php:90:25: MissingClosureReturnType: Closure does not have a return type, expecting null (see https://psalm.dev/068)
\sprintf(
"<fg=gray>%s</><fg=white;options=bold>%s<fg=yellow>%s</>\n%s<fg=yellow>%s</><fg=gray>%s()</>",
\str_pad("#$i", $numPad, ' '),
$file,
!$line ? '' : ":$line",
(string) $file,
$line !== '' ? ":$line" : '',
\str_repeat(' ', $numPad),
$class,
$function,
(string) $function,
),
);

Expand Down
25 changes: 11 additions & 14 deletions src/Sender/Console/Renderer/VarDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,23 @@ public function render(OutputInterface $output, Frame $frame): void
{
\assert($frame instanceof Frame\VarDumper);

$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.");
}

static $describer = null;
$describer ??= $this->getDescriber();
$payload === false and throw new \RuntimeException("Unable to decode the message.");

[$data, $context] = $payload;

$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 CliDumper $dumper = new CliDumper(),
private readonly CliDumper $dumper,
) {}

/**
Expand All @@ -72,8 +69,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('<href=%s>%s</>', $fileLink, $sourceInfo);
if (isset($source['file_link'])) {
$sourceInfo = \sprintf('<href=%s>%s</>', $source['file_link'], $sourceInfo);
$meta['Source'] = $sourceInfo;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Sender/Console/Support/Tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +14,9 @@
*/
final class Tables
{
/**
* @param array<string, string> $data
*/
public static function renderKeyValueTable(OutputInterface $output, string $title, array $data): void
{
$table = (new Table($output))->setHeaderTitle($title);
Expand All @@ -27,10 +29,8 @@ 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<string, string> $data */
foreach ($data as $key => $value) {
if (!\is_string($value)) {
$value = Json::encode($value);
}
$values = \strlen($value) > $valueLength
? \str_split($value, $valueLength)
: [$value];
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Stream/Base64DecodeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions src/Traffic/Message/Multipart/Part.php
Original file line number Diff line number Diff line change
Expand Up @@ -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=(?:(?<a>[^" ;,]++)|"(?<b>[^"]++)")/', $contentDisposition, $matches) === 1
? ($matches['a'] ?: $matches['b'])
Expand Down
3 changes: 2 additions & 1 deletion src/Traffic/Parser/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,7 +112,7 @@ 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;
$encoding === 'base64' and $writeFilters[] = Base64DecodeFilter::FILTER_NAME;
}

$fileStream = StreamHelper::createFileStream(writeFilters: $writeFilters);
Expand Down
Loading