Skip to content

Commit 2fed4cc

Browse files
committed
wip
1 parent 2857052 commit 2fed4cc

File tree

4 files changed

+61
-25
lines changed

4 files changed

+61
-25
lines changed

Controller/Crud.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Doctrine\ORM\EntityRepository;
1919
use Doctrine\ORM\Mapping\ClassMetadata;
2020
use Doctrine\ORM\QueryBuilder;
21+
use Exception;
2122
use JetBrains\PhpStorm\ExpectedValues;
2223
use Knp\Component\Pager\PaginatorInterface;
2324
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -38,15 +39,18 @@
3839
use Symfony\Component\String\Slugger\SluggerInterface;
3940
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
4041
use Symfony\Contracts\Translation\TranslatorInterface;
42+
use function count;
4143

4244
/**
4345
* Creates a Crud for a given entity managed by Doctrine.
44-
* @template T
46+
* @template T of object
4547
*/
4648
abstract class Crud extends AbstractController
4749
{
4850
private const ITEMS_PER_PAGE = 15;
4951
protected EntityManagerInterface $em;
52+
53+
/** @var ClassMetadata<T> */
5054
protected ClassMetadata $metadata;
5155
protected FieldService $fieldService;
5256
protected ?Request $request = null;
@@ -60,10 +64,8 @@ abstract class Crud extends AbstractController
6064
/** @var EntityRepository<T> */
6165
protected EntityRepository $repository;
6266

63-
/** @var Fields|Field[] */
6467
protected Fields $fields;
6568

66-
/** @var Filters|Filter[] */
6769
protected Filters $filters;
6870

6971
/** If the Crud is active and fully loaded */
@@ -177,6 +179,7 @@ public function getGlobalActions(): Actions
177179
}
178180

179181
/**
182+
* @param T $entity
180183
* Actions that can be applied to a single existing entity, such as "Edit" or "Delete"
181184
*/
182185
public function getActions($entity): Actions
@@ -211,6 +214,9 @@ public function getActions($entity): Actions
211214

212215
/**
213216
* All the actions available for a list of entities
217+
*
218+
* @param iterable<T> $entities
219+
* @return Actions[]
214220
*/
215221
public function getActionsPerEntities(iterable $entities): array
216222
{
@@ -224,6 +230,8 @@ public function getActionsPerEntities(iterable $entities): array
224230

225231
/**
226232
* The batch actions available for a lit of entities
233+
*
234+
* @param iterable<T> $entities
227235
*/
228236
public function getBatchActions(iterable $entities): Actions
229237
{
@@ -254,6 +262,7 @@ public function getListEntityActionsDisplayMode(): EntityActionsDisplayMode
254262

255263
/**
256264
* Removes an entity
265+
* @param T $entity
257266
*/
258267
public function deleteAction($entity): Response
259268
{
@@ -284,7 +293,7 @@ public function deleteBatchAction(): Response
284293
return $this->redirectToList();
285294
}
286295
$checked = $this->request->request->all('batch-actions');
287-
$nbChecked = \count($checked);
296+
$nbChecked = count($checked);
288297
foreach ($checked as $k => $v) {
289298
/** @var T $entity */
290299
$entity = $this->repository->find($k);
@@ -329,7 +338,7 @@ public function toggleBooleanPostAction(Request $request, $entity): Response
329338
$propertyAccessor = PropertyAccess::createPropertyAccessor();
330339
try {
331340
$propertyAccessor->setValue($entity, $index, $value);
332-
} catch (\Exception) {
341+
} catch (Exception) {
333342
throw $this->createAccessDeniedException("Entity {$this->getEntity()}'s property $index cannot be read or written");
334343
}
335344

@@ -600,7 +609,7 @@ public function exportAction(Request $request): Response
600609
foreach ($fields as $field) {
601610
try {
602611
$value = $propertyAccessor->getValue($entity, $field->getIndex());
603-
} catch (\Exception) {
612+
} catch (Exception) {
604613
$value = '';
605614
}
606615

@@ -703,6 +712,8 @@ public function getForm($entity, bool $creation): FormInterface
703712

704713
/**
705714
* Returns all the entity's attributes that will be turned into Fields.
715+
*
716+
* @return string[]
706717
*/
707718
protected function getAllEntityFields(): array
708719
{
@@ -725,9 +736,9 @@ protected function getAllEntityFields(): array
725736
}
726737
}
727738
}
728-
$methods = $this->metadata->getReflectionClass()?->getMethods(\ReflectionMethod::IS_PUBLIC);
739+
$methods = $this->metadata->getReflectionClass()->getMethods(\ReflectionMethod::IS_PUBLIC);
729740
foreach ($methods as $method) {
730-
if (\count($method->getAttributes(\Arkounay\Bundle\QuickAdminGeneratorBundle\Attribute\Field::class)) === 1) {
741+
if (count($method->getAttributes(\Arkounay\Bundle\QuickAdminGeneratorBundle\Attribute\Field::class)) === 1) {
731742
$res[] = $method->getName();
732743
}
733744
}
@@ -1065,7 +1076,7 @@ protected function getListRouteParams(): array
10651076
protected function hasActions(array $actionEntities): bool
10661077
{
10671078
foreach ($actionEntities as $actions) {
1068-
if (\count($actions) > 0) {
1079+
if (count($actions) > 0) {
10691080
return true;
10701081
}
10711082
}

Model/Fields.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
use Doctrine\ORM\Mapping\ClassMetadata;
88

99
/**
10+
* @template TEntity of object
1011
* @template-extends TypedArray<int, Field>
1112
* @method Field get(string $field)
1213
*/
1314
class Fields extends TypedArray
1415
{
1516

17+
/**
18+
* @param ClassMetadata<TEntity> $metadata
19+
*/
1620
public function __construct(private readonly ClassMetadata $metadata, private readonly FieldService $fieldService){}
1721

1822
public function createFromIndexName(string $index): Listable
@@ -25,15 +29,15 @@ protected function getType(): string
2529
return Field::class;
2630
}
2731

28-
public function sortByPosition(): self
32+
public function sortByPosition(): static
2933
{
3034
uasort($this->items, static fn(Field $a, Field $b): int => $a->getPosition() <=> $b->getPosition());
3135

3236
return $this;
3337
}
3438

3539

36-
public function moveToLastPosition(string $index): self
40+
public function moveToLastPosition(string $index): static
3741
{
3842
$maxPosition = 0;
3943
foreach ($this as $field) {
@@ -45,7 +49,7 @@ public function moveToLastPosition(string $index): self
4549
return $this;
4650
}
4751

48-
public function moveToFirstPosition(string $index): self
52+
public function moveToFirstPosition(string $index): static
4953
{
5054
$minPosition = 0;
5155
foreach ($this as $field) {

Model/Filters.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
use Doctrine\ORM\Mapping\ClassMetadata;
88

99
/**
10+
* @template TEntity of object
1011
* @template-extends TypedArray<int, Filter>
1112
* @method Filter get(string $field)
1213
*/
1314
class Filters extends TypedArray
1415
{
1516

17+
/**
18+
* @param ClassMetadata<TEntity> $metadata
19+
*/
1620
public function __construct(private readonly ClassMetadata $metadata, private readonly FieldService $fieldService) {}
1721

1822
public function createFromIndexName(string $index): Listable

Model/TypedArray.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
/**
77
* @internal
88
* @template TKey of array-key
9-
* @template T
9+
* @template T of Listable
1010
* @template-implements \IteratorAggregate<TKey, T>
1111
* @template-implements \ArrayAccess<TKey|null, T>
1212
*/
1313
abstract class TypedArray implements \IteratorAggregate, \ArrayAccess, \Countable
1414
{
1515

1616
/**
17-
* @var Listable[]
17+
* @var array<TKey, T>
1818
*/
1919
protected array $items = [];
2020

@@ -26,17 +26,17 @@ abstract public function createFromIndexName(string $index): Listable;
2626
abstract protected function getType(): string;
2727

2828
/**
29-
* @return T
29+
* @phpstan-return T
3030
*/
3131
public function get(string $field): Listable
3232
{
3333
return $this->items[$field];
3434
}
3535

3636
/**
37-
* @param T|string $field
37+
* @phpstan-param T|string $field
3838
*/
39-
public function add(Listable|string $field): self
39+
public function add(Listable|string $field): static
4040
{
4141
$type = $this->getType();
4242
if ($field instanceof $type) {
@@ -55,20 +55,23 @@ public function add(Listable|string $field): self
5555
return $this;
5656
}
5757

58-
public function remove(string $fieldIndex): self
58+
public function remove(string $fieldIndex): static
5959
{
6060
unset($this->items[$fieldIndex]);
6161

6262
return $this;
6363
}
6464

65+
/**
66+
* @return \ArrayIterator<TKey, T>
67+
*/
6568
public function getIterator(): \Traversable
6669
{
6770
return new \ArrayIterator($this->items);
6871
}
6972

7073
/**
71-
* @param Listable $value
74+
* @param T $value
7275
*/
7376
public function offsetSet(mixed $offset, $value): void
7477
{
@@ -81,17 +84,24 @@ public function offsetSet(mixed $offset, $value): void
8184
}
8285
}
8386

87+
/**
88+
* @param TKey $offset
89+
*/
8490
public function offsetExists($offset): bool
8591
{
8692
return isset($this->items[$offset]);
8793
}
8894

95+
/**
96+
* @param TKey $offset
97+
*/
8998
public function offsetUnset($offset): void
9099
{
91100
unset($this->items[$offset]);
92101
}
93102

94103
/**
104+
* @param TKey $offset
95105
* @return T
96106
*/
97107
public function offsetGet($offset): Listable
@@ -104,13 +114,16 @@ public function isEmpty(): bool
104114
return empty($this->items);
105115
}
106116

107-
public function clear(): self
117+
public function clear(): static
108118
{
109119
$this->items = [];
110120

111121
return $this;
112122
}
113123

124+
/**
125+
* @param iterable<TKey, T>|iterable<T> $fields
126+
*/
114127
public function set(iterable $fields): void
115128
{
116129
$this->clear();
@@ -124,7 +137,7 @@ public function count(): int
124137
return \count($this->items);
125138
}
126139

127-
public function moveToLastPosition(string $index): self
140+
public function moveToLastPosition(string $index): static
128141
{
129142
$tmp = $this->items[$index];
130143
unset($this->items[$index]);
@@ -133,7 +146,7 @@ public function moveToLastPosition(string $index): self
133146
return $this;
134147
}
135148

136-
public function moveToFirstPosition(string $index): self
149+
public function moveToFirstPosition(string $index): static
137150
{
138151
$this->items = array_merge([$index => $this->items[$index]], $this->items);
139152

@@ -142,12 +155,16 @@ public function moveToFirstPosition(string $index): self
142155

143156
public function contains(string $index): bool
144157
{
145-
return isset($this->items);
158+
return isset($this->items[$index]);
146159
}
147160

148-
public function filter(callable $callback): self
161+
/**
162+
* @param callable(T, TKey): bool $callback
163+
* @return static
164+
*/
165+
public function filter(callable $callback): static
149166
{
150-
$this->items = array_filter($this->items, $callback);
167+
$this->items = array_filter($this->items, $callback, ARRAY_FILTER_USE_BOTH);
151168

152169
return $this;
153170
}

0 commit comments

Comments
 (0)