From f784e4f1a3918af90df52eae1982e937ce054604 Mon Sep 17 00:00:00 2001 From: Edoardo Dusi Date: Tue, 28 Jan 2025 11:22:32 +0100 Subject: [PATCH] chore: rector fixes --- src/Data/StoryData.php | 15 +++-------- src/Endpoints/StoryApi.php | 52 ++++++++----------------------------- src/MapiClient.php | 14 ---------- tests/Feature/StoryTest.php | 6 ++--- 4 files changed, 18 insertions(+), 69 deletions(-) diff --git a/src/Data/StoryData.php b/src/Data/StoryData.php index 0c29031..a0fab63 100644 --- a/src/Data/StoryData.php +++ b/src/Data/StoryData.php @@ -90,20 +90,13 @@ public function uuid(): string } /** - * Validates if the story data contains all required fields and valid values - * - * @return bool - */ + * Validates if the story data contains all required fields and valid values + */ public function isValid(): bool { - if (!$this->hasKey('name') || empty($this->getString('name'))) { + if (!$this->hasKey('name') || in_array($this->getString('name'), ['', '0'], true)) { return false; } - - if (!$this->hasKey('slug') || empty($this->getString('slug'))) { - return false; - } - - return true; + return $this->hasKey('slug') && !in_array($this->getString('slug'), ['', '0'], true); } } diff --git a/src/Endpoints/StoryApi.php b/src/Endpoints/StoryApi.php index 8426e9d..9b3aca4 100644 --- a/src/Endpoints/StoryApi.php +++ b/src/Endpoints/StoryApi.php @@ -20,28 +20,25 @@ */ class StoryApi extends EndpointSpace { - private const DEFAULT_ITEMS_PER_PAGE = 25; - private const DEFAULT_PAGE = 1; - private const RATE_LIMIT_STATUS_CODE = 429; - private const RETRY_DELAY = 1; - private const MAX_RETRIES = 3; + private const int DEFAULT_ITEMS_PER_PAGE = 25; - private LoggerInterface $logger; + private const int DEFAULT_PAGE = 1; + + private const int RATE_LIMIT_STATUS_CODE = 429; + + private const int RETRY_DELAY = 1; + + private const int MAX_RETRIES = 3; /** * StoryApi constructor. - * - * @param HttpClientInterface $httpClient - * @param string|int $spaceId - * @param LoggerInterface $logger */ public function __construct( HttpClientInterface $httpClient, string|int $spaceId, - LoggerInterface $logger, + private readonly LoggerInterface $logger, ) { parent::__construct($httpClient, $spaceId); - $this->logger = $logger; } /** @@ -82,10 +79,6 @@ public function all(int $itemsPerPage = 5): \Generator /** * Retrieves a specific page of stories - * - * @param int $page - * @param int $perPage - * @return StoryblokResponseInterface */ public function page( int $page = self::DEFAULT_PAGE, @@ -111,8 +104,6 @@ public function page( /** * Retrieves a specific story by ID * - * @param string $storyId - * @return StoryblokResponseInterface * @throws StoryblokApiException */ public function get(string $storyId): StoryblokResponseInterface @@ -129,8 +120,6 @@ public function get(string $storyId): StoryblokResponseInterface /** * Creates a new story * - * @param StoryData $storyData - * @return StoryblokResponseInterface * @throws InvalidStoryDataException */ public function create(StoryData $storyData): StoryblokResponseInterface @@ -156,9 +145,6 @@ public function create(StoryData $storyData): StoryblokResponseInterface /** * Updates an existing story * - * @param string $storyId - * @param StoryData $storyData - * @return StoryblokResponseInterface * @throws InvalidStoryDataException */ public function update(string $storyId, StoryData $storyData): StoryblokResponseInterface @@ -178,11 +164,6 @@ public function update(string $storyId, StoryData $storyData): StoryblokResponse /** * Handles successful API response - * - * @param StoryblokResponseInterface $response - * @param int|null $totalPages - * @param int $itemsPerPage - * @return int */ private function handleSuccessfulResponse( StoryblokResponseInterface $response, @@ -200,8 +181,6 @@ private function handleSuccessfulResponse( /** * Handles error responses from the API * - * @param StoryblokResponseInterface $response - * @param int $retryCount * @throws StoryblokApiException */ private function handleErrorResponse(StoryblokResponseInterface $response, int $retryCount): void @@ -233,7 +212,6 @@ private function handleRateLimit(): void /** * Extracts stories from the API response * - * @param StoryblokResponseInterface $response * @return \Generator */ private function getStoriesFromResponse(StoryblokResponseInterface $response): \Generator @@ -249,8 +227,6 @@ private function getStoriesFromResponse(StoryblokResponseInterface $response): \ /** * Validates pagination parameters * - * @param int $page - * @param int $perPage * @throws \InvalidArgumentException */ private function validatePaginationParams(int $page, int $perPage): void @@ -258,6 +234,7 @@ private function validatePaginationParams(int $page, int $perPage): void if ($page < 1) { throw new \InvalidArgumentException('Page number must be greater than 0'); } + if ($perPage < 1) { throw new \InvalidArgumentException('Items per page must be greater than 0'); } @@ -266,12 +243,11 @@ private function validatePaginationParams(int $page, int $perPage): void /** * Validates story ID * - * @param string $storyId * @throws \InvalidArgumentException */ private function validateStoryId(string $storyId): void { - if (empty($storyId)) { + if ($storyId === '' || $storyId === '0') { throw new \InvalidArgumentException('Story ID cannot be empty'); } } @@ -279,7 +255,6 @@ private function validateStoryId(string $storyId): void /** * Validates story data * - * @param StoryData $storyData * @throws InvalidStoryDataException */ private function validateStoryData(StoryData $storyData): void @@ -291,8 +266,6 @@ private function validateStoryData(StoryData $storyData): void /** * Builds the base endpoint for stories - * - * @return string */ private function buildStoriesEndpoint(): string { @@ -301,9 +274,6 @@ private function buildStoriesEndpoint(): string /** * Builds the endpoint for a specific story - * - * @param string $storyId - * @return string */ private function buildStoryEndpoint(string $storyId): string { diff --git a/src/MapiClient.php b/src/MapiClient.php index 7beb1d2..2cd0303 100644 --- a/src/MapiClient.php +++ b/src/MapiClient.php @@ -26,10 +26,6 @@ class MapiClient /** * MapiClient constructor. - * - * @param string $personalAccessToken - * @param Region $region - * @param string|null $baseUri */ public function __construct( string $personalAccessToken, @@ -51,11 +47,6 @@ public function __construct( /** * Initialize the MapiClient - * - * @param string $personalAccessToken - * @param Region $region - * @param string|null $baseUri - * @return MapiClient */ public static function init( string $personalAccessToken, @@ -84,11 +75,6 @@ public function spaceApi(): SpaceApi return new SpaceApi($this->httpClient); } - /** - * @param string|int $spaceId - * @param LoggerInterface|null $logger - * @return StoryApi - */ public function storyApi(string|int $spaceId, ?LoggerInterface $logger = null): StoryApi { return new StoryApi( diff --git a/tests/Feature/StoryTest.php b/tests/Feature/StoryTest.php index 55c4192..44b8e63 100644 --- a/tests/Feature/StoryTest.php +++ b/tests/Feature/StoryTest.php @@ -43,12 +43,12 @@ ]; // Create a callback to verify the request - $callback = function ($method, $url, $options) use ($expectedStoryData): void { + $callback = function ($method, $url, array $options) use ($expectedStoryData): void { expect($method)->toBe('POST'); expect($url)->toContain('/stories'); // Decode the request body and verify it matches expected structure - $requestBody = json_decode($options['body'], true); + $requestBody = json_decode((string) $options['body'], true); expect($requestBody)->toBeArray(); expect($requestBody)->toHaveKey('story'); expect($requestBody['story'])->toEqual($expectedStoryData); @@ -118,7 +118,7 @@ public function error(string|\Stringable $message, array $context = []): void // Use the all() method which we know triggers logging try { iterator_to_array($storyApi->all()); - } catch (\Exception $e) { + } catch (\Exception) { // Expected exception }