Skip to content

Commit

Permalink
replace array_key_exists
Browse files Browse the repository at this point in the history
  • Loading branch information
rich-spitkovsky-riskified committed Feb 4, 2020
1 parent 1714404 commit 82bb62d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Riskified/DecisionNotification/Model/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ protected function test_authorization() {
*/
protected function parse_body() {
$body = json_decode($this->body);
if (!array_key_exists('order', $body))
if (!isset($body["order"]))
throw new Exception\BadPostJsonException($this->headers, $this->body);

$order = $body->{'order'};
if (!array_key_exists('id', $order) || !array_key_exists('status', $order))
if (!isset($order["id"]) || !isset($order["status"]))
throw new Exception\BadPostJsonException($this->headers, $this->body);

//foreach($order as $key => $value)
Expand All @@ -105,11 +105,11 @@ protected function parse_body() {
$this->oldStatus = $order->{'old_status'};
$this->description = $order->{'description'};

if (array_key_exists('category', $order)) {
if (isset($order["category"])) {
$this->category = $order->{'category'};
}

if (array_key_exists('decision_code', $order)) {
if (isset($order["decision_code"])) {
$this->decisionCode = $order->{'decision_code'};
}
}
Expand Down

2 comments on commit 82bb62d

@horrower
Copy link

@horrower horrower commented on 82bb62d May 26, 2020

Choose a reason for hiding this comment

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

you have a bug here
json_decode will return object, not array (until you set true as second param)
if (!isset($body["order"])) - this will fails with error Cannot use object of type stdClass as array

btw, array_key_exists will work on object, but will return null without error

@yabushraber
Copy link

@yabushraber yabushraber commented on 82bb62d Jun 4, 2020

Choose a reason for hiding this comment

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

array_key_exists on object in PHP 7.4 will produce:

(E_DEPRECATED)
array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead

Problem: setting true as second parameter causes a problem in PHP 7.4:
Trying to get property 'order' of non-object:
$body->{'order'} reading object
$body["order"] reading array

can you please use array or object syntax, but not both
thank you

Please sign in to comment.