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..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; @@ -25,10 +24,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..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 { @@ -52,10 +51,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 +69,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..e518398 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)[