Skip to content

Commit 890d98d

Browse files
committed
Add phpstan
1 parent 7a7e6b0 commit 890d98d

File tree

13 files changed

+84
-71
lines changed

13 files changed

+84
-71
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ jobs:
1414
run: composer install --no-interaction --no-progress
1515
- name: Run tests
1616
run: php${{ matrix.php }} vendor/bin/phpunit --configuration Tests/phpunit.xml --verbose --fail-on-warning
17+
- name: Run phpstan
18+
run: php${{ matrix.php }} vendor/bin/phpstan
1719
- name: Run psalm
1820
run: php${{ matrix.php }} vendor/bin/psalm

Examples/Example.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
use xPaw\SourceQuery\SourceQuery;
55

66
// For the sake of this example
7-
Header( 'Content-Type: text/plain' );
8-
Header( 'X-Content-Type-Options: nosniff' );
7+
header( 'Content-Type: text/plain' );
8+
header( 'X-Content-Type-Options: nosniff' );
99

1010
// Edit this ->
1111
define( 'SQ_SERVER_ADDR', 'localhost' );

Examples/RconExample.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
use xPaw\SourceQuery\SourceQuery;
55

66
// For the sake of this example
7-
Header( 'Content-Type: text/plain' );
8-
Header( 'X-Content-Type-Options: nosniff' );
7+
header( 'Content-Type: text/plain' );
8+
header( 'X-Content-Type-Options: nosniff' );
99

1010
// Edit this ->
1111
define( 'SQ_SERVER_ADDR', 'localhost' );

Examples/View.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
define( 'SQ_ENGINE', SourceQuery::SOURCE );
1111
// Edit this <-
1212

13-
$Timer = MicroTime( true );
13+
$Timer = microtime( true );
1414

1515
$Query = new SourceQuery( );
1616

17-
$Info = Array( );
18-
$Rules = Array( );
19-
$Players = Array( );
17+
$Info = [];
18+
$Rules = [];
19+
$Players = [];
2020
$Exception = null;
2121

2222
try
@@ -37,7 +37,7 @@
3737
$Query->Disconnect( );
3838
}
3939

40-
$Timer = Number_Format( MicroTime( true ) - $Timer, 4, '.', '' );
40+
$Timer = number_format( microtime( true ) - $Timer, 4, '.', '' );
4141
?>
4242
<!DOCTYPE html>
4343
<html>
@@ -103,12 +103,12 @@
103103
</tr>
104104
</thead>
105105
<tbody>
106-
<?php if( Is_Array( $Info ) ): ?>
106+
<?php if( !empty( $Info ) ): ?>
107107
<?php foreach( $Info as $InfoKey => $InfoValue ): ?>
108108
<tr>
109109
<td><?php echo htmlspecialchars( $InfoKey ); ?></td>
110110
<td><?php
111-
if( Is_Array( $InfoValue ) )
111+
if( is_array( $InfoValue ) )
112112
{
113113
echo "<pre>";
114114
print_r( $InfoValue );
@@ -176,7 +176,7 @@
176176
</tr>
177177
</thead>
178178
<tbody>
179-
<?php if( Is_Array( $Rules ) ): ?>
179+
<?php if( !empty( $Rules ) ): ?>
180180
<?php foreach( $Rules as $Rule => $Value ): ?>
181181
<tr>
182182
<td><?php echo htmlspecialchars( $Rule ); ?></td>

SourceQuery/BaseSocket.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
abstract class BaseSocket
2727
{
28-
/** @var resource */
28+
/** @var ?resource */
2929
public $Socket;
3030
public int $Engine;
3131

@@ -108,30 +108,30 @@ protected function ReadInternal( Buffer $Buffer, int $Length, callable $Sherlock
108108
}
109109
while( $ReadMore && $SherlockFunction( $Buffer, $Length ) );
110110

111-
$Data = Implode( $Packets );
111+
$Data = implode( $Packets );
112112

113113
// TODO: Test this
114114
if( $IsCompressed )
115115
{
116116
// Let's make sure this function exists, it's not included in PHP by default
117-
if( !Function_Exists( 'bzdecompress' ) )
117+
if( !function_exists( 'bzdecompress' ) )
118118
{
119119
throw new \RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' );
120120
}
121121

122122
$Data = bzdecompress( $Data );
123123

124-
if( !is_string( $Data ) || CRC32( $Data ) !== $PacketChecksum )
124+
if( !is_string( $Data ) || crc32( $Data ) !== $PacketChecksum )
125125
{
126126
throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH );
127127
}
128128
}
129129

130-
$Buffer->Set( SubStr( $Data, 4 ) );
130+
$Buffer->Set( substr( $Data, 4 ) );
131131
}
132132
else
133133
{
134-
throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . DecHex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
134+
throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . dechex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
135135
}
136136

137137
return $Buffer;

SourceQuery/Buffer.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Buffer
4444
public function Set( string $Buffer ) : void
4545
{
4646
$this->Buffer = $Buffer;
47-
$this->Length = StrLen( $Buffer );
47+
$this->Length = strlen( $Buffer );
4848
$this->Position = 0;
4949
}
5050

@@ -81,7 +81,7 @@ public function Get( int $Length = -1 ) : string
8181
return '';
8282
}
8383

84-
$Data = SubStr( $this->Buffer, $this->Position, $Length );
84+
$Data = substr( $this->Buffer, $this->Position, $Length );
8585

8686
$this->Position += $Length;
8787

@@ -93,7 +93,7 @@ public function Get( int $Length = -1 ) : string
9393
*/
9494
public function GetByte( ) : int
9595
{
96-
return Ord( $this->Get( 1 ) );
96+
return ord( $this->Get( 1 ) );
9797
}
9898

9999
/**
@@ -106,7 +106,7 @@ public function GetShort( ) : int
106106
throw new InvalidPacketException( 'Not enough data to unpack a short.', InvalidPacketException::BUFFER_EMPTY );
107107
}
108108

109-
$Data = UnPack( 'v', $this->Get( 2 ) );
109+
$Data = unpack( 'v', $this->Get( 2 ) );
110110

111111
return (int)$Data[ 1 ];
112112
}
@@ -121,7 +121,7 @@ public function GetLong( ) : int
121121
throw new InvalidPacketException( 'Not enough data to unpack a long.', InvalidPacketException::BUFFER_EMPTY );
122122
}
123123

124-
$Data = UnPack( 'l', $this->Get( 4 ) );
124+
$Data = unpack( 'l', $this->Get( 4 ) );
125125

126126
return (int)$Data[ 1 ];
127127
}
@@ -136,7 +136,7 @@ public function GetFloat( ) : float
136136
throw new InvalidPacketException( 'Not enough data to unpack a float.', InvalidPacketException::BUFFER_EMPTY );
137137
}
138138

139-
$Data = UnPack( 'f', $this->Get( 4 ) );
139+
$Data = unpack( 'f', $this->Get( 4 ) );
140140

141141
return (float)$Data[ 1 ];
142142
}
@@ -151,7 +151,7 @@ public function GetUnsignedLong( ) : int
151151
throw new InvalidPacketException( 'Not enough data to unpack an usigned long.', InvalidPacketException::BUFFER_EMPTY );
152152
}
153153

154-
$Data = UnPack( 'V', $this->Get( 4 ) );
154+
$Data = unpack( 'V', $this->Get( 4 ) );
155155

156156
return (int)$Data[ 1 ];
157157
}
@@ -161,7 +161,7 @@ public function GetUnsignedLong( ) : int
161161
*/
162162
public function GetString( ) : string
163163
{
164-
$ZeroBytePosition = StrPos( $this->Buffer, "\0", $this->Position );
164+
$ZeroBytePosition = strpos( $this->Buffer, "\0", $this->Position );
165165

166166
if( $ZeroBytePosition === false )
167167
{

SourceQuery/GoldSourceRcon.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public function Open( ) : void
5353

5454
public function Write( int $Header, string $String = '' ) : bool
5555
{
56-
$Command = Pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String );
57-
$Length = StrLen( $Command );
56+
$Command = pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String );
57+
$Length = strlen( $Command );
5858

59-
return $Length === FWrite( $this->Socket->Socket, $Command, $Length );
59+
return $Length === fwrite( $this->Socket->Socket, $Command, $Length );
6060
}
6161

6262
/**
@@ -89,7 +89,7 @@ public function Read( int $Length = 1400 ) : Buffer
8989
//$StringBuffer .= SubStr( $Packet, 0, -2 );
9090

9191
// Let's assume if this packet is not long enough, there are no more after this one
92-
$ReadMore = StrLen( $Packet ) > 1000; // use 1300?
92+
$ReadMore = strlen( $Packet ) > 1000; // use 1300?
9393
9494
if( $ReadMore )
9595
{
@@ -140,6 +140,6 @@ public function Authorize( string $Password ) : void
140140
throw new AuthenticationException( 'Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD );
141141
}
142142

143-
$this->RconChallenge = Trim( $Buffer->Get( ) );
143+
$this->RconChallenge = trim( $Buffer->Get( ) );
144144
}
145145
}

SourceQuery/Socket.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function Close( ) : void
2929
{
3030
if( $this->Socket !== null )
3131
{
32-
FClose( $this->Socket );
32+
fclose( $this->Socket );
3333

3434
$this->Socket = null;
3535
}
@@ -42,24 +42,24 @@ public function Open( string $Address, int $Port, int $Timeout, int $Engine ) :
4242
$this->Port = $Port;
4343
$this->Address = $Address;
4444

45-
$Socket = @FSockOpen( 'udp://' . $Address, $Port, $ErrNo, $ErrStr, $Timeout );
45+
$Socket = @fsockopen( 'udp://' . $Address, $Port, $ErrNo, $ErrStr, $Timeout );
4646

4747
if( $ErrNo || $Socket === false )
4848
{
4949
throw new SocketException( 'Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET );
5050
}
5151

5252
$this->Socket = $Socket;
53-
Stream_Set_Timeout( $this->Socket, $Timeout );
54-
Stream_Set_Blocking( $this->Socket, true );
53+
stream_set_timeout( $this->Socket, $Timeout );
54+
stream_set_blocking( $this->Socket, true );
5555
}
5656

5757
public function Write( int $Header, string $String = '' ) : bool
5858
{
59-
$Command = Pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String );
60-
$Length = StrLen( $Command );
59+
$Command = pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String );
60+
$Length = strlen( $Command );
6161

62-
return $Length === FWrite( $this->Socket, $Command, $Length );
62+
return $Length === fwrite( $this->Socket, $Command, $Length );
6363
}
6464

6565
/**
@@ -72,7 +72,7 @@ public function Write( int $Header, string $String = '' ) : bool
7272
public function Read( int $Length = 1400 ) : Buffer
7373
{
7474
$Buffer = new Buffer( );
75-
$Buffer->Set( FRead( $this->Socket, $Length ) );
75+
$Buffer->Set( fread( $this->Socket, $Length ) );
7676

7777
$this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] );
7878

@@ -81,9 +81,9 @@ public function Read( int $Length = 1400 ) : Buffer
8181

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

86-
if( StrLen( $Data ) < 4 )
86+
if( strlen( $Data ) < 4 )
8787
{
8888
return false;
8989
}

SourceQuery/SourceQuery.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ public function GetInfo( ) : array
240240
$Server[ 'Players' ] = $Buffer->GetByte( );
241241
$Server[ 'MaxPlayers' ] = $Buffer->GetByte( );
242242
$Server[ 'Protocol' ] = $Buffer->GetByte( );
243-
$Server[ 'Dedicated' ] = Chr( $Buffer->GetByte( ) );
244-
$Server[ 'Os' ] = Chr( $Buffer->GetByte( ) );
243+
$Server[ 'Dedicated' ] = chr( $Buffer->GetByte( ) );
244+
$Server[ 'Os' ] = chr( $Buffer->GetByte( ) );
245245
$Server[ 'Password' ] = $Buffer->GetByte( ) === 1;
246246
$Server[ 'IsMod' ] = $Buffer->GetByte( ) === 1;
247247

@@ -266,7 +266,7 @@ public function GetInfo( ) : array
266266

267267
if( $Type !== self::S2A_INFO_SRC )
268268
{
269-
throw new InvalidPacketException( 'GetInfo: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
269+
throw new InvalidPacketException( 'GetInfo: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
270270
}
271271

272272
$Server[ 'Protocol' ] = $Buffer->GetByte( );
@@ -278,8 +278,8 @@ public function GetInfo( ) : array
278278
$Server[ 'Players' ] = $Buffer->GetByte( );
279279
$Server[ 'MaxPlayers' ] = $Buffer->GetByte( );
280280
$Server[ 'Bots' ] = $Buffer->GetByte( );
281-
$Server[ 'Dedicated' ] = Chr( $Buffer->GetByte( ) );
282-
$Server[ 'Os' ] = Chr( $Buffer->GetByte( ) );
281+
$Server[ 'Dedicated' ] = chr( $Buffer->GetByte( ) );
282+
$Server[ 'Os' ] = chr( $Buffer->GetByte( ) );
283283
$Server[ 'Password' ] = $Buffer->GetByte( ) === 1;
284284
$Server[ 'Secure' ] = $Buffer->GetByte( ) === 1;
285285

@@ -390,7 +390,7 @@ public function GetPlayers( ) : array
390390

391391
if( $Type !== self::S2A_PLAYER )
392392
{
393-
throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
393+
throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
394394
}
395395

396396
$Players = [];
@@ -403,7 +403,7 @@ public function GetPlayers( ) : array
403403
$Player[ 'Name' ] = $Buffer->GetString( );
404404
$Player[ 'Frags' ] = $Buffer->GetLong( );
405405
$Player[ 'Time' ] = (int)$Buffer->GetFloat( );
406-
$Player[ 'TimeF' ] = GMDate( ( $Player[ 'Time' ] > 3600 ? "H:i:s" : "i:s" ), $Player[ 'Time' ] );
406+
$Player[ 'TimeF' ] = gmdate( ( $Player[ 'Time' ] > 3600 ? 'H:i:s' : 'i:s' ), $Player[ 'Time' ] );
407407

408408
$Players[ ] = $Player;
409409
}
@@ -435,7 +435,7 @@ public function GetRules( ) : array
435435

436436
if( $Type !== self::S2A_RULES )
437437
{
438-
throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
438+
throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
439439
}
440440

441441
$Rules = [];
@@ -446,7 +446,7 @@ public function GetRules( ) : array
446446
$Rule = $Buffer->GetString( );
447447
$Value = $Buffer->GetString( );
448448

449-
if( !Empty( $Rule ) )
449+
if( !empty( $Rule ) )
450450
{
451451
$Rules[ $Rule ] = $Value;
452452
}
@@ -497,7 +497,7 @@ private function GetChallenge( int $Header, int $ExpectedResult ) : void
497497
}
498498
default:
499499
{
500-
throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
500+
throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
501501
}
502502
}
503503
}

0 commit comments

Comments
 (0)