Skip to content

Commit 7a556c1

Browse files
vincentchalamonalanpoulain
authored andcommitted
chore: apply rector to migrate code to PHP 8.1
1 parent 8d98018 commit 7a556c1

15 files changed

+32
-100
lines changed

Extension/EagerLoadingExtension.php

+5-20
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,8 @@
4141
*/
4242
final class EagerLoadingExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface
4343
{
44-
private PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory;
45-
private PropertyMetadataFactoryInterface $propertyMetadataFactory;
46-
private ?ClassMetadataFactoryInterface $classMetadataFactory;
47-
private int $maxJoins;
48-
private bool $forceEager;
49-
private bool $fetchPartial;
50-
51-
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, int $maxJoins = 30, bool $forceEager = true, bool $fetchPartial = false, ClassMetadataFactoryInterface $classMetadataFactory = null)
44+
public function __construct(private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly int $maxJoins = 30, private readonly bool $forceEager = true, private readonly bool $fetchPartial = false, private readonly ?ClassMetadataFactoryInterface $classMetadataFactory = null)
5245
{
53-
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
54-
$this->propertyMetadataFactory = $propertyMetadataFactory;
55-
$this->classMetadataFactory = $classMetadataFactory;
56-
$this->maxJoins = $maxJoins;
57-
$this->forceEager = $forceEager;
58-
$this->fetchPartial = $fetchPartial;
5946
}
6047

6148
/**
@@ -141,11 +128,11 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt
141128

142129
try {
143130
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $association, $options);
144-
} catch (PropertyNotFoundException $propertyNotFoundException) {
131+
} catch (PropertyNotFoundException) {
145132
// skip properties not found
146133
continue;
147134
// @phpstan-ignore-next-line indeed this can be thrown by the SerializerPropertyMetadataFactory
148-
} catch (ResourceClassNotFoundException $resourceClassNotFoundException) {
135+
} catch (ResourceClassNotFoundException) {
149136
// skip associations that are not resource classes
150137
continue;
151138
}
@@ -207,7 +194,7 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt
207194
if (true === $fetchPartial) {
208195
try {
209196
$this->addSelect($queryBuilder, $mapping['targetEntity'], $associationAlias, $options);
210-
} catch (ResourceClassNotFoundException $resourceClassNotFoundException) {
197+
} catch (ResourceClassNotFoundException) {
211198
continue;
212199
}
213200
} else {
@@ -283,9 +270,7 @@ private function addSelect(QueryBuilder $queryBuilder, string $entity, string $a
283270

284271
private function addSelectOnce(QueryBuilder $queryBuilder, string $alias): void
285272
{
286-
$existingSelects = array_reduce($queryBuilder->getDQLPart('select') ?? [], function ($existing, $dqlSelect) {
287-
return ($dqlSelect instanceof Select) ? array_merge($existing, $dqlSelect->getParts()) : $existing;
288-
}, []);
273+
$existingSelects = array_reduce($queryBuilder->getDQLPart('select') ?? [], fn ($existing, $dqlSelect) => ($dqlSelect instanceof Select) ? array_merge($existing, $dqlSelect->getParts()) : $existing, []);
289274

290275
if (!\in_array($alias, $existingSelects, true)) {
291276
$queryBuilder->addSelect($alias);

Extension/FilterEagerLoadingExtension.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,8 @@
2929
*/
3030
final class FilterEagerLoadingExtension implements QueryCollectionExtensionInterface
3131
{
32-
private ?ResourceClassResolverInterface $resourceClassResolver;
33-
private bool $forceEager;
34-
35-
public function __construct(bool $forceEager = true, ResourceClassResolverInterface $resourceClassResolver = null)
32+
public function __construct(private readonly bool $forceEager = true, private readonly ?ResourceClassResolverInterface $resourceClassResolver = null)
3633
{
37-
$this->forceEager = $forceEager;
38-
$this->resourceClassResolver = $resourceClassResolver;
3934
}
4035

4136
/**
@@ -200,8 +195,6 @@ private function getQueryBuilderWithNewAliases(QueryBuilder $queryBuilder, Query
200195

201196
private function buildReplacePatterns(array $aliases): array
202197
{
203-
return array_map(static function (string $alias): string {
204-
return '/\b'.preg_quote($alias, '/').'/';
205-
}, $aliases);
198+
return array_map(static fn (string $alias): string => '/\b'.preg_quote($alias, '/').'/', $aliases);
206199
}
207200
}

Extension/FilterExtension.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@
2929
*/
3030
final class FilterExtension implements QueryCollectionExtensionInterface
3131
{
32-
private ContainerInterface $filterLocator;
33-
34-
public function __construct(ContainerInterface $filterLocator)
32+
public function __construct(private readonly ContainerInterface $filterLocator)
3533
{
36-
$this->filterLocator = $filterLocator;
3734
}
3835

3936
/**
@@ -62,13 +59,13 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
6259
continue;
6360
}
6461

65-
$context['filters'] = $context['filters'] ?? [];
62+
$context['filters'] ??= [];
6663
$filter->apply($queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context);
6764
}
6865
}
6966

7067
foreach ($orderFilters as $orderFilter) {
71-
$context['filters'] = $context['filters'] ?? [];
68+
$context['filters'] ??= [];
7269
$orderFilter->apply($queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context);
7370
}
7471
}

Extension/OrderExtension.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@
2828
*/
2929
final class OrderExtension implements QueryCollectionExtensionInterface
3030
{
31-
private ?string $order;
32-
33-
public function __construct(string $order = null)
31+
public function __construct(private readonly ?string $order = null)
3432
{
35-
$this->order = $order;
3633
}
3734

3835
/**

Extension/PaginationExtension.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,8 @@ class_exists(AbstractPaginator::class);
3636
*/
3737
final class PaginationExtension implements QueryResultCollectionExtensionInterface
3838
{
39-
private ManagerRegistry $managerRegistry;
40-
private ?Pagination $pagination;
41-
42-
public function __construct(ManagerRegistry $managerRegistry, Pagination $pagination)
39+
public function __construct(private readonly ManagerRegistry $managerRegistry, private readonly ?Pagination $pagination)
4340
{
44-
$this->managerRegistry = $managerRegistry;
45-
$this->pagination = $pagination;
4641
}
4742

4843
/**

Filter/AbstractFilter.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,17 @@ abstract class AbstractFilter implements FilterInterface
2727
{
2828
use OrmPropertyHelperTrait;
2929
use PropertyHelperTrait;
30-
31-
protected ManagerRegistry $managerRegistry;
3230
protected LoggerInterface $logger;
33-
protected ?array $properties;
34-
protected ?NameConverterInterface $nameConverter;
3531

36-
public function __construct(ManagerRegistry $managerRegistry, LoggerInterface $logger = null, array $properties = null, NameConverterInterface $nameConverter = null)
32+
public function __construct(protected ManagerRegistry $managerRegistry, LoggerInterface $logger = null, protected ?array $properties = null, protected ?NameConverterInterface $nameConverter = null)
3733
{
38-
$this->managerRegistry = $managerRegistry;
3934
$this->logger = $logger ?? new NullLogger();
40-
$this->properties = $properties;
41-
$this->nameConverter = $nameConverter;
4235
}
4336

4437
/**
4538
* {@inheritdoc}
4639
*/
47-
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, Operation $operation = null, array $context = [])
40+
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, Operation $operation = null, array $context = []): void
4841
{
4942
foreach ($context['filters'] as $property => $value) {
5043
$this->filterProperty($this->denormalizePropertyName($property), $value, $queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context);

Filter/DateFilter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ protected function filterProperty(string $property, $values, QueryBuilder $query
129129
/**
130130
* Adds the where clause according to the chosen null management.
131131
*/
132-
protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, string $operator, mixed $value, string $nullManagement = null, DBALType|string $type = null)
132+
protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, string $operator, mixed $value, string $nullManagement = null, DBALType|string $type = null): void
133133
{
134134
$type = (string) $type;
135135
$value = $this->normalizeValue($value, $operator);
@@ -140,7 +140,7 @@ protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterf
140140

141141
try {
142142
$value = !str_contains($type, '_immutable') ? new \DateTime($value) : new \DateTimeImmutable($value);
143-
} catch (\Exception $e) {
143+
} catch (\Exception) {
144144
// Silently ignore this filter if it can not be transformed to a \DateTime
145145
$this->logger->notice('Invalid filter ignored', [
146146
'exception' => new InvalidArgumentException(sprintf('The field "%s" has a wrong date format. Use one accepted by the \DateTime constructor', $field)),

Filter/OrderFilter.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ final class OrderFilter extends AbstractFilter implements OrderFilterInterface
4040
{
4141
use OrderFilterTrait;
4242

43-
private ?string $orderNullsComparison;
44-
45-
public function __construct(ManagerRegistry $managerRegistry, string $orderParameterName = 'order', LoggerInterface $logger = null, array $properties = null, NameConverterInterface $nameConverter = null, ?string $orderNullsComparison = null)
43+
public function __construct(ManagerRegistry $managerRegistry, string $orderParameterName = 'order', LoggerInterface $logger = null, array $properties = null, NameConverterInterface $nameConverter = null, private readonly ?string $orderNullsComparison = null)
4644
{
4745
if (null !== $properties) {
4846
$properties = array_map(static function ($propertyOptions) {
@@ -60,7 +58,6 @@ public function __construct(ManagerRegistry $managerRegistry, string $orderParam
6058
parent::__construct($managerRegistry, $logger, $properties, $nameConverter);
6159

6260
$this->orderParameterName = $orderParameterName;
63-
$this->orderNullsComparison = $orderNullsComparison;
6461
}
6562

6663
/**

Metadata/Property/DoctrineOrmPropertyMetadataFactory.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,8 @@
2525
*/
2626
final class DoctrineOrmPropertyMetadataFactory implements PropertyMetadataFactoryInterface
2727
{
28-
private PropertyMetadataFactoryInterface $decorated;
29-
private ManagerRegistry $managerRegistry;
30-
31-
public function __construct(ManagerRegistry $managerRegistry, PropertyMetadataFactoryInterface $decorated)
28+
public function __construct(private readonly ManagerRegistry $managerRegistry, private readonly PropertyMetadataFactoryInterface $decorated)
3229
{
33-
$this->managerRegistry = $managerRegistry;
34-
$this->decorated = $decorated;
3530
}
3631

3732
/**

Metadata/Resource/DoctrineOrmResourceCollectionMetadataFactory.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,8 @@
2727

2828
final class DoctrineOrmResourceCollectionMetadataFactory implements ResourceMetadataCollectionFactoryInterface
2929
{
30-
private ManagerRegistry $managerRegistry;
31-
32-
private ResourceMetadataCollectionFactoryInterface $decorated;
33-
34-
public function __construct(ManagerRegistry $managerRegistry, ResourceMetadataCollectionFactoryInterface $decorated)
30+
public function __construct(private readonly ManagerRegistry $managerRegistry, private readonly ResourceMetadataCollectionFactoryInterface $decorated)
3531
{
36-
$this->decorated = $decorated;
37-
$this->managerRegistry = $managerRegistry;
3832
}
3933

4034
/**

Paginator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
final class Paginator extends AbstractPaginator implements PaginatorInterface, QueryAwareInterface
2525
{
26-
private ?int $totalItems;
26+
private ?int $totalItems = null;
2727

2828
/**
2929
* {@inheritdoc}

State/CollectionProvider.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,11 @@ final class CollectionProvider implements ProviderInterface
3434
{
3535
use LinksHandlerTrait;
3636

37-
private ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory;
38-
private ManagerRegistry $managerRegistry;
39-
private iterable $collectionExtensions;
40-
4137
/**
4238
* @param QueryCollectionExtensionInterface[] $collectionExtensions
4339
*/
44-
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, ManagerRegistry $managerRegistry, iterable $collectionExtensions = [])
40+
public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ManagerRegistry $managerRegistry, private readonly iterable $collectionExtensions = [])
4541
{
46-
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
47-
$this->managerRegistry = $managerRegistry;
48-
$this->collectionExtensions = $collectionExtensions;
4942
}
5043

5144
public function provide(Operation $operation, array $uriVariables = [], array $context = [])

State/ItemProvider.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,11 @@ final class ItemProvider implements ProviderInterface
3434
{
3535
use LinksHandlerTrait;
3636

37-
private ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory;
38-
private ManagerRegistry $managerRegistry;
39-
private iterable $itemExtensions;
40-
4137
/**
4238
* @param QueryItemExtensionInterface[] $itemExtensions
4339
*/
44-
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, ManagerRegistry $managerRegistry, iterable $itemExtensions = [])
40+
public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ManagerRegistry $managerRegistry, private readonly iterable $itemExtensions = [])
4541
{
46-
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
47-
$this->managerRegistry = $managerRegistry;
48-
$this->itemExtensions = $itemExtensions;
4942
}
5043

5144
public function provide(Operation $operation, array $uriVariables = [], array $context = [])

Util/QueryBuilderHelper.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static function traverseJoins(string $alias, QueryBuilder $queryBuilder,
113113

114114
$joinAliasMap = self::mapJoinAliases($joinParts[$rootAlias]);
115115

116-
$aliasMap = array_merge($rootAliasMap, $joinAliasMap);
116+
$aliasMap = [...$rootAliasMap, ...$joinAliasMap];
117117

118118
$apexEntityClass = null;
119119
$associationStack = [];
@@ -208,8 +208,8 @@ private static function mapJoinAliases(iterable $joins): array
208208
$alias = $join->getAlias();
209209
$relationship = $join->getJoin();
210210

211-
if (str_contains($relationship, '.')) {
212-
$aliasMap[$alias] = explode('.', $relationship);
211+
if (str_contains((string) $relationship, '.')) {
212+
$aliasMap[$alias] = explode('.', (string) $relationship);
213213
} else {
214214
$aliasMap[$alias] = $relationship;
215215
}

Util/QueryChecker.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public static function hasMaxResults(QueryBuilder $queryBuilder): bool
9292
public static function hasOrderByOnFetchJoinedToManyAssociation(QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry): bool
9393
{
9494
if (
95-
0 === \count($selectParts = $queryBuilder->getDQLPart('select')) ||
96-
0 === \count($queryBuilder->getDQLPart('join')) ||
97-
0 === \count($orderByParts = $queryBuilder->getDQLPart('orderBy'))
95+
0 === (is_countable($selectParts = $queryBuilder->getDQLPart('select')) ? \count($selectParts = $queryBuilder->getDQLPart('select')) : 0) ||
96+
0 === (is_countable($queryBuilder->getDQLPart('join')) ? \count($queryBuilder->getDQLPart('join')) : 0) ||
97+
0 === (is_countable($orderByParts = $queryBuilder->getDQLPart('orderBy')) ? \count($orderByParts = $queryBuilder->getDQLPart('orderBy')) : 0)
9898
) {
9999
return false;
100100
}
@@ -105,7 +105,7 @@ public static function hasOrderByOnFetchJoinedToManyAssociation(QueryBuilder $qu
105105

106106
foreach ($selectParts as $select) {
107107
foreach ($select->getParts() as $part) {
108-
[$alias] = explode('.', $part);
108+
[$alias] = explode('.', (string) $part);
109109

110110
$selectAliases[] = $alias;
111111
}
@@ -120,8 +120,8 @@ public static function hasOrderByOnFetchJoinedToManyAssociation(QueryBuilder $qu
120120

121121
foreach ($orderByParts as $orderBy) {
122122
foreach ($orderBy->getParts() as $part) {
123-
if (str_contains($part, '.')) {
124-
[$alias] = explode('.', $part);
123+
if (str_contains((string) $part, '.')) {
124+
[$alias] = explode('.', (string) $part);
125125

126126
$orderByAliases[] = $alias;
127127
}
@@ -172,7 +172,7 @@ public static function hasLeftJoin(QueryBuilder $queryBuilder): bool
172172
public static function hasJoinedToManyAssociation(QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry): bool
173173
{
174174
if (
175-
0 === \count($queryBuilder->getDQLPart('join'))
175+
0 === (is_countable($queryBuilder->getDQLPart('join')) ? \count($queryBuilder->getDQLPart('join')) : 0)
176176
) {
177177
return false;
178178
}

0 commit comments

Comments
 (0)