Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 0 additions & 6 deletions src/Responses/ChannelsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,8 @@ public function __construct(public string $data, public bool $status, public arr
*/
public static function fromBytes(string $data, ValueSize $size): ChannelsResponse|null
{
$valueSize = $size->value;
$offset = 0;

// Less than 1 byte? not enough for status.
if (strlen($data) < 1) {
return null;
}

$status = ord($data[$offset]) === 1;
$offset++;

Expand Down
5 changes: 0 additions & 5 deletions src/Responses/ConnectionsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ public static function fromBytes(string $data, ValueSize $size): ConnectionsResp
{
$offset = 0;

// Less than 1 byte? not enough for status.
if (strlen($data) < 1) {
return null;
}

$status = ord($data[$offset]) === 1;
$offset++;

Expand Down
19 changes: 2 additions & 17 deletions src/Responses/GetResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,41 +51,26 @@ public static function fromBytes(string $data, ValueSize $size): GetResponse|nul
$valueSize = $size->value;
$offset = 0;

// Less than 1 byte? not enough for status.
if (strlen($data) < 1) {
return null;
}

$status = ord($data[$offset]) === 1;
$offset++;

if ($status) {
// Less than 2 bytes? not enough for ttl type.
if (strlen($data) < 2) {
if (strlen($data) < $offset + 1 + $valueSize * 2) {
return null;
}

$ttl_type = TTLType::from(ord($data[$offset]));
$offset++;

// Less than 2 + N bytes? not enough for ttl.
if (strlen($data) < 2 + $valueSize) {
return null;
}

$ttl = unpack(BaseRequest::pack($size), substr($data, $offset, $valueSize))[1];
$offset += $valueSize;

// Less than 2 + 2 * N bytes? not enough for value size.
if (strlen($data) < 2 + ($valueSize * 2)) {
return null;
}

$value_sized = unpack(BaseRequest::pack($size), substr($data, $offset, $valueSize))[1];
$offset += $valueSize;

// Less than 2 + 2 * N + O bytes? not enough for value.
if (strlen($data) < 2 + ($valueSize * 2) + $value_sized) {
if (strlen($data) < $offset + $value_sized) {
return null;
}

Expand Down
5 changes: 0 additions & 5 deletions src/Responses/ListResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ public static function fromBytes(string $data, ValueSize $size): ListResponse|nu
{
$offset = 0;

// Less than 1 byte? not enough for status.
if (strlen($data) < 1) {
return null;
}

$status = ord($data[$offset]) === 1;
$offset++;

Expand Down
21 changes: 4 additions & 17 deletions src/Responses/QueryResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,23 @@ public static function fromBytes(string $data, ValueSize $size): QueryResponse|n
$valueSize = $size->value;
$offset = 0;

// Less than 1 byte? not enough for status.
if (strlen($data) < 1) {
return null;
}

$status = ord($data[$offset]) === 1;
$offset++;

if ($status) {
// Less than 1 + N bytes? not enough for quota.
if (strlen($data) < 1 + $valueSize) {
if (strlen($data) < $offset +
$valueSize * 2 + // quota and ttl
1 // ttl type
) {
return null;
}

$quota = unpack(BaseRequest::pack($size), substr($data, $offset, $valueSize))[1];
$offset += $size->value;

// Less than 2 + N bytes? not enough for ttl type.
if (strlen($data) < 2 + $valueSize) {
return null;
}

$ttl_type = TTLType::from(ord($data[$offset]));
$offset++;

// Less than 2 + 2N bytes? not enough for ttl.
if (strlen($data) < 2 + ($valueSize * 2)) {
return null;
}

$ttl = unpack(BaseRequest::pack($size), substr($data, $offset, $valueSize))[1];
return new QueryResponse($data, true, $quota, $ttl_type, $ttl);
}
Expand Down
6 changes: 0 additions & 6 deletions src/Responses/StatResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,8 @@ public function __construct(public string $data, public bool $status, public arr
*/
public static function fromBytes(string $data, ValueSize $size): StatResponse|null
{
$valueSize = $size->value;
$offset = 0;

// Less than 1 byte? not enough for status.
if (strlen($data) < 1) {
return null;
}

$status = ord($data[$offset]) === 1;

$offset++;
Expand Down
2 changes: 0 additions & 2 deletions src/Responses/StatsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function __construct(public string $data, public bool $status, public arr
*/
public static function fromBytes(string $data, ValueSize $size): StatsResponse|null
{
$valueSize = $size->value;
$offset = 0;

// Less than 1 byte? not enough for status.
Expand Down Expand Up @@ -80,7 +79,6 @@ public static function fromBytes(string $data, ValueSize $size): StatsResponse|n
return null;
}

$fragment = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

// Less than offset + 8 bytes? not enough for fragment keys count.
Expand Down
10 changes: 1 addition & 9 deletions src/Responses/StatusResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ public function __construct(public string $data, public bool $status)
*/
public static function fromBytes(string $data, ValueSize $size): StatusResponse|null
{
$offset = 0;

// Less than 1 byte? not enough for status.
if (strlen($data) < 1) {
return null;
}

$status = ord($data[$offset]) === 1;
return new StatusResponse($data, $status);
return new StatusResponse($data, ord($data[0]) === 1);
}
}
2 changes: 1 addition & 1 deletion tests/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public function testWhoami() {
$this->prepares(function (Service $service) {
$whoami = $service->whoami();
$this->assertTrue($whoami->status);
$this->assertTrue(strlen($whoami->id) == 32);
$this->assertEquals(32, strlen($whoami->id));
});
}

Expand Down