Skip to content

Commit

Permalink
run code inspect check, some update
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 27, 2018
1 parent 636d20e commit b127a53
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/DataHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected function split_packet($length, $packet, $user)
if ($user->hasSentClose) {
$this->disconnect($user->socket);
} else {
if ((preg_match('//u', $message)) || ($headers['opcode'] == 2)) {
if (((int)$headers['opcode'] === 2) || preg_match('//u', $message)) {
//$this->stdout("Text msg encoded UTF-8 or Binary msg\n".$message);
$this->process($user, $message);
} else {
Expand Down
27 changes: 19 additions & 8 deletions src/Frame/HybiFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class HybiFrame extends Frame
*/
public function encode($payload, $type = Protocol::TYPE_TEXT, $masked = false)
{
if (!\is_int($type) || !\in_array($type, Protocol::FRAME_TYPES)) {
if (!\is_int($type) || !in_array($type, Protocol::FRAME_TYPES, true)) {
throw new InvalidArgumentException('Invalid frame type');
}

Expand Down Expand Up @@ -292,39 +292,47 @@ public function receiveData($data)
* @return bool
* @throws FrameException
*/
public function isFinal()
public function isFinal(): bool
{
if (!isset($this->buffer[self::BYTE_HEADER])) {
throw new FrameException('Cannot yet tell if frame is final');
}

return (boolean)(\ord($this->buffer[self::BYTE_HEADER]) & self::BITFIELD_FINAL);
return (bool)(\ord($this->buffer[self::BYTE_HEADER]) & self::BITFIELD_FINAL);
}

/**
* @throws FrameException
*/
public function getType()
public function getType(): int
{
if (!isset($this->buffer[self::BYTE_HEADER])) {
throw new FrameException('Cannot yet tell type of frame');
}

$type = (int)(\ord($this->buffer[self::BYTE_HEADER]) & self::BITFIELD_TYPE);
$type = (\ord($this->buffer[self::BYTE_HEADER]) & self::BITFIELD_TYPE);

if (!\in_array($type, Protocol::FRAME_TYPES)) {
if (!in_array($type, Protocol::FRAME_TYPES, true)) {
throw new FrameException('Invalid payload type');
}

return $type;
}

protected function getExpectedBufferLength()
/**
* @return int
* @throws FrameException
*/
protected function getExpectedBufferLength(): int
{
return $this->getLength() + $this->getPayloadOffset();
}

public function getLength()
/**
* @return int
* @throws FrameException
*/
public function getLength(): int
{
if (!$this->length) {
$initial = $this->getInitialLength();
Expand Down Expand Up @@ -353,6 +361,9 @@ public function getLength()
return $this->length;
}

/**
* @throws FrameException
*/
protected function decodeFramePayloadFromBuffer()
{
$payload = substr($this->buffer, $this->getPayloadOffset());
Expand Down
2 changes: 1 addition & 1 deletion src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function frame(string $s): string
* @param $buffer
* @return string
*/
public static function decode($buffer)
public static function decode($buffer): string
{
/*$len = $masks = $data =*/
$decoded = '';
Expand Down
2 changes: 1 addition & 1 deletion src/Payload/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function encode(string $data, int $type = Protocol::TYPE_TEXT, bool $mask
*
* @return Frame
*/
abstract protected function getFrame();
abstract protected function getFrame(): Frame;

/**
* Whether this payload is waiting for more data
Expand Down
13 changes: 8 additions & 5 deletions src/Payload/PayloadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace MyLib\WebSocket\Util\Payload;

use Inhere\Library\StdObject;
use InvalidArgumentException;
use Wrench\Exception\PayloadException;
use MyLib\ObjUtil\Obj;
use MyLib\PhpUtil\PhpHelper;
use MyLib\WebSocket\Util\Exception\PayloadException;

/**
* Handles chunking and splitting of payloads into frames
*/
class PayloadHandler extends StdObject
class PayloadHandler
{
/**
* A callback that will be called when a complete payload is available
Expand All @@ -20,6 +21,7 @@ class PayloadHandler extends StdObject

/**
* The current payload
* @var Payload
*/
protected $payload;

Expand All @@ -30,7 +32,7 @@ class PayloadHandler extends StdObject
*/
public function __construct(callable $callback, array $options = [])
{
parent::__construct($options);
Obj::init($this, $options);

$this->callback = $callback;
}
Expand All @@ -49,6 +51,7 @@ public function handle(string $data)

while ($data) { // Each iteration pulls off a single payload chunk
$size = \strlen($data);
$chunkSize = 0;
$remaining = $this->payload->getRemainingData();

// If we don't yet know how much data is remaining, read data into
Expand Down Expand Up @@ -91,7 +94,7 @@ public function handle(string $data)
*/
protected function emit(Payload $payload)
{
\call_user_func($this->callback, $payload);
PhpHelper::call($this->callback, $payload);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Protocol/Hybi10Protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Hybi10Protocol extends HybiProtocol
/**
* @see Protocol::getVersion()
*/
public function getVersion(): string
public function getVersion(): int
{
return self::VERSION;
}
Expand Down
Loading

0 comments on commit b127a53

Please sign in to comment.