Skip to content

Commit 914ec4b

Browse files
committed
Add more type info
1 parent b87c9fa commit 914ec4b

File tree

7 files changed

+107
-145
lines changed

7 files changed

+107
-145
lines changed

SourceQuery/BaseSocket.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,25 @@
2525
*/
2626
abstract class BaseSocket
2727
{
28+
/** @var resource */
2829
public $Socket;
29-
public $Engine;
30+
public int $Engine;
3031

31-
public $Address;
32-
public $Port;
33-
public $Timeout;
32+
public string $Address;
33+
public int $Port;
34+
public int $Timeout;
3435

3536
public function __destruct( )
3637
{
3738
$this->Close( );
3839
}
3940

40-
abstract public function Close( );
41-
abstract public function Open( $Address, $Port, $Timeout, $Engine );
42-
abstract public function Write( $Header, $String = '' );
43-
abstract public function Read( $Length = 1400 );
41+
abstract public function Close( ) : void;
42+
abstract public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void;
43+
abstract public function Write( int $Header, string $String = '' ) : bool;
44+
abstract public function Read( int $Length = 1400 ) : Buffer;
4445

45-
protected function ReadInternal( $Buffer, $Length, $SherlockFunction )
46+
protected function ReadInternal( Buffer $Buffer, int $Length, callable $SherlockFunction ) : Buffer
4647
{
4748
if( $Buffer->Remaining( ) === 0 )
4849
{

SourceQuery/Buffer.php

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,23 @@ class Buffer
2525
{
2626
/**
2727
* Buffer
28-
*
29-
* @var string
3028
*/
31-
private $Buffer;
29+
private string $Buffer = '';
3230

3331
/**
3432
* Buffer length
35-
*
36-
* @var int
3733
*/
38-
private $Length;
34+
private int $Length = 0;
3935

4036
/**
4137
* Current position in buffer
42-
*
43-
* @var int
4438
*/
45-
private $Position;
39+
private int $Position = 0;
4640

4741
/**
4842
* Sets buffer
49-
*
50-
* @param string $Buffer Buffer
5143
*/
52-
public function Set( $Buffer )
44+
public function Set( string $Buffer ) : void
5345
{
5446
$this->Buffer = $Buffer;
5547
$this->Length = StrLen( $Buffer );
@@ -61,7 +53,7 @@ public function Set( $Buffer )
6153
*
6254
* @return int Remaining bytes in buffer
6355
*/
64-
public function Remaining( )
56+
public function Remaining( ) : int
6557
{
6658
return $this->Length - $this->Position;
6759
}
@@ -70,10 +62,8 @@ public function Remaining( )
7062
* Gets data from buffer
7163
*
7264
* @param int $Length Bytes to read
73-
*
74-
* @return string
7565
*/
76-
public function Get( $Length = -1 )
66+
public function Get( int $Length = -1 ) : string
7767
{
7868
if( $Length === 0 )
7969
{
@@ -100,20 +90,16 @@ public function Get( $Length = -1 )
10090

10191
/**
10292
* Get byte from buffer
103-
*
104-
* @return int
10593
*/
106-
public function GetByte( )
94+
public function GetByte( ) : int
10795
{
10896
return Ord( $this->Get( 1 ) );
10997
}
11098

11199
/**
112100
* Get short from buffer
113-
*
114-
* @return int
115101
*/
116-
public function GetShort( )
102+
public function GetShort( ) : int
117103
{
118104
if( $this->Remaining( ) < 2 )
119105
{
@@ -122,15 +108,13 @@ public function GetShort( )
122108

123109
$Data = UnPack( 'v', $this->Get( 2 ) );
124110

125-
return $Data[ 1 ];
111+
return (int)$Data[ 1 ];
126112
}
127113

128114
/**
129115
* Get long from buffer
130-
*
131-
* @return int
132116
*/
133-
public function GetLong( )
117+
public function GetLong( ) : int
134118
{
135119
if( $this->Remaining( ) < 4 )
136120
{
@@ -139,15 +123,13 @@ public function GetLong( )
139123

140124
$Data = UnPack( 'l', $this->Get( 4 ) );
141125

142-
return $Data[ 1 ];
126+
return (int)$Data[ 1 ];
143127
}
144128

145129
/**
146130
* Get float from buffer
147-
*
148-
* @return float
149131
*/
150-
public function GetFloat( )
132+
public function GetFloat( ) : float
151133
{
152134
if( $this->Remaining( ) < 4 )
153135
{
@@ -156,15 +138,13 @@ public function GetFloat( )
156138

157139
$Data = UnPack( 'f', $this->Get( 4 ) );
158140

159-
return $Data[ 1 ];
141+
return (float)$Data[ 1 ];
160142
}
161143

162144
/**
163145
* Get unsigned long from buffer
164-
*
165-
* @return int
166146
*/
167-
public function GetUnsignedLong( )
147+
public function GetUnsignedLong( ) : int
168148
{
169149
if( $this->Remaining( ) < 4 )
170150
{
@@ -173,15 +153,13 @@ public function GetUnsignedLong( )
173153

174154
$Data = UnPack( 'V', $this->Get( 4 ) );
175155

176-
return $Data[ 1 ];
156+
return (int)$Data[ 1 ];
177157
}
178158

179159
/**
180160
* Read one string from buffer ending with null byte
181-
*
182-
* @return string
183161
*/
184-
public function GetString( )
162+
public function GetString( ) : string
185163
{
186164
$ZeroBytePosition = StrPos( $this->Buffer, "\0", $this->Position );
187165

SourceQuery/GoldSourceRcon.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,30 @@ class GoldSourceRcon
2828
/**
2929
* Points to socket class
3030
*
31-
* @var Socket
31+
* @var BaseSocket
3232
*/
3333
private $Socket;
3434

35-
private $RconPassword;
36-
private $RconChallenge;
35+
private string $RconPassword = '';
36+
private string $RconChallenge = '';
3737

38-
public function __construct( $Socket )
38+
public function __construct( BaseSocket $Socket )
3939
{
4040
$this->Socket = $Socket;
4141
}
4242

43-
public function Close( )
43+
public function Close( ) : void
4444
{
45-
$this->RconChallenge = 0;
46-
$this->RconPassword = 0;
45+
$this->RconChallenge = '';
46+
$this->RconPassword = '';
4747
}
4848

49-
public function Open( )
49+
public function Open( ) : void
5050
{
5151
//
5252
}
5353

54-
public function Write( $Header, $String = '' )
54+
public function Write( int $Header, string $String = '' ) : bool
5555
{
5656
$Command = Pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String );
5757
$Length = StrLen( $Command );
@@ -64,7 +64,7 @@ public function Write( $Header, $String = '' )
6464
* @throws AuthenticationException
6565
* @return Buffer
6666
*/
67-
public function Read( $Length = 1400 )
67+
public function Read( int $Length = 1400 ) : Buffer
6868
{
6969
// GoldSource RCON has same structure as Query
7070
$Buffer = $this->Socket->Read( );
@@ -115,7 +115,7 @@ public function Read( $Length = 1400 )
115115
return $Buffer;
116116
}
117117

118-
public function Command( $Command )
118+
public function Command( string $Command ) : string
119119
{
120120
if( !$this->RconChallenge )
121121
{
@@ -128,7 +128,7 @@ public function Command( $Command )
128128
return $Buffer->Get( );
129129
}
130130

131-
public function Authorize( $Password )
131+
public function Authorize( string $Password ) : void
132132
{
133133
$this->RconPassword = $Password;
134134

SourceQuery/Socket.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@
2525
*/
2626
class Socket extends BaseSocket
2727
{
28-
public function Close( )
28+
public function Close( ) : void
2929
{
30-
if( $this->Socket )
30+
if( $this->Socket !== null )
3131
{
3232
FClose( $this->Socket );
3333

3434
$this->Socket = null;
3535
}
3636
}
3737

38-
public function Open( $Address, $Port, $Timeout, $Engine )
38+
public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void
3939
{
4040
$this->Timeout = $Timeout;
4141
$this->Engine = $Engine;
@@ -53,7 +53,7 @@ public function Open( $Address, $Port, $Timeout, $Engine )
5353
Stream_Set_Blocking( $this->Socket, true );
5454
}
5555

56-
public function Write( $Header, $String = '' )
56+
public function Write( int $Header, string $String = '' ) : bool
5757
{
5858
$Command = Pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String );
5959
$Length = StrLen( $Command );
@@ -68,7 +68,7 @@ public function Write( $Header, $String = '' )
6868
*
6969
* @return Buffer Buffer
7070
*/
71-
public function Read( $Length = 1400 )
71+
public function Read( int $Length = 1400 ) : Buffer
7272
{
7373
$Buffer = new Buffer( );
7474
$Buffer->Set( FRead( $this->Socket, $Length ) );
@@ -78,7 +78,7 @@ public function Read( $Length = 1400 )
7878
return $Buffer;
7979
}
8080

81-
public function Sherlock( $Buffer, $Length )
81+
public function Sherlock( Buffer $Buffer, int $Length ) : bool
8282
{
8383
$Data = FRead( $this->Socket, $Length );
8484

0 commit comments

Comments
 (0)