Skip to content

Commit

Permalink
#55: Fix $isMasked, Add more comments + Fix tests with phpunit >= 9.4
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Mar 14, 2021
1 parent 32167a1 commit 6d3dc0f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/
vendor/
tests/tests.log
composer.lock
composer.lock
.phpunit.result.cache
10 changes: 10 additions & 0 deletions src/Components/WscMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class WscMain implements WscCommonsContract
*/
private ?string $hugePayload;

/**
* @var array|int[]
*/
private static array $opcodes = [
CommonsContract::EVENT_TYPE_CONTINUATION => 0,
CommonsContract::EVENT_TYPE_TEXT => 1,
Expand All @@ -58,7 +61,14 @@ class WscMain implements WscCommonsContract
CommonsContract::EVENT_TYPE_PONG => 10,
];

/**
* @var string
*/
protected string $socketUrl = '';

/**
* @var ClientConfig
*/
protected ClientConfig $config;

/**
Expand Down
18 changes: 8 additions & 10 deletions src/Components/WssMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
class WssMain implements CommonsContract
{
/**
* @var bool
*/
private bool $isPcntlLoaded = false;

/**
Expand Down Expand Up @@ -76,18 +79,13 @@ protected function decode(string $data)
return false;
}

if ($isMasked) {
for ($i = $payloadOffset; $i < $dataLength; $i++) {
$j = $i - $payloadOffset;
if (isset($data[$i])) {
$unmaskedPayload .= $data[$i] ^ $mask[$j % 4];
}
for ($i = $payloadOffset; $i < $dataLength; $i++) {
$j = $i - $payloadOffset;
if (isset($data[$i])) {
$unmaskedPayload .= $data[$i] ^ $mask[$j % 4];
}
$decodedData['payload'] = $unmaskedPayload;
} else {
$payloadOffset -= 4;
$decodedData['payload'] = substr($data, $payloadOffset);
}
$decodedData['payload'] = $unmaskedPayload;

return $decodedData;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Contracts/WebSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
*/
abstract class WebSocket implements WebSocketContract, MessageContract
{
public $pathParams = [];
/**
* @var array
*/
public array $pathParams = [];

/**
* You may want to implement these methods to bring ping/pong events
Expand Down
16 changes: 13 additions & 3 deletions tests/ServerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ class ServerHandler extends WebSocket
* Otherwise leave $pathParams as an empty array
*/

public $pathParams = [':entity', ':context', ':token'];
private $clients = [];
/**
* @var array|string[]
*/
public array $pathParams = [':entity', ':context', ':token'];

private $log;
/**
* @var array
*/
private array $clients = [];

/**
* @var Logger
*/
private Logger $log;

/**
* ServerHandler constructor.
Expand Down
2 changes: 1 addition & 1 deletion tests/WebSocketClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class WebSocketClientTest extends TestCase

private $url;

public function setUp()/* The :void return type declaration that should be here would cause a BC issue */
public function setUp(): void
{
$this->url = self::WS_SCHEME . self::WS_HOST . self::WS_PORT . self::WS_URI;
}
Expand Down

0 comments on commit 6d3dc0f

Please sign in to comment.