Skip to content

Commit a43f758

Browse files
committed
minor code update with new PHP 8.1 features
1 parent 97db2eb commit a43f758

File tree

10 files changed

+91
-66
lines changed

10 files changed

+91
-66
lines changed

src/Api/AbstractApi.php

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ private static function prepareJsonBody(array $params): string
131131
*/
132132
private static function prepareUri(string $uri, array $query = []): string
133133
{
134-
$query = array_filter($query, static function($value): bool {
135-
return null !== $value;
136-
});
134+
$query = array_filter($query, static fn($value): bool => null !== $value);
137135

138136
$httpQueryParameter = http_build_query($query);
139137
if ($httpQueryParameter !== '') {
@@ -193,28 +191,21 @@ protected function handleErrors(ResponseInterface $response): void
193191
return;
194192
}
195193

196-
switch ($statusCode) {
197-
case 400:
198-
throw HttpClientException::badRequest($response);
199-
case 401:
200-
throw HttpClientException::unauthorized($response);
201-
case 402:
202-
throw HttpClientException::requestFailed($response);
203-
case 403:
204-
throw HttpClientException::forbidden($response);
205-
case 404:
206-
throw HttpClientException::notFound($response);
207-
case 409:
208-
throw HttpClientException::conflict($response);
209-
case 413:
210-
throw HttpClientException::payloadTooLarge($response);
211-
case 429:
212-
throw HttpClientException::tooManyRequests($response);
213-
case 500 <= $statusCode:
214-
throw HttpServerException::serverError($response);
215-
default:
216-
throw new ConfluencePhpClientException($response->getBody()->getContents(), $response->getStatusCode());
194+
if (500 <= $statusCode) {
195+
throw HttpServerException::serverError($response);
217196
}
197+
198+
throw match ($statusCode) {
199+
400 => HttpClientException::badRequest($response),
200+
401 => HttpClientException::unauthorized($response),
201+
402 => HttpClientException::requestFailed($response),
202+
403 => HttpClientException::forbidden($response),
203+
404 => HttpClientException::notFound($response),
204+
409 => HttpClientException::conflict($response),
205+
413 => HttpClientException::payloadTooLarge($response),
206+
429 => HttpClientException::tooManyRequests($response),
207+
default => new ConfluencePhpClientException($response->getBody()->getContents(), $response->getStatusCode()),
208+
};
218209
}
219210

220211
/**

src/Api/Content.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
use CloudPlayDev\ConfluenceClient\Entity\ContentBody;
1010
use CloudPlayDev\ConfluenceClient\Entity\Hydratable;
1111
use CloudPlayDev\ConfluenceClient\Exception\ConfluencePhpClientException;
12-
use CloudPlayDev\ConfluenceClient\Exception\HttpServerException;
13-
use CloudPlayDev\ConfluenceClient\Exception\HydrationException;
1412
use Http\Client\Exception as HttpClientException;
1513
use JsonException;
1614
use Psr\Http\Message\ResponseInterface;
@@ -84,9 +82,7 @@ public function get(int $contentId, ?int $version = null): ?AbstractContent
8482
public function find(array $searchParameter, ?int $limit = null, ?int $start = null): ContentSearchResult
8583
{
8684
$allowedSearchParameter = ['title', 'spaceKey', 'type', 'id'];
87-
$queryParameter = array_filter($searchParameter, static function (string $searchKey) use ($allowedSearchParameter) {
88-
return in_array($searchKey, $allowedSearchParameter, true);
89-
}, ARRAY_FILTER_USE_KEY);
85+
$queryParameter = array_filter($searchParameter, static fn(string $searchKey): bool => in_array($searchKey, $allowedSearchParameter, true), ARRAY_FILTER_USE_KEY);
9086

9187
$queryParameter['expand'] = self::DEFAULT_EXPAND;
9288

@@ -161,9 +157,7 @@ public function create(AbstractContent $content): AbstractContent
161157
];
162158

163159
if (count($content->getAncestors()) > 0) {
164-
$ancestorsData = array_map(static function (int $id) {
165-
return ['id' => $id];
166-
}, $content->getAncestors());
160+
$ancestorsData = array_map(static fn(int $id) => ['id' => $id], $content->getAncestors());
167161

168162
$data['ancestors'] = $ancestorsData;
169163
}

src/Entity/AbstractContent.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public function addAncestor(int $id): self
271271
* @return AbstractContent|ContentPage|ContentComment
272272
* @throws HydrationException
273273
*/
274-
public static function load(array $data): self
274+
public static function load(array $data): ContentComment|AbstractContent|ContentPage
275275
{
276276
/* handle older content versions */
277277
if(isset($data['content'], $data['when'])) {
@@ -284,16 +284,11 @@ public static function load(array $data): self
284284
$data['_links']['self']));
285285
Assert::string($data['type']);
286286

287-
switch ($data['type']) {
288-
case Content::CONTENT_TYPE_PAGE:
289-
$content = new ContentPage();
290-
break;
291-
case Content::CONTENT_TYPE_COMMENT:
292-
$content = new ContentComment();
293-
break;
294-
default:
295-
throw new HydrationException('Invalid content type: ' . $data['type']);
296-
}
287+
$content = match ($data['type']) {
288+
Content::CONTENT_TYPE_PAGE => new ContentPage(),
289+
Content::CONTENT_TYPE_COMMENT => new ContentComment(),
290+
default => throw new HydrationException('Invalid content type: ' . $data['type']),
291+
};
297292

298293
$content->setId((int)$data['id']);
299294
$content->setTitle((string)$data['title']);

src/Exception/HttpClientException.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct(string $message, int $code, ResponseInterface $respo
2020

2121
public static function badRequest(ResponseInterface $response): HttpClientException
2222
{
23-
$validationMessage = self::extractValidationMessage($response, 'message');
23+
$validationMessage = self::extractValidationMessage($response);
2424

2525
$message = sprintf("The parameters passed to the API were invalid. Check your inputs!\n\n%s", $validationMessage);
2626

@@ -59,7 +59,7 @@ public static function tooManyRequests(ResponseInterface $response): HttpClientE
5959

6060
public static function forbidden(ResponseInterface $response): HttpClientException
6161
{
62-
$validationMessage = self::extractValidationMessage($response, 'message');
62+
$validationMessage = self::extractValidationMessage($response);
6363

6464
$message = sprintf("Forbidden!\n\n%s", $validationMessage);
6565

@@ -71,7 +71,7 @@ public static function forbidden(ResponseInterface $response): HttpClientExcepti
7171
* @param string $jsonField
7272
* @return string
7373
*/
74-
private static function extractValidationMessage(ResponseInterface $response, string $jsonField): string
74+
private static function extractValidationMessage(ResponseInterface $response, string $jsonField = 'message'): string
7575
{
7676

7777
$validationMessage = $response->getBody()->getContents();
@@ -84,7 +84,7 @@ private static function extractValidationMessage(ResponseInterface $response, st
8484
Assert::string($jsonDecoded[$jsonField]);
8585
$validationMessage = $jsonDecoded[$jsonField];
8686
}
87-
} catch (Throwable $e) {
87+
} catch (Throwable) {
8888
return $validationMessage;
8989
}
9090

tests/Api/ContentTest.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use CloudPlayDev\ConfluenceClient\Exception\HttpClientException;
1212
use CloudPlayDev\ConfluenceClient\Exception\HttpServerException;
1313
use Http\Client\Exception;
14+
use JsonException;
1415
use Webmozart\Assert\InvalidArgumentException;
15-
use function PHPUnit\Framework\assertEquals;
1616

1717
class ContentTest extends TestCase
1818
{
@@ -303,6 +303,52 @@ public function testCanCreatePage(): void
303303
self::assertInstanceOf(ContentPage::class, $page);
304304
}
305305

306+
public function testCanCreatePageWithAncestorsAndContainer(): void
307+
{
308+
$api = $this->getApiMock();
309+
310+
$data = [
311+
'type' => 'page',
312+
'title' => 'Test',
313+
'space' => [
314+
'key' => 'KEY'
315+
],
316+
'body' => [
317+
'storage' => [
318+
'value' => 'my text',
319+
'representation' => 'storage'
320+
]
321+
],
322+
'ancestors' => [
323+
['id' => 1],
324+
['id' => 2],
325+
['id' => 3]
326+
],
327+
'container' => [
328+
'id' => 123,
329+
'type' => 'blogpost'
330+
]
331+
];
332+
333+
$api->expects(self::once())
334+
->method('httpPost')
335+
->with('content', [], $data)
336+
->willReturn($this->createResponse(json_encode(self::PAGE_CONTENT, JSON_THROW_ON_ERROR)));
337+
338+
$content = new ContentPage();
339+
$content->setTitle('Test');
340+
$content->setVersion(1);
341+
$content->setContent('my text');
342+
$content->setSpace('KEY');
343+
$content->setContainerId(123);
344+
$content->setContainerType('blogpost');
345+
$content->setAncestors([1, 2, 3]);
346+
347+
$page = $api->create($content);
348+
349+
self::assertInstanceOf(ContentPage::class, $page);
350+
}
351+
306352
public function testCantCreateSavedPage(): void
307353
{
308354
$this->expectException(InvalidArgumentException::class);
@@ -350,7 +396,7 @@ public function testCanHandleHttpError400(int $errorCode, string $class): void
350396
/**
351397
* @throws Exception
352398
* @throws ConfluencePhpClientException
353-
* @throws \JsonException
399+
* @throws JsonException
354400
*/
355401
public function testCanExtractErrorMessageFromResponse(): void
356402
{

tests/Api/TestCase.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ abstract protected function getApiClass(): string;
2626
*
2727
* @return MockObject|Content
2828
*/
29-
protected function getApiMock(array $methods = []): MockObject
29+
protected function getApiMock(array $methods = []): MockObject|Content
3030
{
3131
$httpClient = $this->getMockBuilder(ClientInterface::class)
3232
->onlyMethods(['sendRequest'])
@@ -50,7 +50,8 @@ protected function getApiMock(array $methods = []): MockObject
5050
* @param string $contentType
5151
* @return mixed|MockObject|ResponseInterface
5252
*/
53-
protected function createResponse(string $responseString, int $responseCode = 200, string $contentType = 'application/json') {
53+
protected function createResponse(string $responseString, int $responseCode = 200, string $contentType = 'application/json'): mixed
54+
{
5455

5556
$streamInterface = $this->createMock(StreamInterface::class);
5657
$streamInterface->method('getContents')->willReturn($responseString);

tests/ConfluenceClientTest.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@
66
use CloudPlayDev\ConfluenceClient\ConfluenceClient;
77
use CloudPlayDev\ConfluenceClient\HttpClient\Builder;
88
use GuzzleHttp\Psr7\Uri;
9-
use Http\Client\Common\HttpMethodsClientInterface;
109
use Http\Discovery\Psr17FactoryDiscovery;
1110
use PHPUnit\Framework\TestCase;
1211
use Psr\Http\Message\UriFactoryInterface;
13-
use Psr\Http\Message\UriInterface;
1412

1513
class ConfluenceClientTest extends TestCase
1614
{
1715

1816
public function testCanUseCustomClientBuilder(): void
1917
{
2018
$builder = $this->createMock(Builder::class);
21-
$builder->expects($this->once())
19+
$builder->expects(static::once())
2220
->method('getHttpClient');
2321
$builder->method('getUriFactory')->willReturn(Psr17FactoryDiscovery::findUriFactory());
2422

@@ -29,10 +27,10 @@ public function testCanUseCustomClientBuilder(): void
2927
public function testCanUseBasicAuth(): void
3028
{
3129
$builder = $this->createMock(Builder::class);
32-
$builder->expects($this->atLeast(2))
30+
$builder->expects(static::atLeast(2))
3331
->method('addPlugin');
3432

35-
$builder->expects($this->atLeast(2))
33+
$builder->expects(static::atLeast(2))
3634
->method('removePlugin');
3735

3836
$builder->method('getUriFactory')->willReturn(Psr17FactoryDiscovery::findUriFactory());
@@ -44,10 +42,10 @@ public function testCanUseBasicAuth(): void
4442
public function testCanUseAuth(): void
4543
{
4644
$builder = $this->createMock(Builder::class);
47-
$builder->expects($this->atLeast(2))
45+
$builder->expects(static::atLeast(2))
4846
->method('addPlugin');
4947

50-
$builder->expects($this->atLeast(2))
48+
$builder->expects(static::atLeast(2))
5149
->method('removePlugin');
5250

5351
$builder->method('getUriFactory')->willReturn(Psr17FactoryDiscovery::findUriFactory());
@@ -59,10 +57,10 @@ public function testCanUseAuth(): void
5957
public function testCanUseUsernameAndPasswortInUri(): void
6058
{
6159
$builder = $this->createMock(Builder::class);
62-
$builder->expects($this->exactly(4))
60+
$builder->expects(static::exactly(4))
6361
->method('addPlugin');
6462

65-
$builder->expects($this->exactly(2))
63+
$builder->expects(static::exactly(2))
6664
->method('removePlugin');
6765

6866
$urlFactory = $this->createMock(UriFactoryInterface::class);

tests/Entity/ContentHistoryTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace CloudPlayDev\Tests\ConfluenceClient\Entity;
55

66
use CloudPlayDev\ConfluenceClient\Entity\ContentHistory;
7+
use DateTimeImmutable;
78
use PHPUnit\Framework\TestCase;
89

910
class ContentHistoryTest extends TestCase
@@ -18,8 +19,8 @@ public function testLoad()
1819
$contentHistory = ContentHistory::load($data);
1920

2021
self::assertInstanceOf(ContentHistory::class, $contentHistory);
21-
self::assertInstanceOf(\DateTimeImmutable::class, $contentHistory->getCreatedDate());
22-
self::assertInstanceOf(\DateTimeImmutable::class, $contentHistory->getUpdatedDate());
22+
self::assertInstanceOf(DateTimeImmutable::class, $contentHistory->getCreatedDate());
23+
self::assertInstanceOf(DateTimeImmutable::class, $contentHistory->getUpdatedDate());
2324
self::assertTrue($contentHistory->isLatest());
2425

2526
self::assertSame('atlassian', $contentHistory->getCreatedBy()->getAccountType());

tests/Entity/ContentTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace CloudPlayDev\Tests\ConfluenceClient\Entity;
1111

12-
use CloudPlayDev\ConfluenceClient\Entity\AbstractContent;
1312
use CloudPlayDev\ConfluenceClient\Entity\ContentPage;
1413
use PHPUnit\Framework\TestCase;
1514

tests/HttpClient/BuilderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
namespace CloudPlayDev\Tests\ConfluenceClient\HttpClient;
55

66
use CloudPlayDev\ConfluenceClient\HttpClient\Builder;
7-
use Http\Client\Common\HttpMethodsClientInterface;
87
use Http\Client\Common\Plugin;
8+
use PHPUnit\Framework\MockObject\MockObject;
99
use PHPUnit\Framework\TestCase;
1010
use Psr\Http\Client\ClientInterface;
1111
use Psr\Http\Message\RequestFactoryInterface;
@@ -21,9 +21,9 @@ class BuilderTest extends TestCase
2121
private Builder $subject;
2222

2323
/**
24-
* @var mixed|\PHPUnit\Framework\MockObject\MockObject|UriFactoryInterface
24+
* @var mixed|MockObject|UriFactoryInterface
2525
*/
26-
private $uriFactory;
26+
private mixed $uriFactory;
2727

2828
/**
2929
* @before

0 commit comments

Comments
 (0)