Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions src/Frame/HybiFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,58 +69,60 @@ class HybiFrame extends Frame
*/
public function encode(string $payload, int $type = Protocol::TYPE_TEXT, bool $masked = false): Frame
{
if (!\is_int($type) || !\in_array($type, Protocol::FRAME_TYPES)) {
if (!\in_array($type, Protocol::FRAME_TYPES, true)) {
throw new InvalidArgumentException('Invalid frame type');
}

$this->type = $type;
$this->masked = $masked;
$this->payload = $payload;
$this->length = \strlen($this->payload);
$this->offset_mask = null;
$this->offset_payload = null;

$this->buffer = "\x00\x00";

// FIN + opcode byte
$this->buffer[self::BYTE_HEADER] = \chr(
(self::BITFIELD_TYPE & $this->type)
| (self::BITFIELD_FINAL & \PHP_INT_MAX)
Copy link
Author

@divinity76 divinity76 May 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(non-negative-integer & \PHP_INT_MAX)
is a no-op 🤔 not sure what the original intention was.

| self::BITFIELD_FINAL
);

$masked_bit = (self::BITFIELD_MASKED & ($this->masked ? \PHP_INT_MAX : 0));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was just unnecessarily complex / difficult to read.

$masked_bit = $this->masked ? self::BITFIELD_MASKED : 0;

if ($this->length <= 125) {
$this->buffer[self::BYTE_INITIAL_LENGTH] = \chr(
(self::BITFIELD_INITIAL_LENGTH & $this->length) | $masked_bit
($this->length & self::BITFIELD_INITIAL_LENGTH)
| $masked_bit
);
} elseif ($this->length <= 65536) {
} elseif ($this->length <= 65535) {
$this->buffer[self::BYTE_INITIAL_LENGTH] = \chr(
(self::BITFIELD_INITIAL_LENGTH & 126) | $masked_bit
(126 & self::BITFIELD_INITIAL_LENGTH)
| $masked_bit
);
$this->buffer .= \pack('n', $this->length);
} else {
$this->buffer[self::BYTE_INITIAL_LENGTH] = \chr(
(self::BITFIELD_INITIAL_LENGTH & 127) | $masked_bit
(127 & self::BITFIELD_INITIAL_LENGTH)
| $masked_bit
);

if (\PHP_INT_MAX > 2147483647) {
$this->buffer .= \pack('NN', $this->length >> 32, $this->length);
} else {
if (\PHP_INT_SIZE === 4) {
// J is not available on 32-bit PHP
$this->buffer .= \pack('NN', 0, $this->length);
} else {
$this->buffer .= \pack('J', $this->length);
}
}

if ($this->masked) {
$this->mask = $this->generateMask();
$this->offset_mask = \strlen($this->buffer);
$this->buffer .= $this->mask;
$this->offset_payload = \strlen($this->buffer);
$this->buffer .= $this->mask($this->payload);
} else {
$this->offset_payload = \strlen($this->buffer);
$this->buffer .= $this->payload;
}

$this->offset_mask = $this->getMaskOffset();
$this->offset_payload = $this->getPayloadOffset();

return $this;
}

Expand Down Expand Up @@ -196,7 +198,6 @@ protected function getMaskOffset(): int
if (!isset($this->offset_mask)) {
$offset = self::BYTE_INITIAL_LENGTH + 1;
$offset += $this->getLengthSize();

$this->offset_mask = $offset;
}

Expand Down Expand Up @@ -233,7 +234,6 @@ protected function getInitialLength(): int
if (!isset($this->buffer[self::BYTE_INITIAL_LENGTH])) {
throw new FrameException('Cannot yet tell expected length');
}
$a = (int) (\ord($this->buffer[self::BYTE_INITIAL_LENGTH]) & self::BITFIELD_INITIAL_LENGTH);

return (int) (\ord($this->buffer[self::BYTE_INITIAL_LENGTH]) & self::BITFIELD_INITIAL_LENGTH);
}
Expand All @@ -258,7 +258,6 @@ protected function getPayloadOffset(): int
if (!isset($this->offset_payload)) {
$offset = $this->getMaskOffset();
$offset += $this->getMaskSize();

$this->offset_payload = $offset;
}

Expand Down Expand Up @@ -294,7 +293,7 @@ public function getType(): int

$type = (int) (\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');
}

Expand Down