From 48b89017c4ea7b34b9beb0886676868031f773c2 Mon Sep 17 00:00:00 2001 From: Anthony Birkett Date: Mon, 31 May 2021 13:05:53 +0100 Subject: [PATCH] Psalm fixes around resource handling. --- SourceQuery/Rcon/SourceRcon.php | 8 +++++--- SourceQuery/Socket/AbstractSocket.php | 14 ++++++++------ SourceQuery/Socket/TestableSocket.php | 24 ++++++++++++++---------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/SourceQuery/Rcon/SourceRcon.php b/SourceQuery/Rcon/SourceRcon.php index 6fd0ad1..c2ea251 100644 --- a/SourceQuery/Rcon/SourceRcon.php +++ b/SourceQuery/Rcon/SourceRcon.php @@ -31,6 +31,8 @@ final class SourceRcon extends AbstractRcon /** * @var ?resource + * + * @psalm-var null|resource|closed-resource */ private $rconSocket; @@ -76,7 +78,7 @@ final class SourceRcon extends AbstractRcon */ public function close(): void { - if ($this->rconSocket) { + if (is_resource($this->rconSocket)) { fclose($this->rconSocket); $this->rconSocket = null; @@ -173,7 +175,7 @@ final class SourceRcon extends AbstractRcon */ protected function read(): Buffer { - if (!$this->rconSocket) { + if (!is_resource($this->rconSocket)) { throw new InvalidPacketException('Rcon socket not open.'); } @@ -236,7 +238,7 @@ final class SourceRcon extends AbstractRcon */ protected function write(?int $header, string $string = ''): bool { - if (!$this->rconSocket) { + if (!is_resource($this->rconSocket)) { throw new InvalidPacketException('Rcon socket not open.'); } diff --git a/SourceQuery/Socket/AbstractSocket.php b/SourceQuery/Socket/AbstractSocket.php index f086f3b..32cacca 100644 --- a/SourceQuery/Socket/AbstractSocket.php +++ b/SourceQuery/Socket/AbstractSocket.php @@ -33,7 +33,9 @@ abstract class AbstractSocket implements SocketInterface public int $port = 0; /** - * @var resource|null + * @var ?resource + * + * @psalm-var null|resource|closed-resource */ public $socket; @@ -73,7 +75,7 @@ abstract class AbstractSocket implements SocketInterface */ public function getSocket() { - if (!$this->socket) { + if (!is_resource($this->socket)) { throw new InvalidArgumentException('Socket not open.'); } @@ -117,7 +119,7 @@ abstract class AbstractSocket implements SocketInterface */ public function close(): void { - if ($this->socket) { + if (is_resource($this->socket)) { fclose($this->socket); $this->socket = null; @@ -137,7 +139,7 @@ abstract class AbstractSocket implements SocketInterface */ public function read(int $length = 1400): Buffer { - if (!$this->socket) { + if (!is_resource($this->socket)) { throw new InvalidPacketException('Socket not open.'); } @@ -165,7 +167,7 @@ abstract class AbstractSocket implements SocketInterface */ public function write(int $header, string $string = ''): bool { - if (!$this->socket) { + if (!is_resource($this->socket)) { throw new InvalidPacketException('Socket not open.'); } @@ -185,7 +187,7 @@ abstract class AbstractSocket implements SocketInterface */ public function sherlock(Buffer $buffer, int $length): bool { - if (!$this->socket) { + if (!is_resource($this->socket)) { throw new InvalidPacketException('Socket not open.'); } diff --git a/SourceQuery/Socket/TestableSocket.php b/SourceQuery/Socket/TestableSocket.php index 25e9c79..5a9b2df 100644 --- a/SourceQuery/Socket/TestableSocket.php +++ b/SourceQuery/Socket/TestableSocket.php @@ -15,17 +15,15 @@ declare(strict_types=1); namespace xPaw\SourceQuery\Socket; -use SplQueue; -use SplDoublyLinkedList; use xPaw\SourceQuery\Buffer; use xPaw\SourceQuery\Exception\InvalidPacketException; final class TestableSocket extends AbstractSocket { /** - * @var SplQueue + * @var string[] */ - private SplQueue $packetQueue; + private array $packetQueue; /** * @var int @@ -39,8 +37,7 @@ final class TestableSocket extends AbstractSocket */ public function __construct(int $type) { - $this->packetQueue = new SplQueue(); - $this->packetQueue->setIteratorMode(SplDoublyLinkedList::IT_MODE_DELETE); + $this->packetQueue = []; $this->type = $type; } @@ -57,7 +54,7 @@ final class TestableSocket extends AbstractSocket */ public function queue(string $data): void { - $this->packetQueue->push($data); + $this->packetQueue[] = $data; } /** @@ -89,7 +86,14 @@ final class TestableSocket extends AbstractSocket public function read(int $length = 1400): Buffer { $buffer = new Buffer(); - $buffer->set($this->packetQueue->shift()); + + $packet = array_shift($this->packetQueue); + + if (!$packet) { + throw new InvalidPacketException('Empty packet'); + } + + $buffer->set($packet); $this->readInternal($buffer, $length, [ $this, 'sherlock' ]); @@ -117,11 +121,11 @@ final class TestableSocket extends AbstractSocket */ public function sherlock(Buffer $buffer, int $length): bool { - if ($this->packetQueue->isEmpty()) { + if (count($this->packetQueue) === 0) { return false; } - $buffer->set($this->packetQueue->shift()); + $buffer->set(array_shift($this->packetQueue)); return $buffer->getLong() === -2; }