From 671783f222dfe03ab468b3a47f51a47b1c299c11 Mon Sep 17 00:00:00 2001 From: moririnson Date: Tue, 23 Apr 2019 12:32:36 +0900 Subject: [PATCH] Replace array_key_exists to isset --- src/LINEBot.php | 2 +- src/LINEBot/Event/BaseEvent.php | 8 ++++---- src/LINEBot/Event/MessageEvent/ContentProvider.php | 14 ++++++-------- src/LINEBot/Event/MessageEvent/LocationMessage.php | 4 ++-- src/LINEBot/Event/Parser/EventRequestParser.php | 8 ++++---- src/LINEBot/Event/PostbackEvent.php | 2 +- src/LINEBot/Response.php | 2 +- tests/LINEBot/GetMemberIdsTest.php | 4 ++-- 8 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/LINEBot.php b/src/LINEBot.php index c5fae9b0..f0b151f6 100644 --- a/src/LINEBot.php +++ b/src/LINEBot.php @@ -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']; } } diff --git a/src/LINEBot/Event/BaseEvent.php b/src/LINEBot/Event/BaseEvent.php index 29866140..c582d3dd 100644 --- a/src/LINEBot/Event/BaseEvent.php +++ b/src/LINEBot/Event/BaseEvent.php @@ -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; } /** @@ -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; } @@ -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; } @@ -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; } diff --git a/src/LINEBot/Event/MessageEvent/ContentProvider.php b/src/LINEBot/Event/MessageEvent/ContentProvider.php index e61de52f..6df471bb 100644 --- a/src/LINEBot/Event/MessageEvent/ContentProvider.php +++ b/src/LINEBot/Event/MessageEvent/ContentProvider.php @@ -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; } /** @@ -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; } } diff --git a/src/LINEBot/Event/MessageEvent/LocationMessage.php b/src/LINEBot/Event/MessageEvent/LocationMessage.php index 0735e933..bc873a4c 100644 --- a/src/LINEBot/Event/MessageEvent/LocationMessage.php +++ b/src/LINEBot/Event/MessageEvent/LocationMessage.php @@ -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; } /** @@ -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; } /** diff --git a/src/LINEBot/Event/Parser/EventRequestParser.php b/src/LINEBot/Event/Parser/EventRequestParser.php index 2c248672..5a41dbeb 100644 --- a/src/LINEBot/Event/Parser/EventRequestParser.php +++ b/src/LINEBot/Event/Parser/EventRequestParser.php @@ -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; @@ -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(); } @@ -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); } diff --git a/src/LINEBot/Event/PostbackEvent.php b/src/LINEBot/Event/PostbackEvent.php index 3ab9e048..77228763 100644 --- a/src/LINEBot/Event/PostbackEvent.php +++ b/src/LINEBot/Event/PostbackEvent.php @@ -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; } diff --git a/src/LINEBot/Response.php b/src/LINEBot/Response.php index 70c4cbbd..eda424aa 100644 --- a/src/LINEBot/Response.php +++ b/src/LINEBot/Response.php @@ -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; diff --git a/tests/LINEBot/GetMemberIdsTest.php b/tests/LINEBot/GetMemberIdsTest.php index 168d9bc9..cecae795 100644 --- a/tests/LINEBot/GetMemberIdsTest.php +++ b/tests/LINEBot/GetMemberIdsTest.php @@ -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'); @@ -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');