Fix and suppress static analyze errors

This commit is contained in:
2022-09-11 23:05:38 +03:00
parent bd8d13692f
commit 350f08c2c1
7 changed files with 33 additions and 21 deletions

View File

@ -8,4 +8,9 @@ parameters:
paths: paths:
- src - src
- tests - tests
checkMissingIterableValueType: false ignoreErrors:
-
message: '#no value type specified in iterable type array#'
paths:
- %currentWorkingDirectory%/src
- %currentWorkingDirectory%/tests

View File

@ -13,8 +13,11 @@ final class Buffer extends ByteBuffer
{ {
public function readUInt32LE(): int public function readUInt32LE(): int
{ {
/** @phpstan-ignore-next-line */ $unpacked = unpack('V', $this->consume(4));
return unpack('V', $this->consume(4))[1];
\assert(\is_array($unpacked) && \array_key_exists(1, $unpacked));
return $unpacked[1];
} }
public function consumeTimestamp(): int public function consumeTimestamp(): int

View File

@ -141,6 +141,6 @@ final class ClientConfig
'user_agent' => $this->userAgent, 'user_agent' => $this->userAgent,
]; ];
return json_encode($data, JSON_THROW_ON_ERROR | JSON_FORCE_OBJECT); return json_encode($data, JSON_THROW_ON_ERROR);
} }
} }

View File

@ -120,6 +120,7 @@ final class Lookup
return; return;
} }
/** @phpstan-ignore-next-line */
$producers = $this->producers[$topic] ??= new Deferred(); $producers = $this->producers[$topic] ??= new Deferred();
if ($producers instanceof Deferred) { if ($producers instanceof Deferred) {
@ -200,6 +201,7 @@ final class Lookup
} }
while (true) { while (true) {
/** @phpstan-ignore-next-line */
if (null === ($this->consumers[$consumer->address][$consumer->topic][$consumer->channel] ?? null)) { if (null === ($this->consumers[$consumer->address][$consumer->topic][$consumer->channel] ?? null)) {
$consumer->close(); $consumer->close();
@ -257,6 +259,7 @@ final class Lookup
} }
} }
/** @phpstan-ignore-next-line */
if (($deferred = ($this->producers[$topic] ?? null)) instanceof Deferred) { if (($deferred = ($this->producers[$topic] ?? null)) instanceof Deferred) {
$deferred->resolve($producers); $deferred->resolve($producers);
} }

View File

@ -13,21 +13,17 @@ use function Amp\call;
class GzipStream implements Stream class GzipStream implements Stream
{ {
/** private ?\InflateContext $inflate = null;
* @var null|\InflateContext
*/
private $inflate;
/** private ?\DeflateContext $deflate = null;
* @var null|\DeflateContext
*/
private $deflate;
private Buffer $buffer; private Buffer $buffer;
public function __construct(private Stream $stream, private int $level, string $bytes = '') public function __construct(private Stream $stream, private int $level, string $bytes = '')
{ {
/** @var false|\InflateContext $inflate */
$inflate = @inflate_init(ZLIB_ENCODING_RAW, ['level' => $this->level]); $inflate = @inflate_init(ZLIB_ENCODING_RAW, ['level' => $this->level]);
/** @var \DeflateContext|false $deflate */
$deflate = @deflate_init(ZLIB_ENCODING_RAW, ['level' => $this->level]); $deflate = @deflate_init(ZLIB_ENCODING_RAW, ['level' => $this->level]);
if (false === $inflate) { if (false === $inflate) {
@ -66,6 +62,7 @@ class GzipStream implements Stream
if ('' === $data) { if ('' === $data) {
return null; return null;
} }
/** @psalm-suppress UndefinedFunction,InvalidArgument */
$decompressed = inflate_add($this->inflate, $data, ZLIB_SYNC_FLUSH); $decompressed = inflate_add($this->inflate, $data, ZLIB_SYNC_FLUSH);
if (false === $decompressed) { if (false === $decompressed) {
@ -85,6 +82,7 @@ class GzipStream implements Stream
throw new StreamException('The stream has already been closed'); throw new StreamException('The stream has already been closed');
} }
/** @psalm-suppress UndefinedFunction,InvalidArgument */
$compressed = deflate_add($this->deflate, $data, ZLIB_SYNC_FLUSH); $compressed = deflate_add($this->deflate, $data, ZLIB_SYNC_FLUSH);
if (false === $compressed) { if (false === $compressed) {

View File

@ -26,7 +26,7 @@ class SnappyStream implements Stream
public function __construct(private Stream $stream, string $bytes = '') 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(); throw SnappyException::notInstalled();
} }
@ -60,6 +60,7 @@ class SnappyStream implements Stream
case self::TYPE_COMPRESSED: case self::TYPE_COMPRESSED:
$this->buffer->discard(self::SIZE_CHECKSUM); $this->buffer->discard(self::SIZE_CHECKSUM);
/** @psalm-suppress UndefinedFunction */
return snappy_uncompress($this->buffer->consume($size - self::SIZE_HEADER)); return snappy_uncompress($this->buffer->consume($size - self::SIZE_HEADER));
case self::TYPE_UNCOMPRESSED: case self::TYPE_UNCOMPRESSED:
$this->buffer->discard(self::SIZE_CHECKSUM); $this->buffer->discard(self::SIZE_CHECKSUM);
@ -79,6 +80,7 @@ class SnappyStream implements Stream
public function write(string $data): Promise public function write(string $data): Promise
{ {
return call(function () use ($data): Promise { return call(function () use ($data): Promise {
/** @var string $result */
$result = pack('CCCCCCCCCC', ...self::IDENTIFIER); $result = pack('CCCCCCCCCC', ...self::IDENTIFIER);
foreach (str_split($data, self::SIZE_CHUNK) as $chunk) { foreach (str_split($data, self::SIZE_CHUNK) as $chunk) {
@ -94,23 +96,27 @@ class SnappyStream implements Stream
$this->stream->close(); $this->stream->close();
} }
/**
* @psalm-suppress PossiblyFalseArgument
*/
private function compress(string $uncompressed): string private function compress(string $uncompressed): string
{ {
/** @psalm-suppress UndefinedFunction */
$compressed = snappy_compress($uncompressed); $compressed = snappy_compress($uncompressed);
\assert(\is_string($compressed));
[$type, $data] = \strlen($compressed) <= 0.875 * \strlen($uncompressed) [$type, $data] = \strlen($compressed) <= 0.875 * \strlen($uncompressed)
? [self::TYPE_COMPRESSED, $compressed] ? [self::TYPE_COMPRESSED, $compressed]
: [self::TYPE_UNCOMPRESSED, $uncompressed]; : [self::TYPE_UNCOMPRESSED, $uncompressed];
/** @phpstan-ignore-next-line */ /** @psalm-suppress PossiblyFalseArgument */
$checksum = unpack('N', hash('crc32c', $uncompressed, true))[1]; $unpacked = unpack('N', hash('crc32c', $uncompressed, true));
\assert(\is_array($unpacked));
$checksum = $unpacked[1];
$checksum = (($checksum >> 15) | ($checksum << 17)) + 0xA282EAD8 & 0xFFFFFFFF; $checksum = (($checksum >> 15) | ($checksum << 17)) + 0xA282EAD8 & 0xFFFFFFFF;
$size = (\strlen($data) + 4) << 8; $size = (\strlen($data) + 4) << 8;
/** @psalm-suppress PossiblyFalseOperand */
return pack('VV', $type + $size, $checksum).$data; return pack('VV', $type + $size, $checksum).$data;
} }
} }

View File

@ -26,9 +26,6 @@ final class ProducerTest extends TestCase
}); });
} }
/**
* @return Generator<string, array>
*/
public function pubFails(): Generator public function pubFails(): Generator
{ {
yield 'Empty body' => ['test', '', 'E_BAD_MESSAGE PUB invalid message body size 0']; yield 'Empty body' => ['test', '', 'E_BAD_MESSAGE PUB invalid message body size 0'];