Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c249254

Browse files
VincentLangletjaviereguiluz
authored andcommitted
Solve 40 PHPStan errors level 7
1 parent 69a92e3 commit c249254

36 files changed

Lines changed: 218 additions & 71 deletions

src/Collection/ActionCollection.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
/**
99
* @author Javier Eguiluz <[email protected]>
10+
*
11+
* @implements CollectionInterface<string, ActionDto>
1012
*/
1113
final class ActionCollection implements CollectionInterface
1214
{
1315
/**
14-
* @param ActionDto[] $actions
16+
* @param array<string, ActionDto> $actions
1517
*/
1618
private function __construct(private array $actions)
1719
{
@@ -25,15 +27,15 @@ public function __clone()
2527
}
2628

2729
/**
28-
* @param ActionDto[] $actions
30+
* @param array<string, ActionDto> $actions
2931
*/
3032
public static function new(array $actions): self
3133
{
3234
return new self($actions);
3335
}
3436

3537
/**
36-
* @return ActionDto[]
38+
* @return array<string, ActionDto>
3739
*/
3840
public function all(): array
3941
{
@@ -71,7 +73,7 @@ public function count(): int
7173
}
7274

7375
/**
74-
* @return \ArrayIterator<ActionDto>
76+
* @return \ArrayIterator<string, ActionDto>
7577
*/
7678
public function getIterator(): \ArrayIterator
7779
{

src/Collection/EntityCollection.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77

88
/**
99
* @author Javier Eguiluz <[email protected]>
10+
*
11+
* @implements CollectionInterface<string, EntityDto>
1012
*/
1113
final class EntityCollection implements CollectionInterface
1214
{
1315
/**
14-
* @param EntityDto[] $entities
16+
* @param array<string, EntityDto> $entities
1517
*/
1618
private function __construct(private array $entities)
1719
{
1820
}
1921

2022
/**
21-
* @param EntityDto[] $entities
23+
* @param array<string, EntityDto> $entities
2224
*/
2325
public static function new(array $entities): self
2426
{
@@ -66,7 +68,7 @@ public function count(): int
6668
}
6769

6870
/**
69-
* @return \ArrayIterator<EntityDto>
71+
* @return \ArrayIterator<string, EntityDto>
7072
*/
7173
public function getIterator(): \ArrayIterator
7274
{

src/Collection/FieldCollection.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99

1010
/**
1111
* @author Javier Eguiluz <[email protected]>
12+
*
13+
* @implements CollectionInterface<string, FieldDto>
1214
*/
1315
final class FieldCollection implements CollectionInterface
1416
{
15-
/** @var FieldDto[] */
17+
/** @var array<string, FieldDto> */
1618
private array $fields;
1719

1820
/**
19-
* @param FieldInterface[]|string[] $fields
21+
* @param array<FieldInterface|string> $fields
2022
*/
2123
private function __construct(iterable $fields)
2224
{
@@ -35,7 +37,7 @@ public function __clone()
3537
}
3638

3739
/**
38-
* @param FieldInterface[]|string[] $fields
40+
* @param array<FieldInterface|string> $fields
3941
*/
4042
public static function new(iterable $fields): self
4143
{
@@ -138,17 +140,17 @@ public function count(): int
138140
}
139141

140142
/**
141-
* @return \ArrayIterator<FieldDto>
143+
* @return \ArrayIterator<string, FieldDto>
142144
*/
143145
public function getIterator(): \ArrayIterator
144146
{
145147
return new \ArrayIterator($this->fields);
146148
}
147149

148150
/**
149-
* @param FieldInterface[]|string[] $fields
151+
* @param array<FieldInterface|string> $fields
150152
*
151-
* @return FieldDto[]
153+
* @return array<string, FieldDto>
152154
*/
153155
private function processFields(iterable $fields): array
154156
{

src/Collection/FilterCollection.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,28 @@
77

88
/**
99
* @author Javier Eguiluz <[email protected]>
10+
*
11+
* @implements CollectionInterface<string, FilterDto>
1012
*/
1113
final class FilterCollection implements CollectionInterface
1214
{
1315
/**
14-
* @param FilterDto[] $filters
16+
* @param array<string, FilterDto> $filters
1517
*/
1618
private function __construct(private array $filters)
1719
{
1820
}
1921

2022
/**
21-
* @param FilterDto[] $filters
23+
* @param array<string, FilterDto> $filters
2224
*/
2325
public static function new(array $filters = []): self
2426
{
2527
return new self($filters);
2628
}
2729

2830
/**
29-
* @return FilterDto[]
31+
* @return array<string, FilterDto>
3032
*/
3133
public function all(): array
3234
{
@@ -64,7 +66,7 @@ public function count(): int
6466
}
6567

6668
/**
67-
* @return \ArrayIterator<FilterDto>
69+
* @return \ArrayIterator<string, FilterDto>
6870
*/
6971
public function getIterator(): \ArrayIterator
7072
{

src/Contracts/Collection/CollectionInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
/**
66
* @author Javier Eguiluz <[email protected]>
7+
*
8+
* @template TKey
9+
* @template TValue
10+
*
11+
* @extends \ArrayAccess<TKey, TValue>
12+
* @extends \IteratorAggregate<TKey, TValue>
713
*/
814
interface CollectionInterface extends \ArrayAccess, \Countable, \IteratorAggregate
915
{

src/Controller/AbstractCrudController.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ public function edit(AdminContext $context)
222222
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_EDIT)));
223223
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
224224
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
225+
/** @var TEntity $entityInstance */
225226
$entityInstance = $context->getEntity()->getInstance();
226227

227228
if ($context->getRequest()->isXmlHttpRequest()) {
@@ -301,14 +302,17 @@ public function new(AdminContext $context)
301302
throw new InsufficientEntityPermissionException($context);
302303
}
303304

304-
$context->getEntity()->setInstance($this->createEntity($context->getEntity()->getFqcn()));
305+
/** @var class-string<TEntity> $entityFqcn */
306+
$entityFqcn = $context->getEntity()->getFqcn();
307+
$context->getEntity()->setInstance($this->createEntity($entityFqcn));
305308
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_NEW)));
306309
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
307310
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
308311

309312
$newForm = $this->createNewForm($context->getEntity(), $context->getCrud()->getNewFormOptions(), $context);
310313
$newForm->handleRequest($context->getRequest());
311314

315+
/** @var TEntity $entityInstance */
312316
$entityInstance = $newForm->getData();
313317
$context->getEntity()->setInstance($entityInstance);
314318

@@ -359,11 +363,14 @@ public function delete(AdminContext $context)
359363
throw new InsufficientEntityPermissionException($context);
360364
}
361365

362-
$csrfToken = $context->getRequest()->request->get('token');
366+
$csrfToken = $context->getRequest()->request->has('token')
367+
? (string) $context->getRequest()->request->get('token')
368+
: null;
363369
if ($this->container->has('security.csrf.token_manager') && !$this->isCsrfTokenValid('ea-delete', $csrfToken)) {
364370
return $this->redirectToRoute($context->getDashboardRouteName());
365371
}
366372

373+
/** @var TEntity $entityInstance */
367374
$entityInstance = $context->getEntity()->getInstance();
368375

369376
$event = new BeforeEntityDeletedEvent($entityInstance);
@@ -394,6 +401,9 @@ public function delete(AdminContext $context)
394401
return $this->redirect($this->container->get(AdminUrlGenerator::class)->setController($context->getCrud()->getControllerFqcn())->setAction(Action::INDEX)->unset(EA::ENTITY_ID)->generateUrl());
395402
}
396403

404+
/**
405+
* @param BatchActionDto<TEntity> $batchActionDto
406+
*/
397407
public function batchDelete(AdminContext $context, BatchActionDto $batchActionDto): Response
398408
{
399409
$event = new BeforeCrudActionEvent($context);
@@ -406,11 +416,12 @@ public function batchDelete(AdminContext $context, BatchActionDto $batchActionDt
406416
return $this->redirectToRoute($context->getDashboardRouteName());
407417
}
408418

419+
/** @var EntityManagerInterface $entityManager */
409420
$entityManager = $this->container->get('doctrine')->getManagerForClass($batchActionDto->getEntityFqcn());
410421
$repository = $entityManager->getRepository($batchActionDto->getEntityFqcn());
411422
foreach ($batchActionDto->getEntityIds() as $entityId) {
412423
$entityInstance = $repository->find($entityId);
413-
if (!$entityInstance) {
424+
if (null === $entityInstance) {
414425
continue;
415426
}
416427

@@ -559,6 +570,9 @@ protected function getContext(): ?AdminContext
559570
return $this->container->get(AdminContextProvider::class)->getContext();
560571
}
561572

573+
/**
574+
* @param EntityDto<TEntity> $entityDto
575+
*/
562576
protected function ajaxEdit(EntityDto $entityDto, ?string $propertyName, bool $newValue): AfterCrudActionEvent
563577
{
564578
$field = $entityDto->getFields()->getByProperty($propertyName);
@@ -568,7 +582,9 @@ protected function ajaxEdit(EntityDto $entityDto, ?string $propertyName, bool $n
568582

569583
$this->container->get(EntityUpdater::class)->updateProperty($entityDto, $propertyName, $newValue);
570584

571-
$event = new BeforeEntityUpdatedEvent($entityDto->getInstance());
585+
/** @var TEntity $entityInstance */
586+
$entityInstance = $entityDto->getInstance();
587+
$event = new BeforeEntityUpdatedEvent($entityInstance);
572588
$this->container->get('event_dispatcher')->dispatch($event);
573589
$entityInstance = $event->getEntityInstance();
574590

src/DependencyInjection/CreateControllerRegistriesPass.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ private function createDashboardControllerRegistryService(ContainerBuilder $cont
2626
{
2727
$dashboardControllersFqcn = array_keys($container->findTaggedServiceIds(EasyAdminExtension::TAG_DASHBOARD_CONTROLLER, true));
2828

29+
$secret = $container->getParameter('kernel.secret');
30+
\assert(\is_string($secret));
31+
2932
$controllerFqcnToContextIdMap = [];
3033
foreach ($dashboardControllersFqcn as $controllerFqcn) {
31-
$controllerFqcnToContextIdMap[$controllerFqcn] = substr(sha1($container->getParameter('kernel.secret').$controllerFqcn), 0, 7);
34+
$controllerFqcnToContextIdMap[$controllerFqcn] = substr(sha1($secret.$controllerFqcn), 0, 7);
3235
}
3336

3437
$container
@@ -45,11 +48,13 @@ private function createCrudControllerRegistryService(ContainerBuilder $container
4548
{
4649
$crudControllersFqcn = array_keys($container->findTaggedServiceIds(EasyAdminExtension::TAG_CRUD_CONTROLLER, true));
4750

48-
$crudFqcnToEntityFqcnMap = $crudFqcnToCrudIdMap = [];
51+
$secret = $container->getParameter('kernel.secret');
52+
\assert(\is_string($secret));
4953

54+
$crudFqcnToEntityFqcnMap = $crudFqcnToCrudIdMap = [];
5055
foreach ($crudControllersFqcn as $controllerFqcn) {
5156
$crudFqcnToEntityFqcnMap[$controllerFqcn] = $controllerFqcn::getEntityFqcn();
52-
$crudFqcnToCrudIdMap[$controllerFqcn] = substr(sha1($container->getParameter('kernel.secret').$controllerFqcn), 0, 7);
57+
$crudFqcnToCrudIdMap[$controllerFqcn] = substr(sha1($secret.$controllerFqcn), 0, 7);
5358
}
5459

5560
$container

src/DependencyInjection/EasyAdminExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ public function prepend(ContainerBuilder $builder): void
5151
],
5252
]);
5353

54-
$bundleTemplatesOverrideDir = $builder->getParameter('kernel.project_dir').'/templates/bundles/EasyAdminBundle/';
54+
$projectDir = $builder->getParameter('kernel.project_dir');
55+
\assert(\is_string($projectDir));
56+
57+
$bundleTemplatesOverrideDir = $projectDir.'/templates/bundles/EasyAdminBundle/';
5558
$builder->prependExtensionConfig('twig', [
5659
'paths' => is_dir($bundleTemplatesOverrideDir)
5760
? [

src/Dto/BatchActionDto.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44

55
/**
66
* @author Javier Eguiluz <[email protected]>
7+
*
8+
* @template TEntity of object = object
79
*/
810
class BatchActionDto
911
{
1012
private string $name;
1113
/** @var array<mixed> */
1214
private array $entityIds;
15+
/** @var class-string<TEntity> */
1316
private string $entityFqcn;
1417
private string $referrerUrl;
1518
private string $csrfToken;
1619

1720
/**
18-
* @param array<mixed> $entityIds
21+
* @param array<mixed> $entityIds
22+
* @param class-string<TEntity> $entityFqcn
1923
*/
2024
public function __construct(string $name, array $entityIds, string $entityFqcn, string $referrerUrl, string $csrfToken /* , bool $triggerDeprecation = true */)
2125
{
@@ -58,6 +62,9 @@ public function getEntityIds(): array
5862
return $this->entityIds;
5963
}
6064

65+
/**
66+
* @return class-string<TEntity>
67+
*/
6168
public function getEntityFqcn(): string
6269
{
6370
return $this->entityFqcn;

src/Dto/CrudDto.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
66
use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
77
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\SearchMode;
8+
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\CrudControllerInterface;
89
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
910
use EasyCorp\Bundle\EasyAdminBundle\Translation\TranslatableMessageBuilder;
1011
use Symfony\Component\ExpressionLanguage\Expression;
@@ -16,6 +17,7 @@
1617
*/
1718
final class CrudDto
1819
{
20+
/** @var class-string<CrudControllerInterface>|null */
1921
private ?string $controllerFqcn = null;
2022
private AssetsDto $fieldAssetsDto;
2123
private ?string $pageName = null;
@@ -85,11 +87,17 @@ public function __construct()
8587
$this->overriddenTemplates = [];
8688
}
8789

90+
/**
91+
* @return class-string<CrudControllerInterface>|null
92+
*/
8893
public function getControllerFqcn(): ?string
8994
{
9095
return $this->controllerFqcn;
9196
}
9297

98+
/**
99+
* @param class-string<CrudControllerInterface> $fqcn
100+
*/
93101
public function setControllerFqcn(string $fqcn): void
94102
{
95103
$this->controllerFqcn = $fqcn;

0 commit comments

Comments
 (0)