Skip to content

Commit 45efc3b

Browse files
committed
Use property_exists instead of isset
result and id can be null, in which case isset() returns false event though they are present in the JSON
1 parent 6aa733f commit 45efc3b

File tree

5 files changed

+6
-4
lines changed

5 files changed

+6
-4
lines changed

lib/Message.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public static function parse(string $msg): Message
3535
$obj = new SuccessResponse($decoded->id, $decoded->result);
3636
} else if (ErrorResponse::isErrorResponse($decoded)) {
3737
$obj = new ErrorResponse($decoded->id, new Error($decoded->error->message, $decoded->error->code, $decoded->error->data ?? null));
38+
} else {
39+
throw new Error('Invalid message', ErrorCode::INVALID_REQUEST);
3840
}
3941
return $obj;
4042
}

lib/Notification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Notification extends Message
4141
*/
4242
public static function isNotification($msg): bool
4343
{
44-
return is_object($msg) && !isset($msg->id) && isset($msg->method);
44+
return is_object($msg) && !property_exists($msg, 'id') && isset($msg->method);
4545
}
4646

4747
/**

lib/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Request extends Message
4646
*/
4747
public static function isRequest($msg): bool
4848
{
49-
return is_object($msg) && isset($msg->id) && isset($msg->method);
49+
return is_object($msg) && property_exists($msg, 'id') && isset($msg->method);
5050
}
5151

5252
/**

lib/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ abstract class Response extends Message
2525
*/
2626
public static function isResponse($msg): bool
2727
{
28-
return is_object($msg) && isset($msg->id) && (isset($msg->result) || isset($msg->error));
28+
return is_object($msg) && property_exists($msg, 'id') && (property_exists($msg, 'result') || isset($msg->error));
2929
}
3030

3131
/**

lib/SuccessResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class SuccessResponse extends Response
2525
*/
2626
public static function isSuccessResponse($msg): bool
2727
{
28-
return is_object($msg) && isset($msg->id) && isset($msg->result);
28+
return is_object($msg) && property_exists($msg, 'id') && property_exists($msg, 'result');
2929
}
3030

3131
/**

0 commit comments

Comments
 (0)