From a4594d4448d04fca8c581dad7aade69aabac11f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 9 Nov 2021 18:02:55 +0100 Subject: [PATCH 1/2] ci: Upgrade to PHPStan 1.0 --- composer.json | 18 +++++++++--------- phpstan.neon.dist | 4 +++- src/Annotation/ApiFilter.php | 2 +- src/Annotation/ApiResource.php | 2 +- src/Annotation/ApiSubresource.php | 10 ++++++---- .../MongoDbOdm/CollectionDataProvider.php | 7 ++----- .../Extension/PaginationExtension.php | 7 +++---- tests/Fixtures/DummyObjectWithConstructor.php | 4 ++-- tests/Fixtures/app/console | 5 +++-- tests/Fixtures/app/object-manager.php | 7 +++++++ 10 files changed, 37 insertions(+), 29 deletions(-) create mode 100644 tests/Fixtures/app/object-manager.php diff --git a/composer.json b/composer.json index 79f5fd57541..edda774385d 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "doctrine/inflector": "^1.0 || ^2.0", "fig/link-util": "^1.0", "psr/cache": "^1.0 || ^2.0 || ^3.0", - "psr/container": "^1.0", + "psr/container": "^1.0 || ^2.0", "symfony/http-foundation": "^4.4 || ^5.1", "symfony/http-kernel": "^4.4 || ^5.1", "symfony/property-access": "^3.4.19 || ^4.4 || ^5.1", @@ -30,7 +30,7 @@ "behat/behat": "^3.1", "behat/mink": "^1.7", "doctrine/annotations": "^1.7", - "doctrine/cache": "^1.11", + "doctrine/cache": "^1.11 || ^2.1", "doctrine/common": "^2.11 || ^3.0", "doctrine/data-fixtures": "^1.2.2", "doctrine/doctrine-bundle": "^1.12 || ^2.0", @@ -42,16 +42,16 @@ "friends-of-behat/mink-extension": "^2.2", "friends-of-behat/symfony-extension": "^2.1", "guzzlehttp/guzzle": "^6.0 || ^7.0", - "jangregor/phpstan-prophecy": "^0.8", + "jangregor/phpstan-prophecy": "^1.0", "justinrainbow/json-schema": "^5.2.1", "phpdocumentor/reflection-docblock": "^3.0 || ^4.0 || ^5.1", "phpdocumentor/type-resolver": "^0.3 || ^0.4 || ^1.4", - "phpstan/extension-installer": "^1.0", - "phpstan/phpstan": "^0.12.65", - "phpstan/phpstan-doctrine": "^0.12.7", - "phpstan/phpstan-phpunit": "^0.12.4", - "phpstan/phpstan-symfony": "^0.12.4", - "psr/log": "^1.0", + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan": "^1.1", + "phpstan/phpstan-doctrine": "^1.0", + "phpstan/phpstan-phpunit": "^1.0", + "phpstan/phpstan-symfony": "^1.0", + "psr/log": "^1.0 || ^2.0 || ^3.0", "ramsey/uuid": "^3.7 || ^4.0", "ramsey/uuid-doctrine": "^1.4", "soyuka/contexts": "^3.3.1", diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 520746c369c..dbce909e90f 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -8,12 +8,14 @@ parameters: symfony: container_xml_path: tests/Fixtures/app/var/cache/test/AppKernelTestDebugContainer.xml constant_hassers: false + doctrine: + objectManagerLoader: tests/Fixtures/app/object-manager.php bootstrapFiles: - vendor/bin/.phpunit/phpunit/vendor/autoload.php # We're aliasing classes for phpunit in this file, it needs to be added here see phpstan/#2194 - src/Bridge/Symfony/Bundle/Test/Constraint/ArraySubset.php - tests/Fixtures/app/AppKernel.php - excludes_analyse: + excludePaths: # Symfony cache - tests/Fixtures/app/var/cache # Deprecated integrations (will be removed in API Platform 3) diff --git a/src/Annotation/ApiFilter.php b/src/Annotation/ApiFilter.php index 71809c6b4e7..9953cfc4fd0 100644 --- a/src/Annotation/ApiFilter.php +++ b/src/Annotation/ApiFilter.php @@ -66,7 +66,7 @@ public function __construct( ) { if (\is_array($filterClass)) { /** @phpstan-ignore-line Doctrine annotations */ $options = $filterClass; - $this->filterClass = $options['value'] ?? null; + $this->filterClass = $options['value'] ?? null; /** @phpstan-ignore-line Doctrine annotations */ unset($options['value']); foreach ($options as $key => $value) { diff --git a/src/Annotation/ApiResource.php b/src/Annotation/ApiResource.php index a591557e10b..e6adae76557 100644 --- a/src/Annotation/ApiResource.php +++ b/src/Annotation/ApiResource.php @@ -234,6 +234,6 @@ public function __construct( } } - $this->hydrateAttributes($description ?? []); + $this->hydrateAttributes($description ?? []); // @phpstan-ignore-line } } diff --git a/src/Annotation/ApiSubresource.php b/src/Annotation/ApiSubresource.php index 40eeb3d8bb4..27e321505f6 100644 --- a/src/Annotation/ApiSubresource.php +++ b/src/Annotation/ApiSubresource.php @@ -37,10 +37,12 @@ final class ApiSubresource */ public function __construct($maxDepth = null) { - if (!\is_array($maxDepth)) { // @phpstan-ignore-line - $this->maxDepth = $maxDepth; - } else { - $this->maxDepth = $maxDepth['maxDepth'] ?? null; + if (\is_array($maxDepth)) { // @phpstan-ignore-line + $this->maxDepth = $maxDepth['maxDepth'] ?? null; // @phpstan-ignore-line + + return; } + + $this->maxDepth = $maxDepth; } } diff --git a/src/Bridge/Doctrine/MongoDbOdm/CollectionDataProvider.php b/src/Bridge/Doctrine/MongoDbOdm/CollectionDataProvider.php index 2c0381e444f..15a6efa46b8 100644 --- a/src/Bridge/Doctrine/MongoDbOdm/CollectionDataProvider.php +++ b/src/Bridge/Doctrine/MongoDbOdm/CollectionDataProvider.php @@ -61,11 +61,8 @@ public function getCollection(string $resourceClass, string $operationName = nul /** @var DocumentManager $manager */ $manager = $this->managerRegistry->getManagerForClass($resourceClass); + /** @var DocumentRepository $repository */ $repository = $manager->getRepository($resourceClass); - if (!$repository instanceof DocumentRepository) { - throw new RuntimeException(sprintf('The repository for "%s" must be an instance of "%s".', $resourceClass, DocumentRepository::class)); - } - $aggregationBuilder = $repository->createAggregationBuilder(); foreach ($this->collectionExtensions as $extension) { $extension->applyToCollection($aggregationBuilder, $resourceClass, $operationName, $context); @@ -79,6 +76,6 @@ public function getCollection(string $resourceClass, string $operationName = nul $attribute = $resourceMetadata->getCollectionOperationAttribute($operationName, 'doctrine_mongodb', [], true); $executeOptions = $attribute['execute_options'] ?? []; - return $aggregationBuilder->hydrate($resourceClass)->execute($executeOptions); + return $aggregationBuilder->hydrate($resourceClass)->getAggregation($executeOptions)->getIterator(); } } diff --git a/src/Bridge/Doctrine/MongoDbOdm/Extension/PaginationExtension.php b/src/Bridge/Doctrine/MongoDbOdm/Extension/PaginationExtension.php index cb6dae92d66..ab7f08699a1 100644 --- a/src/Bridge/Doctrine/MongoDbOdm/Extension/PaginationExtension.php +++ b/src/Bridge/Doctrine/MongoDbOdm/Extension/PaginationExtension.php @@ -68,11 +68,10 @@ public function applyToCollection(Builder $aggregationBuilder, string $resourceC throw new RuntimeException(sprintf('The manager for "%s" must be an instance of "%s".', $resourceClass, DocumentManager::class)); } + /** + * @var DocumentRepository + */ $repository = $manager->getRepository($resourceClass); - if (!$repository instanceof DocumentRepository) { - throw new RuntimeException(sprintf('The repository for "%s" must be an instance of "%s".', $resourceClass, DocumentRepository::class)); - } - $resultsAggregationBuilder = $repository->createAggregationBuilder()->skip($offset); if ($limit > 0) { $resultsAggregationBuilder->limit($limit); diff --git a/tests/Fixtures/DummyObjectWithConstructor.php b/tests/Fixtures/DummyObjectWithConstructor.php index 6f8ca93f609..00fb015a90d 100644 --- a/tests/Fixtures/DummyObjectWithConstructor.php +++ b/tests/Fixtures/DummyObjectWithConstructor.php @@ -18,8 +18,8 @@ */ class DummyObjectWithConstructor { - private $foo; - private $bar; + private $foo; // @phpstan-ignore-line + private $bar; // @phpstan-ignore-line public function __construct(string $foo, \stdClass $bar) { diff --git a/tests/Fixtures/app/console b/tests/Fixtures/app/console index bdf6d1d6322..80695da3876 100755 --- a/tests/Fixtures/app/console +++ b/tests/Fixtures/app/console @@ -3,7 +3,8 @@ use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Input\ArgvInput; -use Symfony\Component\Debug\Debug; +use Symfony\Component\Debug\Debug as LegacyDebug; +use Symfony\Component\ErrorHandler\Debug; set_time_limit(0); @@ -15,7 +16,7 @@ $env = $input->getParameterOption(['--env', '-e'], $_SERVER['APP_ENV'] ?? 'test' $debug = ($_SERVER['APP_DEBUG'] ?? false) && !$input->hasParameterOption('--no-debug', true) && 'prod' !== $env; if ($debug) { - Debug::enable(); + class_exists(Debug::class) ? Debug::enable() : LegacyDebug::enable(); } error_reporting(\E_ALL & ~\E_USER_DEPRECATED); diff --git a/tests/Fixtures/app/object-manager.php b/tests/Fixtures/app/object-manager.php new file mode 100644 index 00000000000..9749d3ce531 --- /dev/null +++ b/tests/Fixtures/app/object-manager.php @@ -0,0 +1,7 @@ +boot(); +return $kernel->getContainer()->get('doctrine')->getManager(); From 2a9fa0bcc2d35df251f99c893a75de02f3eb0d1d Mon Sep 17 00:00:00 2001 From: daniele Date: Tue, 9 Nov 2021 12:32:32 -0800 Subject: [PATCH 2/2] ci: added some small fixes to PHPStan --- tests/Fixtures/TestBundle/Document/VoDummyInspection.php | 2 +- tests/Fixtures/TestBundle/Document/WithJsonDummy.php | 4 ++-- tests/Fixtures/TestBundle/Entity/User.php | 2 +- tests/Fixtures/TestBundle/Entity/VoDummyInspection.php | 4 ++-- tests/Fixtures/TestBundle/Entity/VoDummyVehicle.php | 2 +- tests/Fixtures/TestBundle/Entity/WithJsonDummy.php | 4 ++-- tests/Swagger/Serializer/DocumentationNormalizerV3Test.php | 7 +++++-- tests/Util/SortTraitTest.php | 2 +- 8 files changed, 15 insertions(+), 12 deletions(-) diff --git a/tests/Fixtures/TestBundle/Document/VoDummyInspection.php b/tests/Fixtures/TestBundle/Document/VoDummyInspection.php index 79d267bcb1c..3160a6ca039 100644 --- a/tests/Fixtures/TestBundle/Document/VoDummyInspection.php +++ b/tests/Fixtures/TestBundle/Document/VoDummyInspection.php @@ -56,7 +56,7 @@ class VoDummyInspection */ private $performed; - private $attributeWithoutConstructorEquivalent; + private $attributeWithoutConstructorEquivalent; /** @phpstan-ignore-line Intentionally non-serialized value */ public function __construct(bool $accepted, VoDummyCar $car, DateTime $performed = null, string $parameterWhichIsNotClassAttribute = '') { diff --git a/tests/Fixtures/TestBundle/Document/WithJsonDummy.php b/tests/Fixtures/TestBundle/Document/WithJsonDummy.php index a0a82da40b8..50287eb82dc 100644 --- a/tests/Fixtures/TestBundle/Document/WithJsonDummy.php +++ b/tests/Fixtures/TestBundle/Document/WithJsonDummy.php @@ -23,9 +23,9 @@ class WithJsonDummy { /** - * @var int + * @var int|null * - * @ODM\Id(strategy="INCREMENT", type="int", nullable=true) + * @ODM\Id(strategy="INCREMENT", type="int") */ private $id; diff --git a/tests/Fixtures/TestBundle/Entity/User.php b/tests/Fixtures/TestBundle/Entity/User.php index 616b9d4a8d0..ef08a021e6c 100644 --- a/tests/Fixtures/TestBundle/Entity/User.php +++ b/tests/Fixtures/TestBundle/Entity/User.php @@ -97,7 +97,7 @@ class User extends AbstractSecurityUser * * @Groups({"user"}) */ - private $username; + private $username; /** @phpstan-ignore-line Used through serialization group */ public function getId(): int { diff --git a/tests/Fixtures/TestBundle/Entity/VoDummyInspection.php b/tests/Fixtures/TestBundle/Entity/VoDummyInspection.php index dcb13c5d6ca..747efe08ddb 100644 --- a/tests/Fixtures/TestBundle/Entity/VoDummyInspection.php +++ b/tests/Fixtures/TestBundle/Entity/VoDummyInspection.php @@ -41,7 +41,7 @@ class VoDummyInspection private $accepted; /** - * @var VoDummyCar + * @var VoDummyCar|null * * @ORM\ManyToOne(targetEntity="VoDummyCar", inversedBy="inspections") * @Groups({"inspection_read", "inspection_write"}) @@ -56,7 +56,7 @@ class VoDummyInspection */ private $performed; - private $attributeWithoutConstructorEquivalent; + private $attributeWithoutConstructorEquivalent; /** @phpstan-ignore-line Intentionally non-serialized value */ public function __construct(bool $accepted, VoDummyCar $car, DateTime $performed = null, string $parameterWhichIsNotClassAttribute = '') { diff --git a/tests/Fixtures/TestBundle/Entity/VoDummyVehicle.php b/tests/Fixtures/TestBundle/Entity/VoDummyVehicle.php index 14e03216652..73886370999 100644 --- a/tests/Fixtures/TestBundle/Entity/VoDummyVehicle.php +++ b/tests/Fixtures/TestBundle/Entity/VoDummyVehicle.php @@ -34,7 +34,7 @@ abstract class VoDummyVehicle private $make; /** - * @var VoDummyInsuranceCompany + * @var VoDummyInsuranceCompany|null * * @ORM\ManyToOne(targetEntity="VoDummyInsuranceCompany", cascade={"persist"}) * @Groups({"car_read", "car_write"}) diff --git a/tests/Fixtures/TestBundle/Entity/WithJsonDummy.php b/tests/Fixtures/TestBundle/Entity/WithJsonDummy.php index a2769c87c1f..ba05b0ce49e 100644 --- a/tests/Fixtures/TestBundle/Entity/WithJsonDummy.php +++ b/tests/Fixtures/TestBundle/Entity/WithJsonDummy.php @@ -23,9 +23,9 @@ class WithJsonDummy { /** - * @var int + * @var int|null * - * @ORM\Column(type="integer", nullable=true) + * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index b2af2cf3337..49e58424c1e 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -3000,7 +3000,10 @@ public function testLegacyNormalizeWithCustomFormatsDefinedAtOperationLevel(): v $this->doTestNormalizeWithCustomFormatsDefinedAtOperationLevel($formatProviderProphecy->reveal()); } - private function doTestNormalizeWithCustomFormatsDefinedAtOperationLevel(OperationAwareFormatsProviderInterface $formatsProvider = null): void + /** + * @param array|OperationAwareFormatsProviderInterface $formatsProvider + */ + private function doTestNormalizeWithCustomFormatsDefinedAtOperationLevel($formatsProvider = null): void { $documentation = new Documentation(new ResourceNameCollection([Dummy::class]), 'Test API', 'This is a test API.', '1.2.3'); @@ -3264,7 +3267,7 @@ public function testNormalizeOpenApi() 'page', false, 'itemsPerPage', - $formatsProvider ?? [], + [], false, 'pagination', ['spec_version' => 3], diff --git a/tests/Util/SortTraitTest.php b/tests/Util/SortTraitTest.php index 4a924a13afa..b4664426562 100644 --- a/tests/Util/SortTraitTest.php +++ b/tests/Util/SortTraitTest.php @@ -45,7 +45,7 @@ public function testArrayRecursiveSort(): void $sortTrait->arrayRecursiveSort($array, 'sort'); - $this->assertSame([ + $this->assertSame([ /** @phpstan-ignore-line PHPStan bug with pass by reference */ 'first', 'second', [