1
0
mirror of https://github.com/xPaw/PHP-Source-Query.git synced 2026-05-18 14:43:33 +02:00

Add phpstan

This commit is contained in:
Pavel Djundik
2021-04-16 10:26:03 +03:00
parent 7a7e6b0d67
commit 890d98da92
13 changed files with 84 additions and 71 deletions
+6 -6
View File
@@ -25,7 +25,7 @@
*/
abstract class BaseSocket
{
/** @var resource */
/** @var ?resource */
public $Socket;
public int $Engine;
@@ -108,30 +108,30 @@
}
while( $ReadMore && $SherlockFunction( $Buffer, $Length ) );
$Data = Implode( $Packets );
$Data = implode( $Packets );
// TODO: Test this
if( $IsCompressed )
{
// Let's make sure this function exists, it's not included in PHP by default
if( !Function_Exists( 'bzdecompress' ) )
if( !function_exists( 'bzdecompress' ) )
{
throw new \RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' );
}
$Data = bzdecompress( $Data );
if( !is_string( $Data ) || CRC32( $Data ) !== $PacketChecksum )
if( !is_string( $Data ) || crc32( $Data ) !== $PacketChecksum )
{
throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH );
}
}
$Buffer->Set( SubStr( $Data, 4 ) );
$Buffer->Set( substr( $Data, 4 ) );
}
else
{
throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . DecHex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . dechex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
}
return $Buffer;
+8 -8
View File
@@ -44,7 +44,7 @@
public function Set( string $Buffer ) : void
{
$this->Buffer = $Buffer;
$this->Length = StrLen( $Buffer );
$this->Length = strlen( $Buffer );
$this->Position = 0;
}
@@ -81,7 +81,7 @@
return '';
}
$Data = SubStr( $this->Buffer, $this->Position, $Length );
$Data = substr( $this->Buffer, $this->Position, $Length );
$this->Position += $Length;
@@ -93,7 +93,7 @@
*/
public function GetByte( ) : int
{
return Ord( $this->Get( 1 ) );
return ord( $this->Get( 1 ) );
}
/**
@@ -106,7 +106,7 @@
throw new InvalidPacketException( 'Not enough data to unpack a short.', InvalidPacketException::BUFFER_EMPTY );
}
$Data = UnPack( 'v', $this->Get( 2 ) );
$Data = unpack( 'v', $this->Get( 2 ) );
return (int)$Data[ 1 ];
}
@@ -121,7 +121,7 @@
throw new InvalidPacketException( 'Not enough data to unpack a long.', InvalidPacketException::BUFFER_EMPTY );
}
$Data = UnPack( 'l', $this->Get( 4 ) );
$Data = unpack( 'l', $this->Get( 4 ) );
return (int)$Data[ 1 ];
}
@@ -136,7 +136,7 @@
throw new InvalidPacketException( 'Not enough data to unpack a float.', InvalidPacketException::BUFFER_EMPTY );
}
$Data = UnPack( 'f', $this->Get( 4 ) );
$Data = unpack( 'f', $this->Get( 4 ) );
return (float)$Data[ 1 ];
}
@@ -151,7 +151,7 @@
throw new InvalidPacketException( 'Not enough data to unpack an usigned long.', InvalidPacketException::BUFFER_EMPTY );
}
$Data = UnPack( 'V', $this->Get( 4 ) );
$Data = unpack( 'V', $this->Get( 4 ) );
return (int)$Data[ 1 ];
}
@@ -161,7 +161,7 @@
*/
public function GetString( ) : string
{
$ZeroBytePosition = StrPos( $this->Buffer, "\0", $this->Position );
$ZeroBytePosition = strpos( $this->Buffer, "\0", $this->Position );
if( $ZeroBytePosition === false )
{
+5 -5
View File
@@ -53,10 +53,10 @@
public function Write( int $Header, string $String = '' ) : bool
{
$Command = Pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String );
$Length = StrLen( $Command );
$Command = pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String );
$Length = strlen( $Command );
return $Length === FWrite( $this->Socket->Socket, $Command, $Length );
return $Length === fwrite( $this->Socket->Socket, $Command, $Length );
}
/**
@@ -89,7 +89,7 @@
//$StringBuffer .= SubStr( $Packet, 0, -2 );
// Let's assume if this packet is not long enough, there are no more after this one
$ReadMore = StrLen( $Packet ) > 1000; // use 1300?
$ReadMore = strlen( $Packet ) > 1000; // use 1300?
if( $ReadMore )
{
@@ -140,6 +140,6 @@
throw new AuthenticationException( 'Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD );
}
$this->RconChallenge = Trim( $Buffer->Get( ) );
$this->RconChallenge = trim( $Buffer->Get( ) );
}
}
+10 -10
View File
@@ -29,7 +29,7 @@
{
if( $this->Socket !== null )
{
FClose( $this->Socket );
fclose( $this->Socket );
$this->Socket = null;
}
@@ -42,7 +42,7 @@
$this->Port = $Port;
$this->Address = $Address;
$Socket = @FSockOpen( 'udp://' . $Address, $Port, $ErrNo, $ErrStr, $Timeout );
$Socket = @fsockopen( 'udp://' . $Address, $Port, $ErrNo, $ErrStr, $Timeout );
if( $ErrNo || $Socket === false )
{
@@ -50,16 +50,16 @@
}
$this->Socket = $Socket;
Stream_Set_Timeout( $this->Socket, $Timeout );
Stream_Set_Blocking( $this->Socket, true );
stream_set_timeout( $this->Socket, $Timeout );
stream_set_blocking( $this->Socket, true );
}
public function Write( int $Header, string $String = '' ) : bool
{
$Command = Pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String );
$Length = StrLen( $Command );
$Command = pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String );
$Length = strlen( $Command );
return $Length === FWrite( $this->Socket, $Command, $Length );
return $Length === fwrite( $this->Socket, $Command, $Length );
}
/**
@@ -72,7 +72,7 @@
public function Read( int $Length = 1400 ) : Buffer
{
$Buffer = new Buffer( );
$Buffer->Set( FRead( $this->Socket, $Length ) );
$Buffer->Set( fread( $this->Socket, $Length ) );
$this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] );
@@ -81,9 +81,9 @@
public function Sherlock( Buffer $Buffer, int $Length ) : bool
{
$Data = FRead( $this->Socket, $Length );
$Data = fread( $this->Socket, $Length );
if( StrLen( $Data ) < 4 )
if( strlen( $Data ) < 4 )
{
return false;
}
+10 -10
View File
@@ -240,8 +240,8 @@
$Server[ 'Players' ] = $Buffer->GetByte( );
$Server[ 'MaxPlayers' ] = $Buffer->GetByte( );
$Server[ 'Protocol' ] = $Buffer->GetByte( );
$Server[ 'Dedicated' ] = Chr( $Buffer->GetByte( ) );
$Server[ 'Os' ] = Chr( $Buffer->GetByte( ) );
$Server[ 'Dedicated' ] = chr( $Buffer->GetByte( ) );
$Server[ 'Os' ] = chr( $Buffer->GetByte( ) );
$Server[ 'Password' ] = $Buffer->GetByte( ) === 1;
$Server[ 'IsMod' ] = $Buffer->GetByte( ) === 1;
@@ -266,7 +266,7 @@
if( $Type !== self::S2A_INFO_SRC )
{
throw new InvalidPacketException( 'GetInfo: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
throw new InvalidPacketException( 'GetInfo: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
}
$Server[ 'Protocol' ] = $Buffer->GetByte( );
@@ -278,8 +278,8 @@
$Server[ 'Players' ] = $Buffer->GetByte( );
$Server[ 'MaxPlayers' ] = $Buffer->GetByte( );
$Server[ 'Bots' ] = $Buffer->GetByte( );
$Server[ 'Dedicated' ] = Chr( $Buffer->GetByte( ) );
$Server[ 'Os' ] = Chr( $Buffer->GetByte( ) );
$Server[ 'Dedicated' ] = chr( $Buffer->GetByte( ) );
$Server[ 'Os' ] = chr( $Buffer->GetByte( ) );
$Server[ 'Password' ] = $Buffer->GetByte( ) === 1;
$Server[ 'Secure' ] = $Buffer->GetByte( ) === 1;
@@ -390,7 +390,7 @@
if( $Type !== self::S2A_PLAYER )
{
throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
}
$Players = [];
@@ -403,7 +403,7 @@
$Player[ 'Name' ] = $Buffer->GetString( );
$Player[ 'Frags' ] = $Buffer->GetLong( );
$Player[ 'Time' ] = (int)$Buffer->GetFloat( );
$Player[ 'TimeF' ] = GMDate( ( $Player[ 'Time' ] > 3600 ? "H:i:s" : "i:s" ), $Player[ 'Time' ] );
$Player[ 'TimeF' ] = gmdate( ( $Player[ 'Time' ] > 3600 ? 'H:i:s' : 'i:s' ), $Player[ 'Time' ] );
$Players[ ] = $Player;
}
@@ -435,7 +435,7 @@
if( $Type !== self::S2A_RULES )
{
throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
}
$Rules = [];
@@ -446,7 +446,7 @@
$Rule = $Buffer->GetString( );
$Value = $Buffer->GetString( );
if( !Empty( $Rule ) )
if( !empty( $Rule ) )
{
$Rules[ $Rule ] = $Value;
}
@@ -497,7 +497,7 @@
}
default:
{
throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
}
}
}
+15 -17
View File
@@ -32,7 +32,7 @@
*/
private BaseSocket $Socket;
/** @var resource */
/** @var ?resource */
private $RconSocket;
private int $RconRequestId = 0;
@@ -45,7 +45,7 @@
{
if( $this->RconSocket )
{
FClose( $this->RconSocket );
fclose( $this->RconSocket );
$this->RconSocket = null;
}
@@ -57,7 +57,7 @@
{
if( !$this->RconSocket )
{
$RconSocket = @FSockOpen( $this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
$RconSocket = @fsockopen( $this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
if( $ErrNo || !$RconSocket )
{
@@ -65,27 +65,27 @@
}
$this->RconSocket = $RconSocket;
Stream_Set_Timeout( $this->RconSocket, $this->Socket->Timeout );
Stream_Set_Blocking( $this->RconSocket, true );
stream_set_timeout( $this->RconSocket, $this->Socket->Timeout );
stream_set_blocking( $this->RconSocket, true );
}
}
public function Write( int $Header, string $String = '' ) : bool
{
// Pack the packet together
$Command = Pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00";
$Command = pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00";
// Prepend packet length
$Command = Pack( 'V', StrLen( $Command ) ) . $Command;
$Length = StrLen( $Command );
$Command = pack( 'V', strlen( $Command ) ) . $Command;
$Length = strlen( $Command );
return $Length === FWrite( $this->RconSocket, $Command, $Length );
return $Length === fwrite( $this->RconSocket, $Command, $Length );
}
public function Read( ) : Buffer
{
$Buffer = new Buffer( );
$Buffer->Set( FRead( $this->RconSocket, 4 ) );
$Buffer->Set( fread( $this->RconSocket, 4 ) );
if( $Buffer->Remaining( ) < 4 )
{
@@ -94,23 +94,21 @@
$PacketSize = $Buffer->GetLong( );
$Buffer->Set( FRead( $this->RconSocket, $PacketSize ) );
$Buffer->Set( fread( $this->RconSocket, $PacketSize ) );
$Data = $Buffer->Get( );
$Remaining = $PacketSize - StrLen( $Data );
$Remaining = $PacketSize - strlen( $Data );
while( $Remaining > 0 )
{
$Data2 = FRead( $this->RconSocket, $Remaining );
$Data2 = fread( $this->RconSocket, $Remaining );
$PacketSize = StrLen( $Data2 );
$PacketSize = strlen( $Data2 );
if( $PacketSize === 0 )
{
throw new InvalidPacketException( 'Read ' . strlen( $Data ) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY );
break;
}
$Data .= $Data2;
@@ -144,7 +142,7 @@
// We do this stupid hack to handle split packets
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses
if( StrLen( $Data ) >= 4000 )
if( strlen( $Data ) >= 4000 )
{
$this->Write( SourceQuery::SERVERDATA_REQUESTVALUE );