From 1df831beaa143c4b8c06265705533f4d663f343b Mon Sep 17 00:00:00 2001 From: Sysix Date: Sun, 11 Feb 2024 04:24:29 +0100 Subject: [PATCH 1/2] improve psalm level to 1 --- psalm.xml | 2 +- src/Api.php | 4 +++- src/Clients/Traits/DocumentClientTrait.php | 3 +-- src/PaginationClient.php | 23 ++++++++++++---------- src/Utils.php | 1 + tests/PaginationClientTest.php | 2 +- tests/TestClient.php | 6 +++--- tests/UtilsTest.php | 6 ++++++ 8 files changed, 29 insertions(+), 18 deletions(-) diff --git a/psalm.xml b/psalm.xml index 4b6b5a8..48ab4d8 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,6 +1,6 @@ withHeader('Authorization', 'Bearer ' . $this->apiKey); + if (!$request->hasHeader('Authorization')) { + $request = $request->withHeader('Authorization', 'Bearer ' . $this->apiKey); + } if (!$request->hasHeader('Accept')) { $request = $request->withHeader('Accept', 'application/json'); diff --git a/src/Clients/Traits/DocumentClientTrait.php b/src/Clients/Traits/DocumentClientTrait.php index a958d00..a6241e4 100644 --- a/src/Clients/Traits/DocumentClientTrait.php +++ b/src/Clients/Traits/DocumentClientTrait.php @@ -25,10 +25,9 @@ public function document(string $id, bool $asContent = false, string $acceptHead return $response; } - /** @var ?stdClass{documentField: string} $content */ $content = Utils::getJsonFromResponse($response); - if ($content === null) { + if ($content === null || !is_object($content) || !property_exists($content, 'documentFileId') || !is_string($content->documentFileId)) { return $response; } diff --git a/src/PaginationClient.php b/src/PaginationClient.php index 20ff384..f57fdc1 100644 --- a/src/PaginationClient.php +++ b/src/PaginationClient.php @@ -52,10 +52,13 @@ public function getAll(): ResponseInterface trigger_error(self::class . '::' . __METHOD__ . ' should not be called anymore, in future versions this method WILL not exist', E_USER_DEPRECATED); $response = $this->getPage(0); - /** @var ?stdClass{totalPages:int, content:stdClass[]} $result */ $result = Utils::getJsonFromResponse($response); - if ($result === null || $result->totalPages == 1) { + if ( + $result === null || !is_object($result) || + !property_exists($result, 'totalPages') || $result->totalPages == 1 || + !property_exists($result, 'content') + ) { return $response; } @@ -67,19 +70,19 @@ public function getAll(): ResponseInterface return $responsePage; } - /** @var ?stdClass{totalPages:int, content:stdClass[]} $resultPage */ $resultPage = Utils::getJsonFromResponse($responsePage); - if ($resultPage === null) { + if ( + $resultPage === null || + !is_object($resultPage) || + !property_exists($resultPage, 'content') || + !is_array($resultPage->content) || + !is_array($result->content) + ) { return $responsePage; } - foreach ($resultPage->content as $entity) { - $result->content = [ - ...$result->content, - $entity - ]; - } + array_push($result->content, ...$resultPage->content); } return $response->withBody(Utils::createStream($result)); diff --git a/src/Utils.php b/src/Utils.php index cd5194e..35eb4ab 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -61,6 +61,7 @@ public static function jsonEncode(mixed $value, int $options = 0, int $depth = 5 */ public static function jsonDecode(string $json, bool $assoc = false, int $depth = 512, int $options = 0): mixed { + /** @var mixed $data */ $data = json_decode($json, $assoc, $depth, $options); if (JSON_ERROR_NONE !== json_last_error()) { throw new InvalidArgumentException('json_decode error: ' . json_last_error_msg()); diff --git a/tests/PaginationClientTest.php b/tests/PaginationClientTest.php index c898802..dcdff92 100644 --- a/tests/PaginationClientTest.php +++ b/tests/PaginationClientTest.php @@ -12,7 +12,7 @@ class PaginationClientTest extends TestClient { /** - * @param Response[] $responses + * @param array $responses * * @return array{0: Api&MockObject, 1: PaginationClient} */ diff --git a/tests/TestClient.php b/tests/TestClient.php index eb33dda..592fb42 100644 --- a/tests/TestClient.php +++ b/tests/TestClient.php @@ -41,7 +41,7 @@ public function createApiMockObject(Response $response) } /** - * @param Response[] $responses + * @param array $responses * * @return Api&MockObject */ @@ -76,8 +76,8 @@ public function createClientMockObject(string $className): array /** * @template T of ClientInterface * - * @param class-string $className - * @param Response[] $responses + * @param class-string $className + * @param array $responses * * @return array{0: Api&MockObject, 1: T&MockObject} */ diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index c27f18d..8b453c8 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -36,6 +36,8 @@ public function testGetJsonFromResponseWithValidHeader(): void ], (string) json_encode([ 'test' => true ])); + + /** @var object $json */ $json = Utils::getJsonFromResponse($response); $this->assertEquals((object)[ @@ -50,6 +52,8 @@ public function testGetJsonFromResponseWithValidCharsetHeader(): void ], (string) json_encode([ 'test' => true ])); + + /** @var object $json */ $json = Utils::getJsonFromResponse($response); $this->assertEquals((object)[ @@ -59,6 +63,8 @@ public function testGetJsonFromResponseWithValidCharsetHeader(): void public function testJsonDecodeValid(): void { + + /** @var object $json */ $json = Utils::jsonDecode('{"content":"test","object":{"content":"test2"}}'); $this->assertEquals($json, (object)[ From 69faed3be455dad9930f681737cba6cb09123440 Mon Sep 17 00:00:00 2001 From: Sysix Date: Sun, 11 Feb 2024 04:26:01 +0100 Subject: [PATCH 2/2] php cs fix --- src/Clients/Traits/DocumentClientTrait.php | 1 - src/PaginationClient.php | 7 +++---- tests/UtilsTest.php | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Clients/Traits/DocumentClientTrait.php b/src/Clients/Traits/DocumentClientTrait.php index a6241e4..7baa134 100644 --- a/src/Clients/Traits/DocumentClientTrait.php +++ b/src/Clients/Traits/DocumentClientTrait.php @@ -5,7 +5,6 @@ namespace Sysix\LexOffice\Clients\Traits; use Psr\Http\Message\ResponseInterface; -use stdClass; use Sysix\LexOffice\Clients\File; use Sysix\LexOffice\Utils; diff --git a/src/PaginationClient.php b/src/PaginationClient.php index f57fdc1..b8e0afc 100644 --- a/src/PaginationClient.php +++ b/src/PaginationClient.php @@ -5,7 +5,6 @@ namespace Sysix\LexOffice; use Psr\Http\Message\ResponseInterface; -use stdClass; abstract class PaginationClient extends BaseClient { @@ -58,7 +57,7 @@ public function getAll(): ResponseInterface $result === null || !is_object($result) || !property_exists($result, 'totalPages') || $result->totalPages == 1 || !property_exists($result, 'content') - ) { + ) { return $response; } @@ -73,8 +72,8 @@ public function getAll(): ResponseInterface $resultPage = Utils::getJsonFromResponse($responsePage); if ( - $resultPage === null || - !is_object($resultPage) || + $resultPage === null || + !is_object($resultPage) || !property_exists($resultPage, 'content') || !is_array($resultPage->content) || !is_array($result->content) diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index 8b453c8..e518398 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -36,7 +36,7 @@ public function testGetJsonFromResponseWithValidHeader(): void ], (string) json_encode([ 'test' => true ])); - + /** @var object $json */ $json = Utils::getJsonFromResponse($response); @@ -63,7 +63,7 @@ public function testGetJsonFromResponseWithValidCharsetHeader(): void public function testJsonDecodeValid(): void { - + /** @var object $json */ $json = Utils::jsonDecode('{"content":"test","object":{"content":"test2"}}');