Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="5"
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down
4 changes: 3 additions & 1 deletion src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public function newRequest(string $method, string $resource, array $headers = []

public function setRequest(RequestInterface $request): self
{
$request = $request->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');
Expand Down
4 changes: 1 addition & 3 deletions src/Clients/Traits/DocumentClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down
24 changes: 13 additions & 11 deletions src/PaginationClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Sysix\LexOffice;

use Psr\Http\Message\ResponseInterface;
use stdClass;

abstract class PaginationClient extends BaseClient
{
Expand Down Expand Up @@ -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;
}

Expand All @@ -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));
Expand Down
1 change: 1 addition & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion tests/PaginationClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class PaginationClientTest extends TestClient
{
/**
* @param Response[] $responses
* @param array<int, Response> $responses
*
* @return array{0: Api&MockObject, 1: PaginationClient}
*/
Expand Down
6 changes: 3 additions & 3 deletions tests/TestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function createApiMockObject(Response $response)
}

/**
* @param Response[] $responses
* @param array<int, Response> $responses
*
* @return Api&MockObject
*/
Expand Down Expand Up @@ -76,8 +76,8 @@ public function createClientMockObject(string $className): array
/**
* @template T of ClientInterface
*
* @param class-string<T> $className
* @param Response[] $responses
* @param class-string<T> $className
* @param array<int, Response> $responses
*
* @return array{0: Api&MockObject, 1: T&MockObject}
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function testGetJsonFromResponseWithValidHeader(): void
], (string) json_encode([
'test' => true
]));

/** @var object $json */
$json = Utils::getJsonFromResponse($response);

$this->assertEquals((object)[
Expand All @@ -50,6 +52,8 @@ public function testGetJsonFromResponseWithValidCharsetHeader(): void
], (string) json_encode([
'test' => true
]));

/** @var object $json */
$json = Utils::getJsonFromResponse($response);

$this->assertEquals((object)[
Expand All @@ -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)[
Expand Down