From caa661d74f0ab86cd5fbbadca2fb4b748f70f8a8 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 1 Sep 2023 22:12:36 +0200 Subject: [PATCH] Psalm improvements --- composer.json | 3 ++- psalm-baseline.xml | 45 ++++++++++++++++++++++++++++++++++++ psalm.xml | 18 +++++++++++++++ src/Attributes/ErrorCode.php | 3 +-- src/Message.php | 5 +++- src/StunClient.php | 4 +--- 6 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 psalm-baseline.xml create mode 100644 psalm.xml diff --git a/composer.json b/composer.json index 36aef66..eefddba 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,8 @@ "require": { "php-64bit": ">=8.1.17", "amphp/socket": "^2.2", - "webmozart/assert": "^1.11" + "webmozart/assert": "^1.11", + "psalm/phar": "^5.15" }, "require-dev": { "amphp/php-cs-fixer-config": "v2.x-dev" diff --git a/psalm-baseline.xml b/psalm-baseline.xml new file mode 100644 index 0000000..5871cd8 --- /dev/null +++ b/psalm-baseline.xml @@ -0,0 +1,45 @@ + + + + + $left + $length + + + $this::TYPE + + + + + $length-4 + + + $class*100+$number + + + + + $_ + + + + + $length + + + + + $length + + + + + $len + + + + + MessageMethod::MASK + + + diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..05d51ab --- /dev/null +++ b/psalm.xml @@ -0,0 +1,18 @@ + + + + + + + + + diff --git a/src/Attributes/ErrorCode.php b/src/Attributes/ErrorCode.php index d339ff3..318b7a2 100644 --- a/src/Attributes/ErrorCode.php +++ b/src/Attributes/ErrorCode.php @@ -27,8 +27,7 @@ final class ErrorCode extends Attribute $reader->readLength(2, $cancellation); $class = \ord($reader->readLength(1, $cancellation)); $number = \ord($reader->readLength(1, $cancellation)); - Assert::true($class >= 3); - Assert::true($class <= 6); + Assert::true($class >= 3 && $class <= 6); Assert::true($number < 100); return new self($class*100+$number, $reader->readLength($length-4)); } diff --git a/src/Message.php b/src/Message.php index 3421b76..4661944 100644 --- a/src/Message.php +++ b/src/Message.php @@ -41,7 +41,10 @@ final class Message $attributes = []; while ($length) { - $attributes []= Attribute::read($reader, $length, $transactionId, $cancellation); + $attr = Attribute::read($reader, $length, $transactionId, $cancellation); + if ($attr) { + $attributes []= $attr; + } } return new self( diff --git a/src/StunClient.php b/src/StunClient.php index 9087885..571cc5b 100644 --- a/src/StunClient.php +++ b/src/StunClient.php @@ -24,9 +24,7 @@ final class StunClient $this->socket = connect($endpoint); } - /** - * @return list - */ + /** @no-named-arguments */ public function bind(Attribute ...$attributes): Message { $msg = new Message(MessageMethod::BINDING, MessageClass::REQUEST, $attributes, \random_bytes(12));