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, clear psalm-baseline.yaml: Test, Http, Sentry, Sender.php #64

Merged
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
438 changes: 305 additions & 133 deletions psalm-baseline.xml

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/Command/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ final class Test extends Command
private string $addr = '127.0.0.1';
private int $port = 9912;

/** @psalm-suppress PropertyNotSetInConstructor */
private Logger $logger;

protected function execute(
Expand Down Expand Up @@ -197,14 +198,15 @@ private function sendMailPackage(
$output->write(
'> ' . \str_replace(["\r", "\n"], ["\e[32m\\r\e[0m", "\e[32m\\n\e[0m"], $content),
true,
$output::OUTPUT_RAW,
OutputInterface::OUTPUT_RAW,
);
}

if ($expectedResponsePrefix === '') {
return;
}
@\socket_recv($socket, $buf, 65536, 0);
/** @var string|null $buf */
if ($buf === null) {
$output->writeln('<error>Disconnected</>');
return;
Expand All @@ -214,7 +216,7 @@ private function sendMailPackage(
"\e[33m< \"%s\"\e[0m",
\str_replace(["\r", "\n"], ["\e[32m\\r\e[33m", "\e[32m\\n\e[33m"], $buf)),
true,
$output::OUTPUT_RAW,
OutputInterface::OUTPUT_RAW,
);

$prefix = \substr($buf, 0, \strlen($expectedResponsePrefix));
Expand Down Expand Up @@ -245,7 +247,7 @@ private function sendContent(string $file): void
} catch (\Throwable $e) {
$this->logger->exception($e, "$file sending error", important: true);
} finally {
if (isset($fp) && \is_resource($fp)) {
if (isset($fp)) {
@\flock($fp, LOCK_UN);
@\fclose($fp);
}
Expand Down
9 changes: 4 additions & 5 deletions src/Handler/Http/Handler/Fallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Buggregator\Trap\Handler\Pipeline;
use Buggregator\Trap\Proto\Frame;
use Buggregator\Trap\Traffic\StreamClient;
use DateTimeInterface;
use Nyholm\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -43,8 +42,9 @@ public function __construct(

public function handle(StreamClient $streamClient, ServerRequestInterface $request, callable $next): iterable
{
$time = $request->getAttribute('begin_at', null);
$time = $time instanceof DateTimeInterface ? $time : new \DateTimeImmutable();
/** @var mixed $time */
$time = $request->getAttribute('begin_at');
$time = $time instanceof \DateTimeImmutable ? $time : new \DateTimeImmutable();
$gotFrame = false;

try {
Expand All @@ -68,13 +68,12 @@ public function handle(StreamClient $streamClient, ServerRequestInterface $reque
throw new \RuntimeException('Invalid response type.');
}

HttpEmitter::emit($streamClient, $response);
break;
}

\Fiber::suspend();
} while (true);

HttpEmitter::emit($streamClient, $response);
} catch (\Throwable) {
// Emit error response
HttpEmitter::emit($streamClient, new Response(500));
Expand Down
3 changes: 2 additions & 1 deletion src/Handler/Http/Handler/Websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function handle(StreamClient $streamClient, ServerRequestInterface $reque
}

// Get the time of the request
$time = $request->getAttribute('begin_at', null);
/** @var mixed $time */
$time = $request->getAttribute('begin_at');
$time = $time instanceof \DateTimeImmutable ? $time : new \DateTimeImmutable();

// Calculate the accept key for the handshake
Expand Down
17 changes: 14 additions & 3 deletions src/Handler/Http/Middleware/SentryTrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
/**
* @internal
* @psalm-internal Buggregator\Trap
*
* @psalm-import-type SentryStoreMessage from Frame\Sentry\SentryStore
*/
final class SentryTrap implements Middleware
{
Expand Down Expand Up @@ -64,7 +66,12 @@ public function processEnvelope(ServerRequestInterface $request): ResponseInterf
}

$request->getBody()->rewind();
$frame = EnvelopeParser::parse($request->getBody(), $request->getAttribute('begin_at', null));

/** @var mixed $time */
$time = $request->getAttribute('begin_at');
$time = $time instanceof \DateTimeImmutable ? $time : new \DateTimeImmutable();

$frame = EnvelopeParser::parse($request->getBody(), $time);
Fiber::suspend($frame);

return new Response(200);
Expand All @@ -77,13 +84,17 @@ private function processStore(ServerRequestInterface $request): ResponseInterfac
// Reject too big content
return new Response(413);
}

/** @var SentryStoreMessage $payload */
$payload = \json_decode((string)$request->getBody(), true, 96, \JSON_THROW_ON_ERROR);

/** @psalm-suppress MixedAssignment */
$time = $request->getAttribute('begin_at');
$time = $time instanceof \DateTimeImmutable ? $time : new \DateTimeImmutable();

Fiber::suspend(
new Frame\Sentry\SentryStore(
message: $payload,
time: $request->getAttribute('begin_at', null),
time: $time,
)
);

Expand Down
20 changes: 16 additions & 4 deletions src/Proto/Frame/Sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,28 @@
/**
* @internal
* @psalm-internal Buggregator
*
* @psalm-import-type SentryStoreMessage from Sentry\SentryStore
* @psalm-import-type SentryEnvelopeMessage from Sentry\SentryEnvelope
*/
abstract class Sentry extends Frame
{
public static function fromString(string $payload, DateTimeImmutable $time): static
final public static function fromString(string $payload, DateTimeImmutable $time): static
{
static::class === self::class or throw new \LogicException(
\sprintf('Factory method must be called from %s class.', self::class),
);

/** @var array{type: string, ...mixed} $data */
$data = \json_decode($payload, true, 512, JSON_THROW_ON_ERROR);
return match (true) {
$data['type'] === SentryEnvelope::SENTRY_FRAME_TYPE => SentryEnvelope::fromArray($data, $time),
$data['type'] === SentryStore::SENTRY_FRAME_TYPE => SentryStore::fromArray($data, $time),

/** @psalm-suppress InvalidArgument */
$result = match ($data['type']) {
SentryEnvelope::SENTRY_FRAME_TYPE => SentryEnvelope::fromArray($data, $time),
SentryStore::SENTRY_FRAME_TYPE => SentryStore::fromArray($data, $time),
default => throw new \InvalidArgumentException('Unknown Sentry frame type.'),
};

return $result;
}
}
11 changes: 11 additions & 0 deletions src/Proto/Frame/Sentry/SentryEnvelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
/**
* @internal
* @psalm-internal Buggregator
*
* @psalm-type SentryEnvelopeMessage = array{
* type: SentryEnvelope::SENTRY_FRAME_TYPE,
* items: array<array-key, array{array<array-key, mixed>, mixed}>,
* headers: array<string, string>
* }
*/
final class SentryEnvelope extends Frame\Sentry
{
Expand Down Expand Up @@ -39,6 +45,11 @@ public function __toString(): string
);
}

/**
* @psalm-assert SentryEnvelopeMessage $data
*
* @param SentryEnvelopeMessage $data
*/
public static function fromArray(array $data, DateTimeImmutable $time): static
{
$items = [];
Expand Down
62 changes: 35 additions & 27 deletions src/Proto/Frame/Sentry/SentryStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,42 @@
/**
* @internal
* @psalm-internal Buggregator
*
* @psalm-type SentryStoreMessage=array{
* type: SentryStore::SENTRY_FRAME_TYPE,
* event_id: non-empty-string,
* timestamp: positive-int,
* platform?: non-empty-string,
* sdk?: array{
* name: non-empty-string,
* version: non-empty-string,
* },
* logger?: non-empty-string,
* contexts?: array<non-empty-string, array<non-empty-string, non-empty-string>>,
* environment?: non-empty-string,
* server_name?: non-empty-string,
* transaction?: non-empty-string,
* modules?: array<non-empty-string, non-empty-string>,
* exception?: array<array-key, array{
* type: non-empty-string,
* value: non-empty-string,
* stacktrace: array{
* frames: array<array-key, array{
* filename: non-empty-string,
* lineno: positive-int,
* abs_path: non-empty-string,
* context_line: non-empty-string
* }>
* }
* }>
* }
*/
final class SentryStore extends Frame\Sentry
{
public const SENTRY_FRAME_TYPE = 'store';

/**
* @param array{
* event_id: non-empty-string,
* timestamp: positive-int,
* platform?: non-empty-string,
* sdk?: array{
* name: non-empty-string,
* version: non-empty-string,
* },
* logger?: non-empty-string,
* contexts?: array<non-empty-string, array<non-empty-string, non-empty-string>>,
* environment?: non-empty-string,
* server_name?: non-empty-string,
* transaction?: non-empty-string,
* modules?: array<non-empty-string, non-empty-string>,
* exception?: array<array-key, array{
* type: non-empty-string,
* value: non-empty-string,
* stacktrace: array{
* frames: array<array-key, array{
* filename: non-empty-string,
* lineno: positive-int,
* abs_path: non-empty-string,
* context_line: non-empty-string
* }
* }
* }>
* } $message
* @param SentryStoreMessage $message
*/
public function __construct(
public readonly array $message,
Expand All @@ -61,6 +64,11 @@ public function __toString(): string
return Json::encode($this->message);
}

/**
* @psalm-assert SentryStoreMessage $data
*
* @param SentryStoreMessage $data
*/
public static function fromArray(array $data, DateTimeImmutable $time): static
{
return new self($data, $time);
Expand Down
2 changes: 1 addition & 1 deletion src/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
interface Sender
{
/**
* @param iterable<int, Frame> $frames
* @param iterable<array-key, Frame> $frames
*/
public function send(iterable $frames): void;
}
5 changes: 4 additions & 1 deletion src/Sender/Console/Renderer/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ private function renderData(OutputInterface $output, Frame\Http $frame): void
foreach ($uploadedFiles as $name => $fileSet) {
$fileSet = \is_array($fileSet) ? $fileSet : [$fileSet];
foreach ($fileSet as $subName => $file) {
/** @var int<0, max>|null $size */
$size = $file->getSize();

Files::renderFile(
$output,
(string)$file->getClientFilename(),
$file->getSize(),
$size,
(string)$file->getClientMediaType(),
Field: \sprintf("%s[%s]", $name, $subName)
);
Expand Down
1 change: 1 addition & 0 deletions src/Sender/Console/Support/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static function renderMetadata(OutputInterface $output, array $data): voi
\array_keys($data)),
);

/** @var mixed $value */
foreach ($data as $head => $value) {
// Align headers to the right
self::renderHeader(
Expand Down
2 changes: 1 addition & 1 deletion src/Traffic/Message/Multipart/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function moveTo(string $targetPath): void
}

/**
* @return non-negative-int|null
* @return int<0, max>|null
*/
public function getSize(): ?int
{
Expand Down
Loading