diff --git a/SourceQuery/BaseSocket.php b/SourceQuery/BaseSocket.php index ecc6531..33fa9c4 100644 --- a/SourceQuery/BaseSocket.php +++ b/SourceQuery/BaseSocket.php @@ -25,24 +25,25 @@ */ abstract class BaseSocket { + /** @var resource */ public $Socket; - public $Engine; + public int $Engine; - public $Address; - public $Port; - public $Timeout; + public string $Address; + public int $Port; + public int $Timeout; public function __destruct( ) { $this->Close( ); } - abstract public function Close( ); - abstract public function Open( $Address, $Port, $Timeout, $Engine ); - abstract public function Write( $Header, $String = '' ); - abstract public function Read( $Length = 1400 ); + abstract public function Close( ) : void; + abstract public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void; + abstract public function Write( int $Header, string $String = '' ) : bool; + abstract public function Read( int $Length = 1400 ) : Buffer; - protected function ReadInternal( $Buffer, $Length, $SherlockFunction ) + protected function ReadInternal( Buffer $Buffer, int $Length, callable $SherlockFunction ) : Buffer { if( $Buffer->Remaining( ) === 0 ) { diff --git a/SourceQuery/Buffer.php b/SourceQuery/Buffer.php index 6ff7668..6112016 100644 --- a/SourceQuery/Buffer.php +++ b/SourceQuery/Buffer.php @@ -25,31 +25,23 @@ { /** * Buffer - * - * @var string */ - private $Buffer; + private string $Buffer = ''; /** * Buffer length - * - * @var int */ - private $Length; + private int $Length = 0; /** * Current position in buffer - * - * @var int */ - private $Position; + private int $Position = 0; /** * Sets buffer - * - * @param string $Buffer Buffer */ - public function Set( $Buffer ) + public function Set( string $Buffer ) : void { $this->Buffer = $Buffer; $this->Length = StrLen( $Buffer ); @@ -61,7 +53,7 @@ * * @return int Remaining bytes in buffer */ - public function Remaining( ) + public function Remaining( ) : int { return $this->Length - $this->Position; } @@ -70,10 +62,8 @@ * Gets data from buffer * * @param int $Length Bytes to read - * - * @return string */ - public function Get( $Length = -1 ) + public function Get( int $Length = -1 ) : string { if( $Length === 0 ) { @@ -100,20 +90,16 @@ /** * Get byte from buffer - * - * @return int */ - public function GetByte( ) + public function GetByte( ) : int { return Ord( $this->Get( 1 ) ); } /** * Get short from buffer - * - * @return int */ - public function GetShort( ) + public function GetShort( ) : int { if( $this->Remaining( ) < 2 ) { @@ -122,15 +108,13 @@ $Data = UnPack( 'v', $this->Get( 2 ) ); - return $Data[ 1 ]; + return (int)$Data[ 1 ]; } /** * Get long from buffer - * - * @return int */ - public function GetLong( ) + public function GetLong( ) : int { if( $this->Remaining( ) < 4 ) { @@ -139,15 +123,13 @@ $Data = UnPack( 'l', $this->Get( 4 ) ); - return $Data[ 1 ]; + return (int)$Data[ 1 ]; } /** * Get float from buffer - * - * @return float */ - public function GetFloat( ) + public function GetFloat( ) : float { if( $this->Remaining( ) < 4 ) { @@ -156,15 +138,13 @@ $Data = UnPack( 'f', $this->Get( 4 ) ); - return $Data[ 1 ]; + return (float)$Data[ 1 ]; } /** * Get unsigned long from buffer - * - * @return int */ - public function GetUnsignedLong( ) + public function GetUnsignedLong( ) : int { if( $this->Remaining( ) < 4 ) { @@ -173,15 +153,13 @@ $Data = UnPack( 'V', $this->Get( 4 ) ); - return $Data[ 1 ]; + return (int)$Data[ 1 ]; } /** * Read one string from buffer ending with null byte - * - * @return string */ - public function GetString( ) + public function GetString( ) : string { $ZeroBytePosition = StrPos( $this->Buffer, "\0", $this->Position ); diff --git a/SourceQuery/GoldSourceRcon.php b/SourceQuery/GoldSourceRcon.php index 4665b5b..ceb7270 100644 --- a/SourceQuery/GoldSourceRcon.php +++ b/SourceQuery/GoldSourceRcon.php @@ -28,30 +28,30 @@ /** * Points to socket class * - * @var Socket + * @var BaseSocket */ private $Socket; - private $RconPassword; - private $RconChallenge; + private string $RconPassword = ''; + private string $RconChallenge = ''; - public function __construct( $Socket ) + public function __construct( BaseSocket $Socket ) { $this->Socket = $Socket; } - public function Close( ) + public function Close( ) : void { - $this->RconChallenge = 0; - $this->RconPassword = 0; + $this->RconChallenge = ''; + $this->RconPassword = ''; } - public function Open( ) + public function Open( ) : void { // } - public function Write( $Header, $String = '' ) + public function Write( int $Header, string $String = '' ) : bool { $Command = Pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String ); $Length = StrLen( $Command ); @@ -64,7 +64,7 @@ * @throws AuthenticationException * @return Buffer */ - public function Read( $Length = 1400 ) + public function Read( int $Length = 1400 ) : Buffer { // GoldSource RCON has same structure as Query $Buffer = $this->Socket->Read( ); @@ -115,7 +115,7 @@ return $Buffer; } - public function Command( $Command ) + public function Command( string $Command ) : string { if( !$this->RconChallenge ) { @@ -128,7 +128,7 @@ return $Buffer->Get( ); } - public function Authorize( $Password ) + public function Authorize( string $Password ) : void { $this->RconPassword = $Password; diff --git a/SourceQuery/Socket.php b/SourceQuery/Socket.php index 1f21d0d..4f5cf9f 100644 --- a/SourceQuery/Socket.php +++ b/SourceQuery/Socket.php @@ -25,9 +25,9 @@ */ class Socket extends BaseSocket { - public function Close( ) + public function Close( ) : void { - if( $this->Socket ) + if( $this->Socket !== null ) { FClose( $this->Socket ); @@ -35,7 +35,7 @@ } } - public function Open( $Address, $Port, $Timeout, $Engine ) + public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void { $this->Timeout = $Timeout; $this->Engine = $Engine; @@ -53,7 +53,7 @@ Stream_Set_Blocking( $this->Socket, true ); } - public function Write( $Header, $String = '' ) + public function Write( int $Header, string $String = '' ) : bool { $Command = Pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String ); $Length = StrLen( $Command ); @@ -68,7 +68,7 @@ * * @return Buffer Buffer */ - public function Read( $Length = 1400 ) + public function Read( int $Length = 1400 ) : Buffer { $Buffer = new Buffer( ); $Buffer->Set( FRead( $this->Socket, $Length ) ); @@ -78,7 +78,7 @@ return $Buffer; } - public function Sherlock( $Buffer, $Length ) + public function Sherlock( Buffer $Buffer, int $Length ) : bool { $Data = FRead( $this->Socket, $Length ); diff --git a/SourceQuery/SourceQuery.php b/SourceQuery/SourceQuery.php index 8daa12b..ecc9757 100644 --- a/SourceQuery/SourceQuery.php +++ b/SourceQuery/SourceQuery.php @@ -76,31 +76,23 @@ /** * Points to socket class - * - * @var BaseSocket */ - private $Socket; + private BaseSocket $Socket; /** * True if connection is open, false if not - * - * @var bool */ - private $Connected; + private bool $Connected = false; /** * Contains challenge - * - * @var string */ - private $Challenge; + private string $Challenge = ''; /** * Use old method for getting challenge number - * - * @var bool */ - private $UseOldGetChallengeMethod; + private bool $UseOldGetChallengeMethod = false; public function __construct( BaseSocket $Socket = null ) { @@ -123,16 +115,16 @@ * @throws InvalidArgumentException * @throws SocketException */ - public function Connect( $Address, $Port, $Timeout = 3, $Engine = self::SOURCE ) + public function Connect( string $Address, int $Port, int $Timeout = 3, int $Engine = self::SOURCE ) : void { $this->Disconnect( ); - if( !is_int( $Timeout ) || $Timeout < 0 ) + if( $Timeout < 0 ) { - throw new InvalidArgumentException( 'Timeout must be an integer.', InvalidArgumentException::TIMEOUT_NOT_INTEGER ); + throw new InvalidArgumentException( 'Timeout must be a positive integer.', InvalidArgumentException::TIMEOUT_NOT_INTEGER ); } - $this->Socket->Open( $Address, (int)$Port, $Timeout, (int)$Engine ); + $this->Socket->Open( $Address, $Port, $Timeout, $Engine ); $this->Connected = true; } @@ -144,7 +136,7 @@ * * @returns bool Previous value */ - public function SetUseOldGetChallengeMethod( $Value ) + public function SetUseOldGetChallengeMethod( bool $Value ) : bool { $Previous = $this->UseOldGetChallengeMethod; @@ -156,7 +148,7 @@ /** * Closes all open connections */ - public function Disconnect( ) + public function Disconnect( ) : void { $this->Connected = false; $this->Challenge = ''; @@ -180,7 +172,7 @@ * * @return bool True on success, false on failure */ - public function Ping( ) + public function Ping( ) : bool { if( !$this->Connected ) { @@ -201,7 +193,7 @@ * * @return array Returns an array with information on success */ - public function GetInfo( ) + public function GetInfo( ) : array { if( !$this->Connected ) { @@ -246,16 +238,12 @@ $Mod[ 'Size' ] = $Buffer->GetLong( ); $Mod[ 'ServerSide' ] = $Buffer->GetByte( ) === 1; $Mod[ 'CustomDLL' ] = $Buffer->GetByte( ) === 1; + $Server[ 'Mod' ] = $Mod; } $Server[ 'Secure' ] = $Buffer->GetByte( ) === 1; $Server[ 'Bots' ] = $Buffer->GetByte( ); - if( isset( $Mod ) ) - { - $Server[ 'Mod' ] = $Mod; - } - return $Server; } @@ -368,7 +356,7 @@ * * @return array Returns an array with players on success */ - public function GetPlayers( ) + public function GetPlayers( ) : array { if( !$this->Connected ) { @@ -414,7 +402,7 @@ * * @return array Returns an array with rules on success */ - public function GetRules( ) + public function GetRules( ) : array { if( !$this->Connected ) { @@ -453,12 +441,9 @@ /** * Get challenge (used for players/rules packets) * - * @param $Header - * @param $ExpectedResult - * * @throws InvalidPacketException */ - private function GetChallenge( $Header, $ExpectedResult ) + private function GetChallenge( int $Header, int $ExpectedResult ) : void { if( $this->Challenge ) { @@ -509,7 +494,7 @@ * @throws InvalidPacketException * @throws SocketException */ - public function SetRconPassword( $Password ) + public function SetRconPassword( string $Password ) : void { if( !$this->Connected ) { @@ -551,7 +536,7 @@ * * @return string Answer from server in string */ - public function Rcon( $Command ) + public function Rcon( string $Command ) : string { if( !$this->Connected ) { diff --git a/SourceQuery/SourceRcon.php b/SourceQuery/SourceRcon.php index a0a916a..06a2e51 100644 --- a/SourceQuery/SourceRcon.php +++ b/SourceQuery/SourceRcon.php @@ -29,20 +29,19 @@ { /** * Points to socket class - * - * @var Socket */ - private $Socket; + private BaseSocket $Socket; + /** @var resource */ private $RconSocket; - private $RconRequestId; + private int $RconRequestId = 0; - public function __construct( $Socket ) + public function __construct( BaseSocket $Socket ) { $this->Socket = $Socket; } - public function Close( ) + public function Close( ) : void { if( $this->RconSocket ) { @@ -54,7 +53,7 @@ $this->RconRequestId = 0; } - public function Open( ) + public function Open( ) : void { if( !$this->RconSocket ) { @@ -70,7 +69,7 @@ } } - public function Write( $Header, $String = '' ) + public function Write( int $Header, string $String = '' ) : bool { // Pack the packet together $Command = Pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00"; @@ -82,7 +81,7 @@ return $Length === FWrite( $this->RconSocket, $Command, $Length ); } - public function Read( ) + public function Read( ) : Buffer { $Buffer = new Buffer( ); $Buffer->Set( FRead( $this->RconSocket, 4 ) ); @@ -122,7 +121,7 @@ return $Buffer; } - public function Command( $Command ) + public function Command( string $Command ) : string { $this->Write( SourceQuery::SERVERDATA_EXECCOMMAND, $Command ); $Buffer = $this->Read( ); @@ -174,7 +173,7 @@ return rtrim( $Data, "\0" ); } - public function Authorize( $Password ) + public function Authorize( string $Password ) : void { $this->Write( SourceQuery::SERVERDATA_AUTH, $Password ); $Buffer = $this->Read( ); diff --git a/Tests/Tests.php b/Tests/Tests.php index 481f171..6c3401f 100644 --- a/Tests/Tests.php +++ b/Tests/Tests.php @@ -1,13 +1,12 @@ PacketQueue->push( $Data ); } - public function Close( ) + public function Close( ) : void { // } - public function Open( $Address, $Port, $Timeout, $Engine ) + public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void { $this->Timeout = $Timeout; $this->Engine = $Engine; @@ -34,29 +33,29 @@ $this->Address = $Address; } - public function Write( $Header, $String = '' ) + public function Write( int $Header, string $String = '' ) : bool { - // + return true; } - public function Read( $Length = 1400 ) + public function Read( int $Length = 1400 ) : Buffer { $Buffer = new Buffer( ); - $Buffer->Set( $this->PacketQueue->shift() ); + $Buffer->Set( (string)$this->PacketQueue->shift() ); $this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] ); return $Buffer; } - public function Sherlock( $Buffer, $Length ) + public function Sherlock( Buffer $Buffer, int $Length ) : bool { if( $this->PacketQueue->isEmpty() ) { return false; } - $Buffer->Set( $this->PacketQueue->shift() ); + $Buffer->Set( (string)$this->PacketQueue->shift() ); return $Buffer->GetLong( ) === -2; } @@ -64,17 +63,17 @@ class SourceQueryTests extends TestCase { - private $Socket; - private $SourceQuery; + private TestableSocket $Socket; + private SourceQuery $SourceQuery; - public function setUp() + public function setUp() : void { $this->Socket = new TestableSocket(); $this->SourceQuery = new SourceQuery( $this->Socket ); $this->SourceQuery->Connect( '', 2 ); } - public function tearDown() + public function tearDown() : void { $this->SourceQuery->Disconnect(); @@ -84,7 +83,7 @@ /** * @expectedException xPaw\SourceQuery\Exception\InvalidArgumentException */ - public function testInvalidTimeout() + public function testInvalidTimeout() : void { $SourceQuery = new SourceQuery( ); $SourceQuery->Connect( '', 2, -1 ); @@ -93,7 +92,7 @@ /** * @expectedException xPaw\SourceQuery\Exception\SocketException */ - public function testNotConnectedGetInfo() + public function testNotConnectedGetInfo() : void { $this->SourceQuery->Disconnect(); $this->SourceQuery->GetInfo(); @@ -102,7 +101,7 @@ /** * @expectedException xPaw\SourceQuery\Exception\SocketException */ - public function testNotConnectedPing() + public function testNotConnectedPing() : void { $this->SourceQuery->Disconnect(); $this->SourceQuery->Ping(); @@ -111,7 +110,7 @@ /** * @expectedException xPaw\SourceQuery\Exception\SocketException */ - public function testNotConnectedGetPlayers() + public function testNotConnectedGetPlayers() : void { $this->SourceQuery->Disconnect(); $this->SourceQuery->GetPlayers(); @@ -120,7 +119,7 @@ /** * @expectedException xPaw\SourceQuery\Exception\SocketException */ - public function testNotConnectedGetRules() + public function testNotConnectedGetRules() : void { $this->SourceQuery->Disconnect(); $this->SourceQuery->GetRules(); @@ -129,7 +128,7 @@ /** * @expectedException xPaw\SourceQuery\Exception\SocketException */ - public function testNotConnectedSetRconPassword() + public function testNotConnectedSetRconPassword() : void { $this->SourceQuery->Disconnect(); $this->SourceQuery->SetRconPassword('a'); @@ -138,7 +137,7 @@ /** * @expectedException xPaw\SourceQuery\Exception\SocketException */ - public function testNotConnectedRcon() + public function testNotConnectedRcon() : void { $this->SourceQuery->Disconnect(); $this->SourceQuery->Rcon('a'); @@ -147,7 +146,7 @@ /** * @expectedException xPaw\SourceQuery\Exception\SocketException */ - public function testRconWithoutPassword() + public function testRconWithoutPassword() : void { $this->SourceQuery->Rcon('a'); } @@ -155,7 +154,7 @@ /** * @dataProvider InfoProvider */ - public function testGetInfo( $RawInput, $ExpectedOutput ) + public function testGetInfo( string $RawInput, array $ExpectedOutput ) : void { if( isset( $ExpectedOutput[ 'IsMod' ] ) ) { @@ -169,7 +168,7 @@ $this->assertEquals( $ExpectedOutput, $RealOutput ); } - public function InfoProvider() + public function InfoProvider() : array { $DataProvider = []; @@ -191,7 +190,7 @@ * @expectedException xPaw\SourceQuery\Exception\InvalidPacketException * @dataProvider BadPacketProvider */ - public function testBadGetInfo( $Data ) + public function testBadGetInfo( string $Data ) : void { $this->Socket->Queue( $Data ); @@ -202,7 +201,7 @@ * @expectedException xPaw\SourceQuery\Exception\InvalidPacketException * @dataProvider BadPacketProvider */ - public function testBadGetChallengeViaPlayers( $Data ) + public function testBadGetChallengeViaPlayers( string $Data ) : void { $this->Socket->Queue( $Data ); @@ -213,7 +212,7 @@ * @expectedException xPaw\SourceQuery\Exception\InvalidPacketException * @dataProvider BadPacketProvider */ - public function testBadGetPlayersAfterCorrectChallenge( $Data ) + public function testBadGetPlayersAfterCorrectChallenge( string $Data ) : void { $this->Socket->Queue( "\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11" ); $this->Socket->Queue( $Data ); @@ -225,7 +224,7 @@ * @expectedException xPaw\SourceQuery\Exception\InvalidPacketException * @dataProvider BadPacketProvider */ - public function testBadGetRulesAfterCorrectChallenge( $Data ) + public function testBadGetRulesAfterCorrectChallenge( string $Data ) : void { $this->Socket->Queue( "\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11" ); $this->Socket->Queue( $Data ); @@ -233,7 +232,7 @@ $this->SourceQuery->GetRules(); } - public function BadPacketProvider( ) + public function BadPacketProvider( ) : array { return [ @@ -247,7 +246,7 @@ ]; } - public function testGetChallengeTwice( ) + public function testGetChallengeTwice( ) : void { $this->Socket->Queue( "\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11" ); $this->Socket->Queue( "\xFF\xFF\xFF\xFF\x45\x01\x00ayy\x00lmao\x00" ); @@ -260,7 +259,7 @@ /** * @dataProvider RulesProvider */ - public function testGetRules( $RawInput, $ExpectedOutput ) + public function testGetRules( array $RawInput, array $ExpectedOutput ) : void { $this->Socket->Queue( hex2bin( "ffffffff4104fce20e" ) ); // Challenge @@ -274,7 +273,7 @@ $this->assertEquals( $ExpectedOutput, $RealOutput ); } - public function RulesProvider() + public function RulesProvider() : array { $DataProvider = []; @@ -295,7 +294,7 @@ /** * @dataProvider PlayersProvider */ - public function testGetPlayers( $RawInput, $ExpectedOutput ) + public function testGetPlayers( array $RawInput, array $ExpectedOutput ) : void { $this->Socket->Queue( hex2bin( "ffffffff4104fce20e" ) ); // Challenge @@ -309,7 +308,7 @@ $this->assertEquals( $ExpectedOutput, $RealOutput ); } - public function PlayersProvider() + public function PlayersProvider() : array { $DataProvider = []; @@ -327,7 +326,7 @@ return $DataProvider; } - public function testPing() + public function testPing() : void { $this->Socket->Queue( "\xFF\xFF\xFF\xFF\x6A\x00"); $this->assertTrue( $this->SourceQuery->Ping() );