Skip to content

Commit 1360ad6

Browse files
IBX-9727: Aligned codebase with core content VO strict types changes (#185)
For more details see https://issues.ibexa.co/browse/IBX-9727 and #185 Key changes: * [ContentCreate] Added int cast to ownerId and sectionId strict properties * [ContentCreate] Defined array shape of ContentCreate::parse `$data` arg * [ContentCreate] Added missing `@throws` to ContentCreate::parse method * [UserCreate] Defined array shape of UserCreate::parse `$data` arg * [UserCreate] Added int cast to sectionId strict property * Added missing type hints to \Ibexa\Rest\Server\Values\RestContentCreateStruct * [Tests] Replaced literal FQCN usage with `RestContentCreateStruct::class` * [Tests] Dropped unnecessary assertions due to strict types changes * [UserGroupCreate] Defined array shape of UserGroupCreate::parse `$data` arg * [UserGroupCreate] Added int cast to sectionId strict property * [Tests] Defined non-nullable `remoteId` property for Location stub of LocationTest * [Tests] Fixed non-nullable `parentRemoteId` property for Location stub of RestLocationRootNodeTest * [Tests][PHPUnit] Configured UTC timezone for integration tests * Fixed incorrect default value assignment to Query properties in TrashItemListController --------- Co-Authored-By: Konrad Oboza <[email protected]>
1 parent be0b537 commit 1360ad6

File tree

10 files changed

+84
-55
lines changed

10 files changed

+84
-55
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -474,18 +474,6 @@ parameters:
474474
count: 1
475475
path: src/lib/Server/Controller/Services.php
476476

477-
-
478-
message: '#^Property Ibexa\\Contracts\\Core\\Repository\\Values\\Content\\Query\:\:\$limit \(int\) does not accept int\<0, max\>\|null\.$#'
479-
identifier: assign.propertyType
480-
count: 1
481-
path: src/lib/Server/Controller/Trash/TrashItemListController.php
482-
483-
-
484-
message: '#^Property Ibexa\\Contracts\\Core\\Repository\\Values\\Content\\Query\:\:\$offset \(int\) does not accept int\<0, max\>\|null\.$#'
485-
identifier: assign.propertyType
486-
count: 1
487-
path: src/lib/Server/Controller/Trash/TrashItemListController.php
488-
489477
-
490478
message: '#^Method Ibexa\\Rest\\Server\\Input\\Parser\\AbstractDestinationLocationParser\:\:parse\(\) has Ibexa\\Contracts\\Rest\\Exceptions\\Parser in PHPDoc @throws tag but it''s not thrown\.$#'
491479
identifier: throws.unusedType

phpunit.integration.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
beStrictAboutTodoAnnotatedTests="true"
66
verbose="true">
77
<php>
8+
<ini name="date.timezone" value="UTC" />
89
<env name="KERNEL_CLASS" value="Ibexa\Tests\Integration\Rest\IbexaTestKernel" />
910
<env name="SEARCH_ENGINE" value="legacy" />
1011
<env name="DATABASE_URL" value="sqlite://i@i/test.db" />

src/lib/Server/Controller/Trash/TrashItemListController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public function loadTrashItems(Request $request): Trash
8282
$limit = $request->query->has('limit') ? (int)$request->query->get('limit') : -1;
8383

8484
$query = new Query();
85-
$query->offset = $offset >= 0 ? $offset : null;
86-
$query->limit = $limit >= 0 ? $limit : null;
85+
$query->offset = max($offset, 0);
86+
$query->limit = max($limit, 0);
8787

8888
$trashItems = [];
8989

src/lib/Server/Input/Parser/ContentCreate.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,29 @@ public function __construct(
7373
/**
7474
* Parse input structure.
7575
*
76-
* @param array $data
77-
* @param \Ibexa\Contracts\Rest\Input\ParsingDispatcher $parsingDispatcher
76+
* @param array{
77+
* LocationCreate: array,
78+
* ContentType: array{_href: string},
79+
* mainLanguageCode: string,
80+
* Section?: array{_href: string},
81+
* alwaysAvailable?: bool|string,
82+
* remoteId?: string,
83+
* modificationDate?: string,
84+
* User?: array{_href: string},
85+
* fields: array{
86+
* field: array<
87+
* array{
88+
* fieldDefinitionIdentifier: string,
89+
* fieldValue: mixed,
90+
* languageCode?: string
91+
* }
92+
* >
93+
* }
94+
* } $data
7895
*
79-
* @return \Ibexa\Rest\Server\Values\RestContentCreateStruct
96+
* @throws \DateMalformedStringException
97+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
98+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
8099
*/
81100
public function parse(array $data, ParsingDispatcher $parsingDispatcher): RestContentCreateStruct
82101
{
@@ -109,7 +128,7 @@ public function parse(array $data, ParsingDispatcher $parsingDispatcher): RestCo
109128
throw new Exceptions\Parser("Missing '_href' attribute for the Section element in ContentCreate.");
110129
}
111130

112-
$contentCreateStruct->sectionId = $this->uriParser->getAttributeFromUri($data['Section']['_href'], 'sectionId');
131+
$contentCreateStruct->sectionId = (int)$this->uriParser->getAttributeFromUri($data['Section']['_href'], 'sectionId');
113132
}
114133

115134
if (array_key_exists('alwaysAvailable', $data)) {
@@ -129,7 +148,7 @@ public function parse(array $data, ParsingDispatcher $parsingDispatcher): RestCo
129148
throw new Exceptions\Parser("Missing '_href' attribute for the User element in ContentCreate.");
130149
}
131150

132-
$contentCreateStruct->ownerId = $this->uriParser->getAttributeFromUri($data['User']['_href'], 'userId');
151+
$contentCreateStruct->ownerId = (int)$this->uriParser->getAttributeFromUri($data['User']['_href'], 'userId');
133152
}
134153

135154
if (!array_key_exists('fields', $data) || !is_array($data['fields']) || !is_array($data['fields']['field'])) {

src/lib/Server/Input/Parser/UserCreate.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ public function __construct(UserService $userService, ContentTypeService $conten
5757
$this->parserTools = $parserTools;
5858
}
5959

60+
/**
61+
* @param array{
62+
* ContentType?: array{_href: string},
63+
* mainLanguageCode: string,
64+
* login: string,
65+
* email: string,
66+
* password: string,
67+
* Section?: array{_href: string},
68+
* remoteId?: string,
69+
* enabled?: bool|string,
70+
* fields: array{
71+
* field: array<
72+
* array{
73+
* fieldDefinitionIdentifier: string,
74+
* fieldValue: mixed,
75+
* languageCode?: string
76+
* }
77+
* >
78+
* }
79+
* } $data
80+
*
81+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
82+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
83+
*/
6084
public function parse(array $data, ParsingDispatcher $parsingDispatcher): UserCreateStruct
6185
{
6286
$contentType = null;
@@ -99,7 +123,7 @@ public function parse(array $data, ParsingDispatcher $parsingDispatcher): UserCr
99123
throw new Exceptions\Parser("Missing '_href' attribute for the Section element in UserCreate.");
100124
}
101125

102-
$userCreateStruct->sectionId = $this->uriParser->getAttributeFromUri($data['Section']['_href'], 'sectionId');
126+
$userCreateStruct->sectionId = (int)$this->uriParser->getAttributeFromUri($data['Section']['_href'], 'sectionId');
103127
}
104128

105129
if (array_key_exists('remoteId', $data)) {

src/lib/Server/Input/Parser/UserGroupCreate.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ public function __construct(UserService $userService, ContentTypeService $conten
4949
$this->fieldTypeParser = $fieldTypeParser;
5050
}
5151

52+
/**
53+
* @param array{
54+
* ContentType?: array{_href: string},
55+
* mainLanguageCode: string,
56+
* Section?: array{_href: string},
57+
* remoteId?: string,
58+
* fields: array{
59+
* field: array<
60+
* array{
61+
* fieldDefinitionIdentifier: string,
62+
* fieldValue: mixed,
63+
* languageCode?: string
64+
* }
65+
* >
66+
* }
67+
* } $data
68+
*
69+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
70+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
71+
*/
5272
public function parse(array $data, ParsingDispatcher $parsingDispatcher): UserGroupCreateStruct
5373
{
5474
$contentType = null;
@@ -73,7 +93,10 @@ public function parse(array $data, ParsingDispatcher $parsingDispatcher): UserGr
7393
throw new Exceptions\Parser("Missing '_href' attribute for the Section element in UserGroupCreate.");
7494
}
7595

76-
$userGroupCreateStruct->sectionId = $this->uriParser->getAttributeFromUri($data['Section']['_href'], 'sectionId');
96+
$userGroupCreateStruct->sectionId = (int)$this->uriParser->getAttributeFromUri(
97+
$data['Section']['_href'],
98+
'sectionId'
99+
);
77100
}
78101

79102
if (array_key_exists('remoteId', $data)) {

src/lib/Server/Values/RestContentCreateStruct.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,15 @@
1616
*/
1717
class RestContentCreateStruct extends ValueObject
1818
{
19-
/**
20-
* @var \Ibexa\Contracts\Core\Repository\Values\Content\ContentCreateStruct
21-
*/
22-
public $contentCreateStruct;
19+
public ContentCreateStruct $contentCreateStruct;
2320

24-
/**
25-
* @var \Ibexa\Contracts\Core\Repository\Values\Content\LocationCreateStruct
26-
*/
27-
public $locationCreateStruct;
21+
public LocationCreateStruct $locationCreateStruct;
2822

29-
/**
30-
* Construct.
31-
*
32-
* @param \Ibexa\Contracts\Core\Repository\Values\Content\ContentCreateStruct $contentCreateStruct
33-
* @param \Ibexa\Contracts\Core\Repository\Values\Content\LocationCreateStruct $locationCreateStruct
34-
*/
3523
public function __construct(ContentCreateStruct $contentCreateStruct, LocationCreateStruct $locationCreateStruct)
3624
{
3725
$this->contentCreateStruct = $contentCreateStruct;
3826
$this->locationCreateStruct = $locationCreateStruct;
27+
28+
parent::__construct();
3929
}
4030
}

tests/lib/Server/Input/Parser/ContentCreateTest.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Ibexa\Rest\Input\FieldTypeParser;
2020
use Ibexa\Rest\Server\Input\Parser\ContentCreate;
2121
use Ibexa\Rest\Server\Input\Parser\LocationCreate;
22+
use Ibexa\Rest\Server\Values\RestContentCreateStruct;
2223
use PHPUnit\Framework\MockObject\MockObject;
2324

2425
class ContentCreateTest extends BaseTest
@@ -57,23 +58,11 @@ public function testParse(): void
5758
$result = $contentCreate->parse($inputArray, $this->getParsingDispatcherMock());
5859

5960
self::assertInstanceOf(
60-
'\\Ibexa\\Rest\\Server\\Values\\RestContentCreateStruct',
61+
RestContentCreateStruct::class,
6162
$result,
6263
'ContentCreate not created correctly.'
6364
);
6465

65-
self::assertInstanceOf(
66-
'\\Ibexa\\Contracts\\Core\\Repository\\Values\\Content\\ContentCreateStruct',
67-
$result->contentCreateStruct,
68-
'contentCreateStruct not created correctly.'
69-
);
70-
71-
self::assertInstanceOf(
72-
'\\Ibexa\\Contracts\\Core\\Repository\\Values\\ContentType\\ContentType',
73-
$result->contentCreateStruct->contentType,
74-
'contentType not created correctly.'
75-
);
76-
7766
self::assertEquals(
7867
13,
7968
$result->contentCreateStruct->contentType->id,
@@ -86,12 +75,6 @@ public function testParse(): void
8675
'mainLanguageCode not created correctly'
8776
);
8877

89-
self::assertInstanceOf(
90-
'\\Ibexa\\Contracts\\Core\\Repository\\Values\\Content\\LocationCreateStruct',
91-
$result->locationCreateStruct,
92-
'locationCreateStruct not created correctly.'
93-
);
94-
9578
self::assertEquals(
9679
4,
9780
$result->contentCreateStruct->sectionId,

tests/lib/Server/Output/ValueObjectVisitor/LocationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function testVisitLocationAttributesResolvesMainLocation(
6262
'priority' => 1,
6363
'sortField' => ApiLocation::SORT_FIELD_DEPTH,
6464
'sortOrder' => ApiLocation::SORT_ORDER_ASC,
65+
'remoteId' => 'cefb8160c6e88d8ea5ae1f31e0c201fc',
6566
'parentLocationId' => 42,
6667
'contentInfo' => new ContentInfo([
6768
'id' => $contentId,

tests/lib/Server/Output/ValueObjectVisitor/RestLocationRootNodeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testVisit()
3636
'invisible' => true,
3737
'explicitlyHidden' => true,
3838
'remoteId' => 'remote-id',
39-
'parentLocationId' => null,
39+
'parentLocationId' => 1,
4040
'pathString' => '/1',
4141
'depth' => 3,
4242
'sortField' => Location::SORT_FIELD_PATH,

0 commit comments

Comments
 (0)