diff --git a/phpstan.neon b/phpstan.neon index 0f3104d..d570dc2 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -8,4 +8,9 @@ parameters: paths: - src - tests - checkMissingIterableValueType: false + ignoreErrors: + - + message: '#no value type specified in iterable type array#' + paths: + - %currentWorkingDirectory%/src + - %currentWorkingDirectory%/tests diff --git a/src/Buffer.php b/src/Buffer.php index 49784ef..2ade3b5 100644 --- a/src/Buffer.php +++ b/src/Buffer.php @@ -13,8 +13,11 @@ final class Buffer extends ByteBuffer { public function readUInt32LE(): int { - /** @phpstan-ignore-next-line */ - return unpack('V', $this->consume(4))[1]; + $unpacked = unpack('V', $this->consume(4)); + + \assert(\is_array($unpacked) && \array_key_exists(1, $unpacked)); + + return $unpacked[1]; } public function consumeTimestamp(): int diff --git a/src/Config/ClientConfig.php b/src/Config/ClientConfig.php index 015a3c6..4236179 100644 --- a/src/Config/ClientConfig.php +++ b/src/Config/ClientConfig.php @@ -141,6 +141,6 @@ final class ClientConfig 'user_agent' => $this->userAgent, ]; - return json_encode($data, JSON_THROW_ON_ERROR | JSON_FORCE_OBJECT); + return json_encode($data, JSON_THROW_ON_ERROR); } } diff --git a/src/Lookup.php b/src/Lookup.php index 9c71a26..b6fb8aa 100644 --- a/src/Lookup.php +++ b/src/Lookup.php @@ -120,6 +120,7 @@ final class Lookup return; } + /** @phpstan-ignore-next-line */ $producers = $this->producers[$topic] ??= new Deferred(); if ($producers instanceof Deferred) { @@ -200,6 +201,7 @@ final class Lookup } while (true) { + /** @phpstan-ignore-next-line */ if (null === ($this->consumers[$consumer->address][$consumer->topic][$consumer->channel] ?? null)) { $consumer->close(); @@ -257,6 +259,7 @@ final class Lookup } } + /** @phpstan-ignore-next-line */ if (($deferred = ($this->producers[$topic] ?? null)) instanceof Deferred) { $deferred->resolve($producers); } diff --git a/src/Stream/GzipStream.php b/src/Stream/GzipStream.php index 43bcc43..58b451c 100644 --- a/src/Stream/GzipStream.php +++ b/src/Stream/GzipStream.php @@ -13,21 +13,17 @@ use function Amp\call; class GzipStream implements Stream { - /** - * @var null|\InflateContext - */ - private $inflate; + private ?\InflateContext $inflate = null; - /** - * @var null|\DeflateContext - */ - private $deflate; + private ?\DeflateContext $deflate = null; private Buffer $buffer; public function __construct(private Stream $stream, private int $level, string $bytes = '') { + /** @var false|\InflateContext $inflate */ $inflate = @inflate_init(ZLIB_ENCODING_RAW, ['level' => $this->level]); + /** @var \DeflateContext|false $deflate */ $deflate = @deflate_init(ZLIB_ENCODING_RAW, ['level' => $this->level]); if (false === $inflate) { @@ -66,6 +62,7 @@ class GzipStream implements Stream if ('' === $data) { return null; } + /** @psalm-suppress UndefinedFunction,InvalidArgument */ $decompressed = inflate_add($this->inflate, $data, ZLIB_SYNC_FLUSH); if (false === $decompressed) { @@ -85,6 +82,7 @@ class GzipStream implements Stream throw new StreamException('The stream has already been closed'); } + /** @psalm-suppress UndefinedFunction,InvalidArgument */ $compressed = deflate_add($this->deflate, $data, ZLIB_SYNC_FLUSH); if (false === $compressed) { diff --git a/src/Stream/SnappyStream.php b/src/Stream/SnappyStream.php index 3318281..4595d12 100644 --- a/src/Stream/SnappyStream.php +++ b/src/Stream/SnappyStream.php @@ -26,7 +26,7 @@ class SnappyStream implements Stream public function __construct(private Stream $stream, string $bytes = '') { - if (!\function_exists('snappy_uncompress')) { + if (!\function_exists('snappy_uncompress') || !\function_exists('snappy_compress')) { throw SnappyException::notInstalled(); } @@ -60,6 +60,7 @@ class SnappyStream implements Stream case self::TYPE_COMPRESSED: $this->buffer->discard(self::SIZE_CHECKSUM); + /** @psalm-suppress UndefinedFunction */ return snappy_uncompress($this->buffer->consume($size - self::SIZE_HEADER)); case self::TYPE_UNCOMPRESSED: $this->buffer->discard(self::SIZE_CHECKSUM); @@ -79,6 +80,7 @@ class SnappyStream implements Stream public function write(string $data): Promise { return call(function () use ($data): Promise { + /** @var string $result */ $result = pack('CCCCCCCCCC', ...self::IDENTIFIER); foreach (str_split($data, self::SIZE_CHUNK) as $chunk) { @@ -94,23 +96,27 @@ class SnappyStream implements Stream $this->stream->close(); } - /** - * @psalm-suppress PossiblyFalseArgument - */ private function compress(string $uncompressed): string { + /** @psalm-suppress UndefinedFunction */ $compressed = snappy_compress($uncompressed); + \assert(\is_string($compressed)); + [$type, $data] = \strlen($compressed) <= 0.875 * \strlen($uncompressed) ? [self::TYPE_COMPRESSED, $compressed] : [self::TYPE_UNCOMPRESSED, $uncompressed]; - /** @phpstan-ignore-next-line */ - $checksum = unpack('N', hash('crc32c', $uncompressed, true))[1]; + /** @psalm-suppress PossiblyFalseArgument */ + $unpacked = unpack('N', hash('crc32c', $uncompressed, true)); + \assert(\is_array($unpacked)); + + $checksum = $unpacked[1]; $checksum = (($checksum >> 15) | ($checksum << 17)) + 0xA282EAD8 & 0xFFFFFFFF; $size = (\strlen($data) + 4) << 8; + /** @psalm-suppress PossiblyFalseOperand */ return pack('VV', $type + $size, $checksum).$data; } } diff --git a/tests/ProducerTest.php b/tests/ProducerTest.php index 9c7d5d1..0c1fc3f 100644 --- a/tests/ProducerTest.php +++ b/tests/ProducerTest.php @@ -26,9 +26,6 @@ final class ProducerTest extends TestCase }); } - /** - * @return Generator - */ public function pubFails(): Generator { yield 'Empty body' => ['test', '', 'E_BAD_MESSAGE PUB invalid message body size 0'];