Skip to content

Commit b991143

Browse files
Solve phpstan level 7 issue in case of UserMenuDto
1 parent 69a92e3 commit b991143

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

src/Config/UserMenu.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace EasyCorp\Bundle\EasyAdminBundle\Config;
44

55
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Menu\MenuItemInterface;
6+
use EasyCorp\Bundle\EasyAdminBundle\Dto\MenuItemDto;
67
use EasyCorp\Bundle\EasyAdminBundle\Dto\UserMenuDto;
78

89
/**
@@ -65,7 +66,12 @@ public function setGravatarEmail(string $emailAddress): self
6566
*/
6667
public function addMenuItems(array $items): self
6768
{
68-
$this->dto->setItems(array_merge($items, $this->dto->getItems()));
69+
$itemsDto = array_map(
70+
static fn (MenuItemInterface $item): MenuItemDto => $item->getAsDto(),
71+
$items
72+
);
73+
74+
$this->dto->setItems(array_merge($itemsDto, $this->dto->getItems()));
6975

7076
return $this;
7177
}
@@ -75,7 +81,12 @@ public function addMenuItems(array $items): self
7581
*/
7682
public function setMenuItems(array $items): self
7783
{
78-
$this->dto->setItems($items);
84+
$itemsDto = array_map(
85+
static fn (MenuItemInterface $item): MenuItemDto => $item->getAsDto(),
86+
$items
87+
);
88+
89+
$this->dto->setItems($itemsDto);
7990

8091
return $this;
8192
}

src/Dto/UserMenuDto.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace EasyCorp\Bundle\EasyAdminBundle\Dto;
44

5-
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Menu\MenuItemInterface;
6-
75
/**
86
* @author Javier Eguiluz <[email protected]>
97
*/
@@ -13,7 +11,7 @@ final class UserMenuDto
1311
private bool $displayAvatar = true;
1412
private ?string $name = null;
1513
private ?string $avatarUrl = null;
16-
/** @var MenuItemInterface[]|MenuItemDto[] */
14+
/** @var array<MenuItemDto> */
1715
private array $items = [];
1816

1917
public function isNameDisplayed(): bool
@@ -57,22 +55,33 @@ public function setAvatarUrl(?string $url): void
5755
}
5856

5957
/**
60-
* @return MenuItemInterface[]|MenuItemDto[]
58+
* @return array<MenuItemDto>
6159
*/
6260
public function getItems(): array
6361
{
6462
return $this->items;
6563
}
6664

6765
/**
68-
* When configuring the application, you are passed an array of
69-
* MenuItemInterface objects; after building the user menu contents,
70-
* this method is called with MenuItemDto objects.
71-
*
72-
* @param MenuItemInterface[]|MenuItemDto[] $items
66+
* @param array<MenuItemDto> $items
7367
*/
7468
public function setItems(array $items): void
7569
{
70+
foreach ($items as $item) {
71+
if (!$item instanceof MenuItemDto) {
72+
trigger_deprecation(
73+
'easycorp/easyadmin-bundle',
74+
'4.25.0',
75+
'Argument "%s" for "%s" must be one of these types: %s. Passing type %s will cause an error in 5.0.0.',
76+
'$items',
77+
__METHOD__,
78+
'"array<MenuItemDto>"',
79+
'"array<MenuItemInterface>"'
80+
);
81+
break;
82+
}
83+
}
84+
7685
$this->items = $items;
7786
}
7887
}

src/Factory/MenuFactory.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function createUserMenu(UserMenu $userMenu): UserMenuDto
5252
}
5353

5454
/**
55-
* @param MenuItemInterface[] $menuItems
55+
* @param array<MenuItemDto|MenuItemInterface> $menuItems
5656
*
5757
* @return MenuItemDto[]
5858
*/
@@ -63,14 +63,19 @@ private function buildMenuItems(array $menuItems): array
6363
$translationDomain = $adminContext->getI18n()->getTranslationDomain() ?? '';
6464

6565
$builtItems = [];
66-
foreach ($menuItems as $i => $menuItem) {
67-
$menuItemDto = $menuItem->getAsDto();
66+
foreach ($menuItems as $menuItem) {
67+
if ($menuItem instanceof MenuItemDto) {
68+
$menuItemDto = $menuItem;
69+
} else {
70+
$menuItemDto = $menuItem->getAsDto();
71+
}
72+
6873
if (false === $this->authChecker->isGranted(Permission::EA_VIEW_MENU_ITEM, $menuItemDto)) {
6974
continue;
7075
}
7176

7277
$subItems = [];
73-
foreach ($menuItemDto->getSubItems() as $j => $menuSubItemDto) {
78+
foreach ($menuItemDto->getSubItems() as $menuSubItemDto) {
7479
if (false === $this->authChecker->isGranted(Permission::EA_VIEW_MENU_ITEM, $menuSubItemDto)) {
7580
continue;
7681
}

0 commit comments

Comments
 (0)