From a904c5dc6276880698053eeb9134951add236086 Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Thu, 2 Jan 2025 10:02:45 +0100 Subject: [PATCH] Fixes for phpstan and phpcs --- composer.json | 4 +++ phpstan-baseline.neon | 9 ------ src/PubNub/Endpoints/Endpoint.php | 8 +++--- .../MessageActions/AddMessageAction.php | 24 ++++++++-------- .../MessageActions/GetMessageAction.php | 18 ++++++------ .../MessageActions/RemoveMessageAction.php | 28 ++++++++++--------- src/PubNub/Enums/PNOperationType.php | 1 + .../PNAddMessageActionResult.php | 7 ++++- .../PNGetMessageActionResult.php | 7 +++-- .../MessageActions/PNMessageAction.php | 17 +++++++---- tests/integrational/MessageActionsTest.php | 6 ++-- 11 files changed, 72 insertions(+), 57 deletions(-) diff --git a/composer.json b/composer.json index 2e0f110..d10f99a 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,10 @@ "cp sdk-specifications/features/history/history-custom-mssg-type.feature tests/Acceptance/CustomMessageType/history-custom-mssg-type.feature", "cp sdk-specifications/features/subscribe/subscribe-custom-mssg-type.feature tests/Acceptance/Subscribe/subscribe-custom-mssg-type.feature", "vendor/bin/behat" + ], + "lint": [ + "vendor/bin/phpstan analyze --memory-limit 256M", + "git diff --name-only --diff-filter=d origin/master HEAD | grep -E '\\.php$' | xargs vendor/bin/phpcs --standard=PSR12" ] }, "require": { diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index c895362..bc22c40 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -605,10 +605,6 @@ parameters: count: 1 path: src/PubNub/Endpoints/Endpoint.php - - - message: "#^Method PubNub\\\\Endpoints\\\\Endpoint\\:\\:createStatus\\(\\) has parameter \\$category with no type specified\\.$#" - count: 1 - path: src/PubNub/Endpoints/Endpoint.php - message: "#^Method PubNub\\\\Endpoints\\\\Endpoint\\:\\:createStatus\\(\\) has parameter \\$response with no type specified\\.$#" @@ -700,11 +696,6 @@ parameters: count: 2 path: src/PubNub/Endpoints/Endpoint.php - - - message: "#^PHPDoc tag @param has invalid value \\(int\\{PNStatusCategory\\:\\:PNUnknownCategory\\.\\.PNStatusCategory\\:\\:PNRequestMessageCountExceededCategory\\} \\$category\\)\\: Unexpected token \"\\{\", expected variable at offset 21$#" - count: 1 - path: src/PubNub/Endpoints/Endpoint.php - - message: "#^Parameter \\#1 \\$result of method PubNub\\\\Endpoints\\\\Endpoint\\:\\:createResponse\\(\\) expects array, WpOrg\\\\Requests\\\\Response given\\.$#" count: 1 diff --git a/src/PubNub/Endpoints/Endpoint.php b/src/PubNub/Endpoints/Endpoint.php index 3b7d742..51b3363 100755 --- a/src/PubNub/Endpoints/Endpoint.php +++ b/src/PubNub/Endpoints/Endpoint.php @@ -130,7 +130,7 @@ protected function validateSubscribeKey() { $subscribeKey = $this->pubnub->getConfiguration()->getSubscribeKey(); - if ($subscribeKey == null || empty($subscribeKey)) { + if ($subscribeKey == null) { throw new PubNubValidationException("Subscribe Key not configured"); } } @@ -142,7 +142,7 @@ protected function validatePublishKey() { $publishKey = $this->pubnub->getConfiguration()->getPublishKey(); - if ($publishKey == null || empty($publishKey)) { + if ($publishKey == null) { throw new PubNubValidationException("Publish Key not configured"); } } @@ -154,7 +154,7 @@ protected function validateSecretKey() { $secretKey = $this->pubnub->getConfiguration()->getSecretKey(); - if ($secretKey === null || empty($secretKey)) { + if ($secretKey === null) { throw new PubNubValidationException("Secret key not configured"); } } @@ -554,7 +554,7 @@ protected function invokeRequest() } /** - * @param int{PNStatusCategory} $category + * @param int $category * @param $response * @param ResponseInfo | null $responseInfo * @param PubNubException | null $exception diff --git a/src/PubNub/Endpoints/MessageActions/AddMessageAction.php b/src/PubNub/Endpoints/MessageActions/AddMessageAction.php index fbb71b5..0c75d23 100644 --- a/src/PubNub/Endpoints/MessageActions/AddMessageAction.php +++ b/src/PubNub/Endpoints/MessageActions/AddMessageAction.php @@ -2,10 +2,12 @@ namespace PubNub\Endpoints\MessageActions; +use PubNub\PubNub; use PubNub\Endpoints\Endpoint; use PubNub\Enums\PNHttpMethod; use PubNub\Enums\PNOperationType; use PubNub\Exceptions\PubNubValidationException; +use PubNub\Exceptions\PubNubBuildRequestException; use PubNub\Models\Consumer\MessageActions\PNMessageAction; use PubNub\Models\Consumer\MessageActions\PNAddMessageActionResult; use PubNub\PubNubUtil; @@ -23,7 +25,7 @@ class AddMessageAction extends Endpoint protected string $channel; protected PNMessageAction $messageAction; - public function __construct($pubnub) + public function __construct(PubNub $pubnub) { parent::__construct($pubnub); $this->endpointConnectTimeout = $this->pubnub->getConfiguration()->getConnectTimeout(); @@ -57,7 +59,7 @@ public function messageAction(PNMessageAction $messageAction): self /** * @throws PubNubValidationException */ - protected function validateParams() + protected function validateParams(): void { if (!$this->channel) { throw new PubNubValidationException("Channel Missing"); @@ -72,13 +74,13 @@ protected function validateParams() */ protected function validateMessageAction(): void { - if (!$this->messageAction) { + if (!isset($this->messageAction)) { throw new PubNubValidationException("Message Action Missing"); } - if (!$this->messageAction->type) { + if (!isset($this->messageAction->type)) { throw new PubNubValidationException("Message Action Type Missing"); } - if (!$this->messageAction->value) { + if (!isset($this->messageAction->value)) { throw new PubNubValidationException("Message Action Value Missing"); } if (!$this->messageAction->messageTimetoken) { @@ -87,7 +89,7 @@ protected function validateMessageAction(): void } /** - * @return array + * @return array */ protected function customParams() { @@ -97,7 +99,7 @@ protected function customParams() } /** - * @return array + * @return array */ protected function customHeaders() { @@ -108,7 +110,7 @@ protected function customHeaders() } /** - * @return array + * @return string | null */ protected function buildData() { @@ -141,11 +143,11 @@ public function sync(): PNAddMessageActionResult } /** - * @param array $json Decoded json - * @return PNPublishResult + * @param array $json Decoded json + * @return PNAddMessageActionResult */ protected function createResponse($json): PNAddMessageActionResult { - return PNAddMessageActionResult::fromJson($json, $this->pubnub->getCrypto()); + return PNAddMessageActionResult::fromJson($json); } } diff --git a/src/PubNub/Endpoints/MessageActions/GetMessageAction.php b/src/PubNub/Endpoints/MessageActions/GetMessageAction.php index 97abb20..185ab46 100644 --- a/src/PubNub/Endpoints/MessageActions/GetMessageAction.php +++ b/src/PubNub/Endpoints/MessageActions/GetMessageAction.php @@ -2,10 +2,12 @@ namespace PubNub\Endpoints\MessageActions; +use PubNub\PubNub; use PubNub\Endpoints\Endpoint; use PubNub\Enums\PNHttpMethod; use PubNub\Enums\PNOperationType; use PubNub\Exceptions\PubNubValidationException; +use PubNub\Exceptions\PubNubBuildRequestException; use PubNub\Models\Consumer\MessageActions\PNGetMessageActionResult; /** @package PubNub\Endpoints\MessageActions */ @@ -24,7 +26,7 @@ class GetMessageAction extends Endpoint protected ?string $end; protected ?int $limit; - public function __construct($pubnub) + public function __construct(PubNub $pubnub) { parent::__construct($pubnub); $this->endpointConnectTimeout = $this->pubnub->getConfiguration()->getConnectTimeout(); @@ -82,7 +84,7 @@ public function setLimit(int $limit): self /** * @throws PubNubValidationException */ - protected function validateParams() + protected function validateParams(): void { if (!$this->channel) { throw new PubNubValidationException("Channel Missing"); @@ -92,7 +94,7 @@ protected function validateParams() } /** - * @return array + * @return array */ protected function customParams() { @@ -112,11 +114,11 @@ protected function customParams() } /** - * @return array + * @return string | null */ protected function buildData() { - return []; + return null; } /** @@ -141,11 +143,11 @@ public function sync(): PNGetMessageActionResult } /** - * @param array $json Decoded json - * @return PNPublishResult + * @param array $json Decoded json + * @return PNGetMessageActionResult */ protected function createResponse($json): PNGetMessageActionResult { - return PNGetMessageActionResult::fromJson($json, $this->pubnub->getCrypto()); + return PNGetMessageActionResult::fromJson($json); } } diff --git a/src/PubNub/Endpoints/MessageActions/RemoveMessageAction.php b/src/PubNub/Endpoints/MessageActions/RemoveMessageAction.php index bc4603d..43d2732 100644 --- a/src/PubNub/Endpoints/MessageActions/RemoveMessageAction.php +++ b/src/PubNub/Endpoints/MessageActions/RemoveMessageAction.php @@ -2,10 +2,12 @@ namespace PubNub\Endpoints\MessageActions; +use PubNub\PubNub; use PubNub\Endpoints\Endpoint; use PubNub\Enums\PNHttpMethod; use PubNub\Enums\PNOperationType; use PubNub\Exceptions\PubNubValidationException; +use PubNub\Exceptions\PubNubBuildRequestException; use PubNub\Models\Consumer\MessageActions\PNRemoveMessageActionResult; /** @package PubNub\Endpoints\MessageActions */ @@ -20,10 +22,10 @@ class RemoveMessageAction extends Endpoint protected const DELETE_PATH = "/v1/message-actions/%s/channel/%s/message/%s/action/%s"; protected string $channel; - protected string $messageTimetoken; - protected string $actionTimetoken; + protected int | float $messageTimetoken; + protected int | float $actionTimetoken; - public function __construct($pubnub) + public function __construct(PubNub $pubnub) { parent::__construct($pubnub); $this->endpointConnectTimeout = $this->pubnub->getConfiguration()->getConnectTimeout(); @@ -45,10 +47,10 @@ public function channel(string $channel): self /** * The publish timetoken of a parent message. * - * @param string $messageTimetoken + * @param int | float $messageTimetoken * @return RemoveMessageAction */ - public function messageTimetoken(string $messageTimetoken): self + public function messageTimetoken(int | float $messageTimetoken): self { $this->messageTimetoken = $messageTimetoken; return $this; @@ -57,10 +59,10 @@ public function messageTimetoken(string $messageTimetoken): self /** * The publish timetoken of the reaction. * - * @param string $actionTimetoken + * @param int | float $actionTimetoken * @return RemoveMessageAction */ - public function actionTimetoken(string $actionTimetoken): self + public function actionTimetoken(int | float $actionTimetoken): self { $this->actionTimetoken = $actionTimetoken; return $this; @@ -69,7 +71,7 @@ public function actionTimetoken(string $actionTimetoken): self /** * @throws PubNubValidationException */ - protected function validateParams() + protected function validateParams(): void { if (!$this->channel) { throw new PubNubValidationException("Channel Missing"); @@ -86,7 +88,7 @@ protected function validateParams() } /** - * @return array + * @return array */ protected function customParams() { @@ -94,11 +96,11 @@ protected function customParams() } /** - * @return array + * @return null|string */ protected function buildData() { - return []; + return null; } /** @@ -125,8 +127,8 @@ public function sync(): PNRemoveMessageActionResult } /** - * @param array $json Decoded json - * @return PNPublishResult + * @param mixed $json Decoded json + * @return PNRemoveMessageActionResult */ protected function createResponse($json): PNRemoveMessageActionResult { diff --git a/src/PubNub/Enums/PNOperationType.php b/src/PubNub/Enums/PNOperationType.php index 16abb3f..93c12fd 100755 --- a/src/PubNub/Enums/PNOperationType.php +++ b/src/PubNub/Enums/PNOperationType.php @@ -1,5 +1,6 @@ $messageAction + * @return void + */ + public function __construct(?array $messageAction = null) { if ($messageAction != null) { $this->type = $messageAction['type']; diff --git a/tests/integrational/MessageActionsTest.php b/tests/integrational/MessageActionsTest.php index 7c1a3b4..12c2c5f 100644 --- a/tests/integrational/MessageActionsTest.php +++ b/tests/integrational/MessageActionsTest.php @@ -8,7 +8,7 @@ class MessageActionsTest extends \PubNubTestCase { - public function testAddMessageAction() + public function testAddMessageAction(): void { $publishResult = $this->pubnub->publish() ->channel("pizza_talks") @@ -35,7 +35,7 @@ public function testAddMessageAction() $this->assertEquals($messageTimetoken, $addMessageActionResult->messageTimetoken); } - public function testGetMessageAction() + public function testGetMessageAction(): void { $publishResult = $this->pubnub->publish() ->channel("pizza_talks") @@ -63,7 +63,7 @@ public function testGetMessageAction() $this->assertEquals("angry_face", $messageAction->value); } - public function testDeleteMessageAction() + public function testDeleteMessageAction(): void { $getMessageActionResult = $this->pubnub->getMessageAction() ->channel("pizza_talks")