Psalm improvements

This commit is contained in:
Daniil Gentili 2023-09-01 22:12:36 +02:00
parent ea741248b5
commit caa661d74f
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
6 changed files with 71 additions and 7 deletions

View File

@ -5,7 +5,8 @@
"require": { "require": {
"php-64bit": ">=8.1.17", "php-64bit": ">=8.1.17",
"amphp/socket": "^2.2", "amphp/socket": "^2.2",
"webmozart/assert": "^1.11" "webmozart/assert": "^1.11",
"psalm/phar": "^5.15"
}, },
"require-dev": { "require-dev": {
"amphp/php-cs-fixer-config": "v2.x-dev" "amphp/php-cs-fixer-config": "v2.x-dev"

45
psalm-baseline.xml Normal file
View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<file src="src/Attribute.php">
<ArgumentTypeCoercion>
<code>$left</code>
<code>$length</code>
</ArgumentTypeCoercion>
<UndefinedConstant>
<code>$this::TYPE</code>
</UndefinedConstant>
</file>
<file src="src/Attributes/ErrorCode.php">
<ArgumentTypeCoercion>
<code>$length-4</code>
</ArgumentTypeCoercion>
<InvalidArgument>
<code>$class*100+$number</code>
</InvalidArgument>
</file>
<file src="src/Attributes/MappedAddress.php">
<ParamNameMismatch>
<code>$_</code>
</ParamNameMismatch>
</file>
<file src="src/Attributes/Software.php">
<ArgumentTypeCoercion>
<code>$length</code>
</ArgumentTypeCoercion>
</file>
<file src="src/Attributes/Username.php">
<ArgumentTypeCoercion>
<code>$length</code>
</ArgumentTypeCoercion>
</file>
<file src="src/Attributes/XorMappedAddress.php">
<PossiblyUndefinedVariable>
<code>$len</code>
</PossiblyUndefinedVariable>
</file>
<file src="src/Message.php">
<UndefinedConstant>
<code>MessageMethod::MASK</code>
</UndefinedConstant>
</file>
</files>

18
psalm.xml Normal file
View File

@ -0,0 +1,18 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="false"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>

View File

@ -27,8 +27,7 @@ final class ErrorCode extends Attribute
$reader->readLength(2, $cancellation); $reader->readLength(2, $cancellation);
$class = \ord($reader->readLength(1, $cancellation)); $class = \ord($reader->readLength(1, $cancellation));
$number = \ord($reader->readLength(1, $cancellation)); $number = \ord($reader->readLength(1, $cancellation));
Assert::true($class >= 3); Assert::true($class >= 3 && $class <= 6);
Assert::true($class <= 6);
Assert::true($number < 100); Assert::true($number < 100);
return new self($class*100+$number, $reader->readLength($length-4)); return new self($class*100+$number, $reader->readLength($length-4));
} }

View File

@ -41,7 +41,10 @@ final class Message
$attributes = []; $attributes = [];
while ($length) { while ($length) {
$attributes []= Attribute::read($reader, $length, $transactionId, $cancellation); $attr = Attribute::read($reader, $length, $transactionId, $cancellation);
if ($attr) {
$attributes []= $attr;
}
} }
return new self( return new self(

View File

@ -24,9 +24,7 @@ final class StunClient
$this->socket = connect($endpoint); $this->socket = connect($endpoint);
} }
/** /** @no-named-arguments */
* @return list<Attribute>
*/
public function bind(Attribute ...$attributes): Message public function bind(Attribute ...$attributes): Message
{ {
$msg = new Message(MessageMethod::BINDING, MessageClass::REQUEST, $attributes, \random_bytes(12)); $msg = new Message(MessageMethod::BINDING, MessageClass::REQUEST, $attributes, \random_bytes(12));