Skip to content

Commit 4fc07cd

Browse files
vincentchalamonalanpoulain
authored andcommitted
chore: apply rector to migrate code to PHP 8.1
1 parent cc42333 commit 4fc07cd

File tree

288 files changed

+1069
-2980
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

288 files changed

+1069
-2980
lines changed

rector.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
15+
use Rector\Config\RectorConfig;
16+
use Rector\PHPUnit\Set\PHPUnitSetList;
17+
use Rector\Set\ValueObject\LevelSetList;
18+
use Rector\Set\ValueObject\SetList;
19+
20+
return static function (RectorConfig $rectorConfig): void {
21+
$rectorConfig->paths([
22+
__DIR__.'/src',
23+
__DIR__.'/tests',
24+
]);
25+
$rectorConfig->skip([
26+
// "Class PHPUnit\Framework\* not found."
27+
__DIR__.'/src/Symfony/Bundle/Test',
28+
__DIR__.'/tests/Fixtures/app/var',
29+
__DIR__.'/tests/ProphecyTrait.php',
30+
]);
31+
$rectorConfig->phpstanConfig(__DIR__.'/phpstan.neon.dist');
32+
$rectorConfig->importNames();
33+
$rectorConfig->disableImportShortClasses();
34+
35+
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
36+
37+
$rectorConfig->sets([
38+
LevelSetList::UP_TO_PHP_81,
39+
SetList::TYPE_DECLARATION_STRICT,
40+
]);
41+
42+
// PHPUnit
43+
// $rectorConfig->sets([
44+
// PHPUnitSetList::PHPUNIT_91,
45+
// PHPUnitSetList::PHPUNIT_CODE_QUALITY,
46+
// PHPUnitSetList::PHPUNIT_EXCEPTION,
47+
// PHPUnitSetList::REMOVE_MOCKS,
48+
// PHPUnitSetList::PHPUNIT_SPECIFIC_METHOD,
49+
// PHPUnitSetList::PHPUNIT_YIELD_DATA_PROVIDER,
50+
// ]);
51+
};

src/Action/EntrypointAction.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@
2323
*/
2424
final class EntrypointAction
2525
{
26-
private $resourceNameCollectionFactory;
27-
28-
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory)
26+
public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory)
2927
{
30-
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
3128
}
3229

3330
public function __invoke(): Entrypoint

src/Action/ExceptionAction.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,12 @@ final class ExceptionAction
3434
{
3535
use OperationRequestInitiatorTrait;
3636

37-
private $serializer;
38-
private $errorFormats;
39-
private $exceptionToStatus;
40-
4137
/**
4238
* @param array $errorFormats A list of enabled error formats
4339
* @param array $exceptionToStatus A list of exceptions mapped to their HTTP status code
4440
*/
45-
public function __construct(SerializerInterface $serializer, array $errorFormats, array $exceptionToStatus = [], ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null)
41+
public function __construct(private readonly SerializerInterface $serializer, private readonly array $errorFormats, private readonly array $exceptionToStatus = [], ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null)
4642
{
47-
$this->serializer = $serializer;
48-
$this->errorFormats = $errorFormats;
49-
$this->exceptionToStatus = $exceptionToStatus;
5043
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
5144
}
5245

src/Api/Entrypoint.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@
2222
*/
2323
final class Entrypoint
2424
{
25-
private $resourceNameCollection;
26-
27-
public function __construct(ResourceNameCollection $resourceNameCollection)
25+
public function __construct(private readonly ResourceNameCollection $resourceNameCollection)
2826
{
29-
$this->resourceNameCollection = $resourceNameCollection;
3027
}
3128

3229
public function getResourceNameCollection(): ResourceNameCollection

src/Api/FilterLocatorTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
trait FilterLocatorTrait
2727
{
28-
private ?ContainerInterface $filterLocator;
28+
private ?ContainerInterface $filterLocator = null;
2929

3030
/**
3131
* Sets a filter locator with a backward compatibility.

src/Api/FormatMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class FormatMatcher
2323
/**
2424
* @var array<string, string[]>
2525
*/
26-
private $formats;
26+
private readonly array $formats;
2727

2828
/**
2929
* @param array<string, string[]|string> $formats

src/Api/IdentifiersExtractor.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,12 @@
3333
final class IdentifiersExtractor implements IdentifiersExtractorInterface
3434
{
3535
use ResourceClassInfoTrait;
36+
private readonly PropertyAccessorInterface|PropertyAccessor $propertyAccessor;
3637

37-
private $propertyNameCollectionFactory;
38-
private $propertyMetadataFactory;
39-
private $propertyAccessor;
40-
41-
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, PropertyAccessorInterface $propertyAccessor = null)
38+
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, PropertyAccessorInterface $propertyAccessor = null)
4239
{
4340
$this->resourceMetadataFactory = $resourceMetadataFactory;
4441
$this->resourceClassResolver = $resourceClassResolver;
45-
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
46-
$this->propertyMetadataFactory = $propertyMetadataFactory;
4742
$this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
4843
}
4944

@@ -61,7 +56,7 @@ public function getIdentifiersFromItem($item, Operation $operation = null, array
6156
}
6257

6358
$resourceClass = $this->getResourceClass($item, true);
64-
$operation = $operation ?? $this->resourceMetadataFactory->create($resourceClass)->getOperation(null, false, true);
59+
$operation ??= $this->resourceMetadataFactory->create($resourceClass)->getOperation(null, false, true);
6560

6661
if ($operation instanceof HttpOperation) {
6762
$links = $operation->getUriVariables();
@@ -70,7 +65,7 @@ public function getIdentifiersFromItem($item, Operation $operation = null, array
7065
}
7166

7267
foreach ($links ?? [] as $link) {
73-
if (1 < \count($link->getIdentifiers())) {
68+
if (1 < (is_countable($link->getIdentifiers()) ? \count($link->getIdentifiers()) : 0)) {
7469
$compositeIdentifiers = [];
7570
foreach ($link->getIdentifiers() as $identifier) {
7671
$compositeIdentifiers[$identifier] = $this->getIdentifierValue($item, $link->getFromClass() ?? $resourceClass, $identifier, $link->getParameterName());

src/Api/QueryParameterValidator/QueryParameterValidator.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
namespace ApiPlatform\Api\QueryParameterValidator;
1515

1616
use ApiPlatform\Api\FilterLocatorTrait;
17+
use ApiPlatform\Api\QueryParameterValidator\Validator\ArrayItems;
18+
use ApiPlatform\Api\QueryParameterValidator\Validator\Bounds;
19+
use ApiPlatform\Api\QueryParameterValidator\Validator\Enum;
20+
use ApiPlatform\Api\QueryParameterValidator\Validator\Length;
21+
use ApiPlatform\Api\QueryParameterValidator\Validator\MultipleOf;
22+
use ApiPlatform\Api\QueryParameterValidator\Validator\Pattern;
23+
use ApiPlatform\Api\QueryParameterValidator\Validator\Required;
1724
use ApiPlatform\Exception\FilterValidationException;
1825
use Psr\Container\ContainerInterface;
1926

@@ -33,13 +40,13 @@ public function __construct(ContainerInterface $filterLocator)
3340
$this->setFilterLocator($filterLocator);
3441

3542
$this->validators = [
36-
new Validator\ArrayItems(),
37-
new Validator\Bounds(),
38-
new Validator\Enum(),
39-
new Validator\Length(),
40-
new Validator\MultipleOf(),
41-
new Validator\Pattern(),
42-
new Validator\Required(),
43+
new ArrayItems(),
44+
new Bounds(),
45+
new Enum(),
46+
new Length(),
47+
new MultipleOf(),
48+
new Pattern(),
49+
new Required(),
4350
];
4451
}
4552

src/Api/QueryParameterValidator/Validator/ArrayItems.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,20 @@ private function getValue(string $name, array $filterDescription, array $queryPa
6262

6363
$collectionFormat = $filterDescription['swagger']['collectionFormat'] ?? 'csv';
6464

65-
return explode(self::getSeparator($collectionFormat), $value) ?: []; // @phpstan-ignore-line
65+
return explode(self::getSeparator($collectionFormat), (string) $value) ?: []; // @phpstan-ignore-line
6666
}
6767

6868
/**
6969
* @return non-empty-string
7070
*/
7171
private static function getSeparator(string $collectionFormat): string
7272
{
73-
switch ($collectionFormat) {
74-
case 'csv':
75-
return ',';
76-
case 'ssv':
77-
return ' ';
78-
case 'tsv':
79-
return '\t';
80-
case 'pipes':
81-
return '|';
82-
default:
83-
throw new \InvalidArgumentException(sprintf('Unknown collection format %s', $collectionFormat));
84-
}
73+
return match ($collectionFormat) {
74+
'csv' => ',',
75+
'ssv' => ' ',
76+
'tsv' => '\t',
77+
'pipes' => '|',
78+
default => throw new \InvalidArgumentException(sprintf('Unknown collection format %s', $collectionFormat)),
79+
};
8580
}
8681
}

src/Api/ResourceClassResolver.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@
2626
final class ResourceClassResolver implements ResourceClassResolverInterface
2727
{
2828
use ClassInfoTrait;
29+
private array $localIsResourceClassCache = [];
30+
private array $localMostSpecificResourceClassCache = [];
2931

30-
private $resourceNameCollectionFactory;
31-
private $localIsResourceClassCache = [];
32-
private $localMostSpecificResourceClassCache = [];
33-
34-
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory)
32+
public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory)
3533
{
36-
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
3734
}
3835

3936
/**

0 commit comments

Comments
 (0)