Psalm fixes around resource handling.

pull/150/head
Anthony Birkett 4 years ago
parent efa37503bf
commit 48b89017c4

@ -31,6 +31,8 @@ final class SourceRcon extends AbstractRcon
/** /**
* @var ?resource * @var ?resource
*
* @psalm-var null|resource|closed-resource
*/ */
private $rconSocket; private $rconSocket;
@ -76,7 +78,7 @@ final class SourceRcon extends AbstractRcon
*/ */
public function close(): void public function close(): void
{ {
if ($this->rconSocket) { if (is_resource($this->rconSocket)) {
fclose($this->rconSocket); fclose($this->rconSocket);
$this->rconSocket = null; $this->rconSocket = null;
@ -173,7 +175,7 @@ final class SourceRcon extends AbstractRcon
*/ */
protected function read(): Buffer protected function read(): Buffer
{ {
if (!$this->rconSocket) { if (!is_resource($this->rconSocket)) {
throw new InvalidPacketException('Rcon socket not open.'); throw new InvalidPacketException('Rcon socket not open.');
} }
@ -236,7 +238,7 @@ final class SourceRcon extends AbstractRcon
*/ */
protected function write(?int $header, string $string = ''): bool protected function write(?int $header, string $string = ''): bool
{ {
if (!$this->rconSocket) { if (!is_resource($this->rconSocket)) {
throw new InvalidPacketException('Rcon socket not open.'); throw new InvalidPacketException('Rcon socket not open.');
} }

@ -33,7 +33,9 @@ abstract class AbstractSocket implements SocketInterface
public int $port = 0; public int $port = 0;
/** /**
* @var resource|null * @var ?resource
*
* @psalm-var null|resource|closed-resource
*/ */
public $socket; public $socket;
@ -73,7 +75,7 @@ abstract class AbstractSocket implements SocketInterface
*/ */
public function getSocket() public function getSocket()
{ {
if (!$this->socket) { if (!is_resource($this->socket)) {
throw new InvalidArgumentException('Socket not open.'); throw new InvalidArgumentException('Socket not open.');
} }
@ -117,7 +119,7 @@ abstract class AbstractSocket implements SocketInterface
*/ */
public function close(): void public function close(): void
{ {
if ($this->socket) { if (is_resource($this->socket)) {
fclose($this->socket); fclose($this->socket);
$this->socket = null; $this->socket = null;
@ -137,7 +139,7 @@ abstract class AbstractSocket implements SocketInterface
*/ */
public function read(int $length = 1400): Buffer public function read(int $length = 1400): Buffer
{ {
if (!$this->socket) { if (!is_resource($this->socket)) {
throw new InvalidPacketException('Socket not open.'); throw new InvalidPacketException('Socket not open.');
} }
@ -165,7 +167,7 @@ abstract class AbstractSocket implements SocketInterface
*/ */
public function write(int $header, string $string = ''): bool public function write(int $header, string $string = ''): bool
{ {
if (!$this->socket) { if (!is_resource($this->socket)) {
throw new InvalidPacketException('Socket not open.'); throw new InvalidPacketException('Socket not open.');
} }
@ -185,7 +187,7 @@ abstract class AbstractSocket implements SocketInterface
*/ */
public function sherlock(Buffer $buffer, int $length): bool public function sherlock(Buffer $buffer, int $length): bool
{ {
if (!$this->socket) { if (!is_resource($this->socket)) {
throw new InvalidPacketException('Socket not open.'); throw new InvalidPacketException('Socket not open.');
} }

@ -15,17 +15,15 @@ declare(strict_types=1);
namespace xPaw\SourceQuery\Socket; namespace xPaw\SourceQuery\Socket;
use SplQueue;
use SplDoublyLinkedList;
use xPaw\SourceQuery\Buffer; use xPaw\SourceQuery\Buffer;
use xPaw\SourceQuery\Exception\InvalidPacketException; use xPaw\SourceQuery\Exception\InvalidPacketException;
final class TestableSocket extends AbstractSocket final class TestableSocket extends AbstractSocket
{ {
/** /**
* @var SplQueue<string> * @var string[]
*/ */
private SplQueue $packetQueue; private array $packetQueue;
/** /**
* @var int * @var int
@ -39,8 +37,7 @@ final class TestableSocket extends AbstractSocket
*/ */
public function __construct(int $type) public function __construct(int $type)
{ {
$this->packetQueue = new SplQueue(); $this->packetQueue = [];
$this->packetQueue->setIteratorMode(SplDoublyLinkedList::IT_MODE_DELETE);
$this->type = $type; $this->type = $type;
} }
@ -57,7 +54,7 @@ final class TestableSocket extends AbstractSocket
*/ */
public function queue(string $data): void 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 public function read(int $length = 1400): Buffer
{ {
$buffer = new 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' ]); $this->readInternal($buffer, $length, [ $this, 'sherlock' ]);
@ -117,11 +121,11 @@ final class TestableSocket extends AbstractSocket
*/ */
public function sherlock(Buffer $buffer, int $length): bool public function sherlock(Buffer $buffer, int $length): bool
{ {
if ($this->packetQueue->isEmpty()) { if (count($this->packetQueue) === 0) {
return false; return false;
} }
$buffer->set($this->packetQueue->shift()); $buffer->set(array_shift($this->packetQueue));
return $buffer->getLong() === -2; return $buffer->getLong() === -2;
} }

Loading…
Cancel
Save