1
0
mirror of https://github.com/xPaw/PHP-Source-Query.git synced 2026-06-11 00:13:14 +02:00

Psalm fixes around resource handling.

This commit is contained in:
Anthony Birkett
2021-05-31 13:05:53 +01:00
parent efa37503bf
commit 48b89017c4
3 changed files with 27 additions and 19 deletions
+5 -3
View File
@@ -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.');
}
+8 -6
View File
@@ -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.');
}
+14 -10
View File
@@ -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<string>
* @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;
}