Skip to content

Commit cfdcfd5

Browse files
Bump phpstan to level 7
1 parent 965149e commit cfdcfd5

File tree

17 files changed

+94
-36
lines changed

17 files changed

+94
-36
lines changed

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ includes:
22
- phpstan-baseline.neon
33

44
parameters:
5-
level: 6
5+
level: 7
66
paths:
77
- src/
88
excludePaths:

src/Config/Crud.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,18 +260,21 @@ public function setDecimalSeparator(string $separator): self
260260
*/
261261
public function setDefaultSort(array $sortFieldsAndOrder): self
262262
{
263-
$sortFieldsAndOrder = array_map('strtoupper', $sortFieldsAndOrder);
263+
$defaultSort = [];
264264
foreach ($sortFieldsAndOrder as $sortField => $sortOrder) {
265+
$sortOrder = strtoupper($sortOrder);
265266
if (!\in_array($sortOrder, [SortOrder::ASC, SortOrder::DESC], true)) {
266267
throw new \InvalidArgumentException(sprintf('The sort order can be only "%s" or "%s", "%s" given.', SortOrder::ASC, SortOrder::DESC, $sortOrder));
267268
}
268269

269270
if (!\is_string($sortField)) {
270271
throw new \InvalidArgumentException(sprintf('The keys of the array that defines the default sort must be strings with the field names, but the given "%s" value is a "%s".', $sortField, \gettype($sortField)));
271272
}
273+
274+
$defaultSort[$sortField] = $sortOrder;
272275
}
273276

274-
$this->dto->setDefaultSort($sortFieldsAndOrder);
277+
$this->dto->setDefaultSort($defaultSort);
275278

276279
return $this;
277280
}

src/Dto/FieldLayoutDto.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function getFields(): array
5959
*/
6060
public function getFieldsInTab(string $tabUniqueId): array
6161
{
62+
/** @phpstan-ignore-next-line return.type */
6263
return $this->fields[$tabUniqueId] ?? [];
6364
}
6465
}

src/EventListener/AdminRouterSubscriber.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ public function onKernelRequest(RequestEvent $event): void
163163

164164
// if this is a ugly URL from legacy EasyAdmin versions and the application
165165
// uses pretty URLs, redirect to the equivalent pretty URL
166-
if ($this->adminRouteGenerator->usesPrettyUrls() && null !== $entityFqcnOrCrudControllerFqcn = $request->query->get(EA::CRUD_CONTROLLER_FQCN)) {
166+
if ($this->adminRouteGenerator->usesPrettyUrls()) {
167+
/** @var class-string|null $entityFqcnOrCrudControllerFqcn */
168+
$entityFqcnOrCrudControllerFqcn = $request->query->get(EA::CRUD_CONTROLLER_FQCN);
167169
if (is_subclass_of($entityFqcnOrCrudControllerFqcn, CrudControllerInterface::class)) {
168170
$crudControllerFqcn = $entityFqcnOrCrudControllerFqcn;
169171
} else {
@@ -209,7 +211,9 @@ public function onKernelController(ControllerEvent $event): void
209211

210212
// if the request is related to a CRUD controller, change the controller to be executed
211213
if (null !== $crudControllerInstance = $this->getCrudControllerInstance($request)) {
214+
/** @var callable $symfonyControllerFqcnCallable */
212215
$symfonyControllerFqcnCallable = [$crudControllerInstance, $request->attributes->get(EA::CRUD_ACTION) ?? $request->query->get(EA::CRUD_ACTION)];
216+
/** @var callable $symfonyControllerStringCallable */
213217
$symfonyControllerStringCallable = [$crudControllerInstance::class, $request->attributes->get(EA::CRUD_ACTION) ?? $request->query->get(EA::CRUD_ACTION)];
214218

215219
// this makes Symfony believe that another controller is being executed

src/Factory/ActionFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private function processActionLabel(ActionDto $actionDto, ?EntityDto $entityDto,
171171
return;
172172
}
173173

174-
if (\is_callable($label) && $label instanceof \Closure) {
174+
if (!\is_string($label) && !$label instanceof TranslatableInterface && \is_callable($label)) {
175175
$label = \call_user_func_array($label, array_filter([$entityDto?->getInstance()], static fn ($item): bool => null !== $item));
176176

177177
if (!\is_string($label) && !$label instanceof TranslatableInterface) {

src/Factory/FormLayoutFactory.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormTabPaneCloseType;
1717
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormTabPaneGroupCloseType;
1818
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormTabPaneGroupOpenType;
19+
use Stringable;
1920
use Symfony\Component\String\Slugger\AsciiSlugger;
2021
use Symfony\Component\Uid\Ulid;
2122

@@ -78,7 +79,10 @@ private function validateLayoutConfiguration(FieldCollection $fields): void
7879
}
7980

8081
if ($theFirstFieldWhichIsATabOrColumn->isFormColumn() && $fieldDto->isFormTab()) {
81-
throw new \InvalidArgumentException(sprintf('When using form columns, you can\'t define tabs inside columns (but you can define columns inside tabs). Move the tab "%s" outside any column.', $fieldDto->getLabel()));
82+
$label = $fieldDto->getLabel();
83+
$labelAsString = (\is_string($label) || $label instanceof Stringable) ? (string) $label : '';
84+
85+
throw new \InvalidArgumentException(sprintf('When using form columns, you can\'t define tabs inside columns (but you can define columns inside tabs). Move the tab "%s" outside any column.', $labelAsString));
8286
}
8387
}
8488
}
@@ -181,7 +185,13 @@ private function linearizeLayoutConfiguration(FieldCollection $fields): void
181185

182186
if ($fieldDto->isFormTab()) {
183187
$isTabActive = 0 === \count($tabs);
184-
$tabId = sprintf('tab-%s', $fieldDto->getLabel() ? $slugger->slug(strip_tags($fieldDto->getLabel()))->lower()->toString() : ++$tabsWithoutLabelCounter);
188+
$label = $fieldDto->getLabel();
189+
$tabId = sprintf(
190+
'tab-%s',
191+
(\is_string($label) || $label instanceof Stringable)
192+
? $slugger->slug(strip_tags((string) $label))->lower()->toString()
193+
: ++$tabsWithoutLabelCounter
194+
);
185195
$fieldDto->setCustomOption(FormField::OPTION_TAB_ID, $tabId);
186196
$fieldDto->setCustomOption(FormField::OPTION_TAB_IS_ACTIVE, $isTabActive);
187197

@@ -395,6 +405,7 @@ public static function createFromFieldDtos(?FieldCollection $fieldDtos): FieldLa
395405
$tabs[$fieldDto->getUniqueId()] = $fieldDto;
396406
} else {
397407
if ($hasTabs) {
408+
/** @phpstan-ignore-next-line offsetAccess.nonOffsetAccessible */
398409
$fields[$currentTab->getUniqueId()][] = $fieldDto;
399410
} else {
400411
$fields[] = $fieldDto;

src/Factory/MenuFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use EasyCorp\Bundle\EasyAdminBundle\Dto\UserMenuDto;
1616
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGeneratorInterface;
1717
use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
18+
use Stringable;
1819
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1920
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
2021
use function Symfony\Component\Translation\t;
@@ -134,7 +135,10 @@ private function generateMenuItemUrl(MenuItemDto $menuItemDto): string
134135
$entityFqcn = $routeParameters[EA::ENTITY_FQCN] ?? null;
135136
$crudControllerFqcn = $routeParameters[EA::CRUD_CONTROLLER_FQCN] ?? null;
136137
if (null === $entityFqcn && null === $crudControllerFqcn) {
137-
throw new \RuntimeException(sprintf('The CRUD menu item with label "%s" must define either the entity FQCN (using the third constructor argument) or the CRUD Controller FQCN (using the "setController()" method).', $menuItemDto->getLabel()));
138+
$label = $menuItemDto->getLabel();
139+
$labelAsString = (\is_string($label) || $label instanceof Stringable) ? (string) $label : '';
140+
141+
throw new \RuntimeException(sprintf('The CRUD menu item with label "%s" must define either the entity FQCN (using the third constructor argument) or the CRUD Controller FQCN (using the "setController()" method).', $labelAsString));
138142
}
139143

140144
// 1. if CRUD controller is defined, use it...

src/Form/Type/CrudFormType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormRowType;
1414
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormTabPaneCloseType;
1515
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormTabPaneOpenType;
16+
use Stringable;
1617
use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser;
1718
use Symfony\Component\Form\AbstractType;
1819
use Symfony\Component\Form\FormBuilderInterface;
@@ -94,7 +95,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
9495
$metadata['label'] = $fieldDto->getLabel();
9596
$metadata['help'] = $fieldDto->getHelp();
9697
$metadata[FormField::OPTION_ICON] = $fieldDto->getCustomOption(FormField::OPTION_ICON);
97-
$currentFormTab = (string) $fieldDto->getLabel();
98+
99+
$label = $fieldDto->getLabel();
100+
$currentFormTab = (\is_string($label) || $label instanceof Stringable) ? (string) $label : '';
98101

99102
// plain arrays are not enough for tabs because they are modified in the
100103
// lifecycle of a form (e.g. add info about form errors). Use an ArrayObject instead.

src/Form/Type/FileUploadType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ public function configureOptions(OptionsResolver $resolver): void
9696

9797
$index = 1;
9898
$pathInfo = pathinfo($filename);
99-
while (file_exists($filename = sprintf('%s/%s_%d.%s', $pathInfo['dirname'], $pathInfo['filename'], $index, $pathInfo['extension']))) {
99+
100+
$basePath = isset($pathInfo['dirname']) ? sprintf('%s/%s', $pathInfo['dirname'], $pathInfo['filename']) : $pathInfo['filename'];
101+
$endPath = isset($pathInfo['extension']) ? '.'.$pathInfo['extension'] : '';
102+
while (file_exists($filename = sprintf('%s_%d%s', $basePath, $index, $endPath))) {
100103
++$index;
101104
}
102105

src/Intl/IntlFormatter.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,16 @@ public function formatNumber($number, array $attrs = [], string $style = 'decima
141141
$formatter = $this->createNumberFormatter($locale, $style, $attrs);
142142

143143
$ret = $formatter->format($number, self::NUMBER_TYPES[$type]);
144-
if (!\is_string($formatter->format($number, self::NUMBER_TYPES[$type]))) {
144+
if (!\is_string($ret)) {
145145
throw new RuntimeError('Unable to format the given number.');
146146
}
147147

148148
return $ret;
149149
}
150150

151151
/**
152-
* @param \DateTimeZone|string|bool|null $timezone The target timezone, null to use the default, false to leave unchanged
152+
* @param \DateTimeZone|string|bool|null $timezone The target timezone, null to use the default, false to leave
153+
* unchanged
153154
*/
154155
public function formatDateTime(?\DateTimeInterface $date, ?string $dateFormat = 'medium', ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', ?string $locale = null): ?string
155156
{
@@ -164,15 +165,17 @@ public function formatDateTime(?\DateTimeInterface $date, ?string $dateFormat =
164165
}
165166

166167
/**
167-
* @param \DateTimeZone|string|bool|null $timezone The target timezone, null to use the default, false to leave unchanged
168+
* @param \DateTimeZone|string|bool|null $timezone The target timezone, null to use the default, false to leave
169+
* unchanged
168170
*/
169171
public function formatDate(?\DateTimeInterface $date, ?string $dateFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', ?string $locale = null): ?string
170172
{
171173
return $this->formatDateTime($date, $dateFormat, 'none', $pattern, $timezone, $calendar, $locale);
172174
}
173175

174176
/**
175-
* @param \DateTimeZone|string|bool|null $timezone The target timezone, null to use the default, false to leave unchanged
177+
* @param \DateTimeZone|string|bool|null $timezone The target timezone, null to use the default, false to leave
178+
* unchanged
176179
*/
177180
public function formatTime(?\DateTimeInterface $date, ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', ?string $locale = null): ?string
178181
{
@@ -261,14 +264,17 @@ private function createNumberFormatter(?string $locale, string $style, array $at
261264

262265
$value = self::NUMBER_PADDING_ATTRIBUTES[$value];
263266
}
267+
/** @var int|float $value */
264268

265269
$this->numberFormatters[$hash]->setAttribute(self::NUMBER_ATTRIBUTES[$name], $value);
266270
}
267271

272+
/** @var string $value */
268273
foreach ($textAttrs as $name => $value) {
269274
$this->numberFormatters[$hash]->setTextAttribute(self::NUMBER_TEXT_ATTRIBUTES[$name], $value);
270275
}
271276

277+
/** @var string $value */
272278
foreach ($symbols as $name => $value) {
273279
$this->numberFormatters[$hash]->setSymbol(self::NUMBER_SYMBOLS[$name], $value);
274280
}

0 commit comments

Comments
 (0)