Skip to content

Commit

Permalink
Replace array_key_exists to isset
Browse files Browse the repository at this point in the history
  • Loading branch information
moririnson committed Apr 23, 2019
1 parent 107a079 commit 671783f
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/LINEBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(HTTPClient $httpClient, array $args)
$this->channelSecret = $args['channelSecret'];

$this->endpointBase = LINEBot::DEFAULT_ENDPOINT_BASE;
if (array_key_exists('endpointBase', $args) && !empty($args['endpointBase'])) {
if (!empty($args['endpointBase'])) {
$this->endpointBase = $args['endpointBase'];
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/LINEBot/Event/BaseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getTimestamp()
*/
public function getReplyToken()
{
return array_key_exists('replyToken', $this->event) ? $this->event['replyToken'] : null;
return isset($this->event['replyToken']) ? $this->event['replyToken'] : null;
}

/**
Expand Down Expand Up @@ -120,7 +120,7 @@ public function isUnknownEvent()
*/
public function getUserId()
{
return array_key_exists('userId', $this->event['source'])
return isset($this->event['source']['userId'])
? $this->event['source']['userId']
: null;
}
Expand All @@ -136,7 +136,7 @@ public function getGroupId()
if (!$this->isGroupEvent()) {
throw new InvalidEventSourceException('This event source is not a group type');
}
return array_key_exists('groupId', $this->event['source'])
return isset($this->event['source']['groupId'])
? $this->event['source']['groupId']
: null;
}
Expand All @@ -152,7 +152,7 @@ public function getRoomId()
if (!$this->isRoomEvent()) {
throw new InvalidEventSourceException('This event source is not a room type');
}
return array_key_exists('roomId', $this->event['source'])
return isset($this->event['source']['roomId'])
? $this->event['source']['roomId']
: null;
}
Expand Down
14 changes: 6 additions & 8 deletions src/LINEBot/Event/MessageEvent/ContentProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ public function isExternal()
*/
public function getOriginalContentUrl()
{
if (array_key_exists('originalContentUrl', $this->contentProvider)) {
return $this->contentProvider['originalContentUrl'];
}
return null;
return isset($this->contentProvider['originalContentUrl'])
? $this->contentProvider['originalContentUrl']
: null;
}

/**
Expand All @@ -75,9 +74,8 @@ public function getOriginalContentUrl()
*/
public function getPreviewImageUrl()
{
if (array_key_exists('previewImageUrl', $this->contentProvider)) {
return $this->contentProvider['previewImageUrl'];
}
return null;
return isset($this->contentProvider['previewImageUrl'])
? $this->contentProvider['previewImageUrl']
: null;
}
}
4 changes: 2 additions & 2 deletions src/LINEBot/Event/MessageEvent/LocationMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct($event)
*/
public function getTitle()
{
return array_key_exists('title', $this->message) ? $this->message['title'] : null;
return isset($this->message['title']) ? $this->message['title'] : null;
}

/**
Expand All @@ -54,7 +54,7 @@ public function getTitle()
*/
public function getAddress()
{
return array_key_exists('address', $this->message) ? $this->message['address'] : null;
return isset($this->message['address']) ? $this->message['address'] : null;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/LINEBot/Event/Parser/EventRequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ public static function parseEventRequest($body, $channelSecret, $signature, $eve
$events = [];

$parsedReq = json_decode($body, true);
if (!array_key_exists('events', $parsedReq)) {
if (!isset($parsedReq['events'])) {
throw new InvalidEventRequestException();
}

foreach ($parsedReq['events'] as $eventData) {
$eventType = $eventData['type'];

if (!array_key_exists($eventType, self::$eventType2class)) {
if (!isset(self::$eventType2class[$eventType])) {
# Unknown event has come
$events[] = new UnknownEvent($eventData);
continue;
Expand All @@ -99,7 +99,7 @@ public static function parseEventRequest($body, $channelSecret, $signature, $eve
}

$parsedReq = json_decode($body, true);
if (!array_key_exists('destination', $parsedReq)) {
if (!isset($parsedReq['destination'])) {
throw new InvalidEventRequestException();
}

Expand All @@ -113,7 +113,7 @@ public static function parseEventRequest($body, $channelSecret, $signature, $eve
private static function parseMessageEvent($eventData)
{
$messageType = $eventData['message']['type'];
if (!array_key_exists($messageType, self::$messageType2class)) {
if (!isset(self::$messageType2class[$messageType])) {
return new UnknownMessage($eventData);
}

Expand Down
2 changes: 1 addition & 1 deletion src/LINEBot/Event/PostbackEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getPostbackData()
*/
public function getPostbackParams()
{
return array_key_exists('params', $this->event['postback'])
return isset($this->event['postback']['params'])
? $this->event['postback']['params']
: null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/LINEBot/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getJSONDecodedBody()
*/
public function getHeader($name)
{
if (array_key_exists($name, $this->headers)) {
if (isset($this->headers[$name])) {
return $this->headers[$name];
}
return null;
Expand Down
4 changes: 2 additions & 2 deletions tests/LINEBot/GetMemberIdsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testGetGroupMemberIds()

$data = $res->getJSONDecodedBody();
$this->assertEquals(['Uxxxxxxxxxxxxxx4', 'Uxxxxxxxxxxxxxx5'], $data['memberIds']);
$this->assertFalse(array_key_exists('next', $data));
$this->assertFalse(isset($data['next']));

// test getAllGroupMemberIds()
$memberIds = $bot->getAllGroupMemberIds('GROUP_ID');
Expand Down Expand Up @@ -108,7 +108,7 @@ public function testGetRoomMemberIds()

$data = $res->getJSONDecodedBody();
$this->assertEquals(['Uxxxxxxxxxxxxxx4', 'Uxxxxxxxxxxxxxx5'], $data['memberIds']);
$this->assertFalse(array_key_exists('next', $data));
$this->assertFalse(isset($data['next']));

// test getAllGroupMemberIds()
$memberIds = $bot->getAllRoomMemberIds('ROOM_ID');
Expand Down

0 comments on commit 671783f

Please sign in to comment.