Skip to content
Open
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
15 changes: 0 additions & 15 deletions phpstan-baseline-7.4.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,6 @@ parameters:
count: 1
path: src/bundle/Controller/ContentDraftController.php

-
message: "#^Parameter \\#1 \\$array_arg of function usort expects TArray of array\\<Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\ContentType\\>, iterable\\<Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\ContentType\\> given\\.$#"
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: "#^Parameter \\#1 \\$s1 of function strnatcasecmp expects string, string\\|null given\\.$#"
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: "#^Parameter \\#2 \\$s2 of function strnatcasecmp expects string, string\\|null given\\.$#"
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, iterable\\<Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\ContentType\\> given\\.$#"
count: 1
Expand Down
15 changes: 0 additions & 15 deletions phpstan-baseline-8.0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ parameters:
count: 1
path: src/bundle/Controller/ContentDraftController.php

-
message: "#^Parameter \\#1 \\$array of function usort expects TArray of array\\<Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\ContentType\\>, iterable\\<Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\ContentType\\> given\\.$#"
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: "#^Parameter \\#1 \\$string1 of function strnatcasecmp expects string, string\\|null given\\.$#"
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: "#^Parameter \\#2 \\$string2 of function strnatcasecmp expects string, string\\|null given\\.$#"
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, iterable\\<Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\ContentType\\> given\\.$#"
count: 1
Expand Down
15 changes: 0 additions & 15 deletions phpstan-baseline-8.3.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ parameters:
count: 1
path: src/bundle/Controller/ContentDraftController.php

-
message: "#^Parameter \\#1 \\$array of function usort expects TArray of array\\<Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\ContentType\\>, iterable\\<Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\ContentType\\> given\\.$#"
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: "#^Parameter \\#1 \\$string1 of function strnatcasecmp expects string, string\\|null given\\.$#"
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: "#^Parameter \\#2 \\$string2 of function strnatcasecmp expects string, string\\|null given\\.$#"
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, iterable\\<Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\ContentType\\> given\\.$#"
count: 1
Expand Down
18 changes: 0 additions & 18 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,6 @@ parameters:
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: '#^Parameter \#1 \$array of class Pagerfanta\\Adapter\\ArrayAdapter constructor expects array, iterable\<Ibexa\\Contracts\\Core\\Repository\\Values\\ContentType\\ContentType\> given\.$#'
identifier: argument.type
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: '#^Parameter \#1 \$contentType of method Ibexa\\Bundle\\AdminUi\\Controller\\ContentTypeController\:\:tryToCreateContentTypeDraft\(\) expects Ibexa\\Contracts\\Core\\Repository\\Values\\ContentType\\ContentType, Ibexa\\Contracts\\Core\\Repository\\Values\\ContentType\\ContentType\|null given\.$#'
identifier: argument.type
Expand All @@ -336,12 +330,6 @@ parameters:
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: '#^Parameter \#1 \$contentTypes of method Ibexa\\Bundle\\AdminUi\\Controller\\ContentTypeController\:\:getContentTypesNumbers\(\) expects array\<Ibexa\\Contracts\\Core\\Repository\\Values\\ContentType\\ContentType\>, iterable\<\(int\|string\), mixed\> given\.$#'
identifier: argument.type
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: '#^Parameter \#2 \$handler of method Ibexa\\AdminUi\\Form\\SubmitHandler\:\:handle\(\) expects callable\(mixed\)\: \(Symfony\\Component\\HttpFoundation\\Response\|null\), Closure\(\)\: void given\.$#'
identifier: argument.type
Expand All @@ -354,12 +342,6 @@ parameters:
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: '#^Variable \$contentTypeGroupList in PHPDoc tag @var does not match assigned variable \$types\.$#'
identifier: varTag.differentVariable
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: '#^Variable \$newContentTypeDraft might not be defined\.$#'
identifier: variable.undefined
Expand Down
25 changes: 14 additions & 11 deletions src/bundle/Controller/ContentTypeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Ibexa\AdminUi\Form\Factory\FormFactory;
use Ibexa\AdminUi\Form\SubmitHandler;
use Ibexa\AdminUi\Form\Type\ContentType\ContentTypeUpdateType;
use Ibexa\AdminUi\Pagination\Pagerfanta\ContentTypeListAdapter;
use Ibexa\AdminUi\Service\MetaFieldType\MetaFieldDefinitionServiceInterface;
use Ibexa\AdminUi\Tab\ContentType\TranslationsTab;
use Ibexa\AdminUi\UI\Module\FieldTypeToolbar\FieldTypeToolbarFactory;
Expand All @@ -36,10 +37,13 @@
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentTypeDraft;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentTypeGroup;
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\ContentTypeQuery;
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\Criterion\ContentTypeGroupId;
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\SortClause\Name;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\Core\MVC\Symfony\Security\Authorization\Attribute;
use function Ibexa\PolyfillPhp82\iterator_to_array;
use JMS\TranslationBundle\Annotation\Desc;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Pagerfanta;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand Down Expand Up @@ -132,32 +136,31 @@ public function __construct(
public function listAction(ContentTypeGroup $group, string $routeName, int $page): Response
{
$deletableTypes = [];
$contentTypes = $this->contentTypeService->loadContentTypes($group, $this->configResolver->getParameter('languages'));

usort($contentTypes, static function (ContentType $contentType1, ContentType $contentType2) {
return strnatcasecmp($contentType1->getName(), $contentType2->getName());
});
$languages = $this->configResolver->getParameter('languages');
$limit = $this->configResolver->getParameter('pagination.content_type_limit');
$query = new ContentTypeQuery(new ContentTypeGroupId($group->id), [new Name()]);

$pagerfanta = new Pagerfanta(
new ArrayAdapter($contentTypes)
new ContentTypeListAdapter($this->contentTypeService, $languages, $query)
);

$pagerfanta->setMaxPerPage($this->configResolver->getParameter('pagination.content_type_limit'));
$pagerfanta->setMaxPerPage($limit);
$pagerfanta->setCurrentPage(min($page, $pagerfanta->getNbPages()));

/** @var \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentTypeGroup[] $contentTypeGroupList */
$types = $pagerfanta->getCurrentPageResults();

$deleteContentTypesForm = $this->formFactory->deleteContentTypes(
new ContentTypesDeleteData($this->getContentTypesNumbers($types))
new ContentTypesDeleteData($this->getContentTypesNumbers(
iterator_to_array($types)
))
);

foreach ($types as $type) {
$deletableTypes[$type->id] = !$this->contentTypeService->isContentTypeUsed($type);
}

$copyData = new ContentTypeCopyData(null, $group);
$contentTypeCopyForm = $this->contentTypeFormFactory->contentTypeCopy($copyData, null)->createView();
$contentTypeCopyForm = $this->contentTypeFormFactory->contentTypeCopy($copyData)->createView();

return $this->render('@ibexadesign/content_type/list.html.twig', [
'content_type_group' => $group,
Expand Down
54 changes: 54 additions & 0 deletions src/lib/Pagination/Pagerfanta/ContentTypeListAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Pagination\Pagerfanta;

use Ibexa\Contracts\Core\Repository\ContentTypeService;
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\ContentTypeQuery;
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\SortClause\Identifier;
use Pagerfanta\Adapter\AdapterInterface;

final class ContentTypeListAdapter implements AdapterInterface
{
private ContentTypeService $contentTypeService;

private ContentTypeQuery $query;

/** @var list<string> */
private array $languages;

/**
* @param list<string> $languages
*/
public function __construct(
ContentTypeService $contentTypeService,
array $languages,
?ContentTypeQuery $query = null
) {
$this->contentTypeService = $contentTypeService;
$this->languages = $languages;
$this->query = $query ?? new ContentTypeQuery(null, [new Identifier()]);
}

public function getNbResults(): int
{
$query = clone $this->query;
$query->setLimit(0);

return $this->contentTypeService->findContentTypes($query, $this->languages)->getTotalCount();
}

public function getSlice($offset, $length): iterable
{
$query = clone $this->query;
$query->setOffset($offset);
$query->setLimit($length);

return $this->contentTypeService->findContentTypes($query, $this->languages)->getIterator();
}
}
79 changes: 79 additions & 0 deletions tests/lib/Pagination/Pagerfanta/ContentTypeListAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\AdminUi\Pagination\Pagerfanta;

use Ibexa\AdminUi\Pagination\Pagerfanta\ContentTypeListAdapter;
use Ibexa\Contracts\Core\Repository\ContentTypeService;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\ContentTypeQuery;
use Ibexa\Contracts\Core\Repository\Values\ContentType\SearchResult;
use PHPUnit\Framework\TestCase;

final class ContentTypeListAdapterTest extends TestCase
{
/** @var \Ibexa\Contracts\Core\Repository\ContentTypeService&\PHPUnit\Framework\MockObject\MockObject */
private ContentTypeService $contentTypeService;

protected function setUp(): void
{
$this->contentTypeService = $this->createMock(ContentTypeService::class);
}

public function testGetNbResults(): void
{
$languages = ['en-GB', 'de-DE'];

$searchResults = new SearchResult([
'totalCount' => 10,
'items' => [],
]);

$this->contentTypeService
->expects(self::once())
->method('findContentTypes')
->with(self::isInstanceOf(ContentTypeQuery::class), $languages)
->willReturn($searchResults);

$adapter = new ContentTypeListAdapter($this->contentTypeService, $languages);

self::assertEquals(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self::assertEquals(
self::assertSame(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertSame doesn't seem to work, these won't be exactly the same types?

10,
$adapter->getNbResults()
);
}

public function testGetSlice(): void
{
$languages = ['en-GB'];
$offset = 5;
$limit = 25;

$item1 = $this->createMock(ContentType::class);
$item2 = $this->createMock(ContentType::class);
$items = [$item1, $item2];

$searchResults = new SearchResult([
'totalCount' => 2,
'items' => $items,
]);

$this->contentTypeService
->expects(self::once())
->method('findContentTypes')
->with($this->isInstanceOf(ContentTypeQuery::class), $languages)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may also want to make the check more specific, utilizing

self::callback(function (ContentTypeQuery $query): bool {
    // ...
    return true;
})

constraint.

Suggested change
->with($this->isInstanceOf(ContentTypeQuery::class), $languages)
->with(self::isInstanceOf(ContentTypeQuery::class), $languages)

->willReturn($searchResults);

$adapter = new ContentTypeListAdapter($this->contentTypeService, $languages);

self::assertEquals(
$searchResults->getIterator(),
$adapter->getSlice($offset, $limit)
);
}
}
Loading