From a40b4e0e80ecd5e55e8e41fbec3bf63c4bbcf179 Mon Sep 17 00:00:00 2001 From: Michael Babker Date: Tue, 3 Dec 2024 19:20:37 -0500 Subject: [PATCH] PHPStan 2.0 --- .github/workflows/run-tests.yml | 4 +- .github/workflows/static-analysis.yml | 2 +- composer.json | 6 +-- phpstan-baseline.neon | 48 ++++++++++++++++--- .../RouterAwareRouteGenerator.php | 6 +++ src/Twig/UndefinedCallableHandler.php | 2 +- .../RequestAwareRouteGeneratorFactoryTest.php | 16 +++---- .../RouterAwareRouteGeneratorTest.php | 2 +- tests/View/TwigViewIntegrationTest.php | 2 +- 9 files changed, 65 insertions(+), 23 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 8d290c5..baddc7e 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -10,7 +10,7 @@ jobs: fail-fast: true matrix: php: ['8.1', '8.2', '8.3', '8.4'] - symfony: ['5.4.*', '6.4.*', '7.1.*', '7.2.*@rc'] + symfony: ['5.4.*', '6.4.*', '7.1.*', '7.2.*'] pagerfanta: [''] composer-flags: ['--prefer-stable'] can-fail: [false] @@ -31,7 +31,7 @@ jobs: - php: '8.1' symfony: '7.1.*' - php: '8.1' - symfony: '7.2.*@rc' + symfony: '7.2.*' name: "PHP ${{ matrix.php }}${{ matrix.pagerfanta != '' && format(' - Pagerfanta {0}', matrix.pagerfanta) || '' }}${{ matrix.with-twig == false && ' - Without Twig' || '' }} - Symfony ${{ matrix.symfony }}${{ matrix.composer-flags != '' && format(' - Composer {0}', matrix.composer-flags) || '' }}" diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index eec85bc..d090bb0 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -23,7 +23,7 @@ jobs: - name: Install dependencies run: composer update --prefer-stable --prefer-dist env: - SYMFONY_REQUIRE: '7.2.*@rc' + SYMFONY_REQUIRE: '7.2.*' - name: Run PHPStan run: vendor/bin/phpstan analyze --error-format=github diff --git a/composer.json b/composer.json index 81b4774..4b375c7 100644 --- a/composer.json +++ b/composer.json @@ -21,9 +21,9 @@ "matthiasnoback/symfony-dependency-injection-test": "^5.0", "pagerfanta/twig": "^3.7 || ^4.0", "phpstan/extension-installer": "^1.3", - "phpstan/phpstan": "1.12.11", - "phpstan/phpstan-phpunit": "1.4.1", - "phpstan/phpstan-symfony": "1.4.12", + "phpstan/phpstan": "2.0.3", + "phpstan/phpstan-phpunit": "2.0.1", + "phpstan/phpstan-symfony": "2.0.0", "phpunit/phpunit": "9.6.21", "symfony/phpunit-bridge": "^5.4 || ^6.4 || ^7.1", "symfony/serializer": "^5.4 || ^6.4 || ^7.1", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 0aa8add..d576837 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,32 +1,68 @@ parameters: ignoreErrors: - - message: "#^Parameter \\#2 \\.\\.\\.\\$arrays of function array_merge expects array, mixed given\\.$#" + message: '#^Cannot access offset ''not_valid_current_page'' on mixed\.$#' + identifier: offsetAccess.nonOffsetAccessible + count: 1 + path: src/DependencyInjection/BabDevPagerfantaExtension.php + + - + message: '#^Cannot access offset ''out_of_range_page'' on mixed\.$#' + identifier: offsetAccess.nonOffsetAccessible + count: 1 + path: src/DependencyInjection/BabDevPagerfantaExtension.php + + - + message: '#^Cannot access offset ''alias'' on mixed\.$#' + identifier: offsetAccess.nonOffsetAccessible + count: 2 + path: src/DependencyInjection/CompilerPass/RegisterPagerfantaViewsPass.php + + - + message: '#^Parameter \#2 \.\.\.\$arrays of function array_merge expects array, mixed given\.$#' + identifier: argument.type count: 2 path: src/RouteGenerator/RequestAwareRouteGeneratorFactory.php - - message: "#^Parameter \\#2 \\$parameters of method Symfony\\\\Component\\\\Routing\\\\Generator\\\\UrlGeneratorInterface\\:\\:generate\\(\\) expects array, array\\|object given\\.$#" + message: '#^Parameter \#3 \$options of class BabDev\\PagerfantaBundle\\RouteGenerator\\RouterAwareRouteGenerator constructor expects array\{routeName\: non\-empty\-string, pageParameter\?\: non\-empty\-string, omitFirstPage\?\: bool, routeParams\?\: array\, referenceType\?\: 0\|1\|2\|3\}, non\-empty\-array\ given\.$#' + identifier: argument.type + count: 1 + path: src/RouteGenerator/RequestAwareRouteGeneratorFactory.php + + - + message: '#^Default value of the parameter \#3 \$options \(array\{\}\) of method BabDev\\PagerfantaBundle\\RouteGenerator\\RouterAwareRouteGenerator\:\:__construct\(\) is incompatible with type array\{routeName\: non\-empty\-string, pageParameter\?\: non\-empty\-string, omitFirstPage\?\: bool, routeParams\?\: array\, referenceType\?\: 0\|1\|2\|3\}\.$#' + identifier: parameter.defaultValue count: 1 path: src/RouteGenerator/RouterAwareRouteGenerator.php - - message: "#^Method BabDev\\\\PagerfantaBundle\\\\View\\\\ContainerBackedImmutableViewFactory\\:\\:get\\(\\) should return Pagerfanta\\\\View\\\\ViewInterface but returns mixed\\.$#" + message: '#^Method BabDev\\PagerfantaBundle\\Serializer\\Handler\\PagerfantaHandler\:\:serializeToJson\(\) should return array\\|ArrayObject\ but returns array\|ArrayObject\.$#' + identifier: return.type + count: 1 + path: src/Serializer/Handler/PagerfantaHandler.php + + - + message: '#^Method BabDev\\PagerfantaBundle\\View\\ContainerBackedImmutableViewFactory\:\:get\(\) should return Pagerfanta\\View\\ViewInterface but returns mixed\.$#' + identifier: return.type count: 1 path: src/View/ContainerBackedImmutableViewFactory.php - - message: "#^Parameter \\#2 \\$array of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertArrayHasKey\\(\\) expects array\\|ArrayAccess, mixed given\\.$#" + message: '#^Parameter \#2 \$array of static method PHPUnit\\Framework\\Assert\:\:assertArrayHasKey\(\) expects array\|ArrayAccess, mixed given\.$#' + identifier: argument.type count: 1 path: tests/DependencyInjection/BabDevPagerfantaExtensionTest.php - - message: "#^Method BabDev\\\\PagerfantaBundle\\\\Tests\\\\View\\\\TwigViewIntegrationTest\\:\\:removeWhitespacesBetweenTags\\(\\) should return string but returns string\\|null\\.$#" + message: '#^Method BabDev\\PagerfantaBundle\\Tests\\View\\TwigViewIntegrationTest\:\:removeWhitespacesBetweenTags\(\) should return string but returns string\|null\.$#' + identifier: return.type count: 1 path: tests/View/TwigViewIntegrationTest.php - - message: "#^Parameter \\#1 \\$path of function dirname expects string, string\\|false given\\.$#" + message: '#^Parameter \#1 \$path of function dirname expects string, string\|false given\.$#' + identifier: argument.type count: 1 path: tests/View/TwigViewIntegrationTest.php diff --git a/src/RouteGenerator/RouterAwareRouteGenerator.php b/src/RouteGenerator/RouterAwareRouteGenerator.php index cda5788..a72ae2f 100644 --- a/src/RouteGenerator/RouterAwareRouteGenerator.php +++ b/src/RouteGenerator/RouterAwareRouteGenerator.php @@ -8,8 +8,14 @@ use Symfony\Component\PropertyAccess\PropertyPath; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +/** + * @phpstan-type RouteGeneratorOptions array{routeName: non-empty-string, pageParameter?: non-empty-string, omitFirstPage?: bool, routeParams?: array, referenceType?: UrlGeneratorInterface::*} + */ final class RouterAwareRouteGenerator implements RouteGeneratorInterface { + /** + * @phpstan-param RouteGeneratorOptions $options + */ public function __construct( private readonly UrlGeneratorInterface $router, private readonly PropertyAccessorInterface $propertyAccessor, diff --git a/src/Twig/UndefinedCallableHandler.php b/src/Twig/UndefinedCallableHandler.php index a729360..9d40989 100644 --- a/src/Twig/UndefinedCallableHandler.php +++ b/src/Twig/UndefinedCallableHandler.php @@ -17,7 +17,7 @@ final class UndefinedCallableHandler /** * @throws SyntaxError if the undefined function is supported by this handler */ - public function onUndefinedFunction(string $name): ?bool + public function onUndefinedFunction(string $name): bool { if (!\in_array($name, self::SUPPORTED_FUNCTIONS, true)) { return false; diff --git a/tests/RouteGenerator/RequestAwareRouteGeneratorFactoryTest.php b/tests/RouteGenerator/RequestAwareRouteGeneratorFactoryTest.php index 1cd3272..1e4d8e2 100644 --- a/tests/RouteGenerator/RequestAwareRouteGeneratorFactoryTest.php +++ b/tests/RouteGenerator/RequestAwareRouteGeneratorFactoryTest.php @@ -34,6 +34,9 @@ protected function tearDown(): void } while (null !== $request); } + /** + * @doesNotPerformAssertions + */ public function testTheGeneratorIsCreatedWhenResolvingTheRouteNameFromTheRequest(): void { $request = Request::create('/'); @@ -42,12 +45,12 @@ public function testTheGeneratorIsCreatedWhenResolvingTheRouteNameFromTheRequest $this->requestStack->push($request); - self::assertInstanceOf( - RouteGeneratorInterface::class, - $this->createFactory()->create(), - ); + $this->createFactory()->create(); } + /** + * @doesNotPerformAssertions + */ public function testTheGeneratorIsCreatedWhenGivenARouteNameDuringASubrequest(): void { $masterRequest = Request::create('/'); @@ -59,10 +62,7 @@ public function testTheGeneratorIsCreatedWhenGivenARouteNameDuringASubrequest(): $this->requestStack->push($masterRequest); $this->requestStack->push($subRequest); - self::assertInstanceOf( - RouteGeneratorInterface::class, - $this->createFactory()->create(['routeName' => 'pagerfanta_view']), - ); + $this->createFactory()->create(['routeName' => 'pagerfanta_view']); } public function testTheGeneratorIsNotCreatedWhenARouteNameIsNotGivenDuringASubrequest(): void diff --git a/tests/RouteGenerator/RouterAwareRouteGeneratorTest.php b/tests/RouteGenerator/RouterAwareRouteGeneratorTest.php index 7986d5c..8c90f39 100644 --- a/tests/RouteGenerator/RouterAwareRouteGeneratorTest.php +++ b/tests/RouteGenerator/RouterAwareRouteGeneratorTest.php @@ -90,7 +90,7 @@ public function testARouteIsNotGeneratedWhenTheRouteNameParameterIsMissing(): vo $generator = new RouterAwareRouteGenerator( $this->createRouter(), $this->createPropertyAccessor(), - ['routeParams' => ['hello' => 'world']], + ['routeParams' => ['hello' => 'world']], /** @phpstan-ignore-line argument.type */ ); $generator(1); diff --git a/tests/View/TwigViewIntegrationTest.php b/tests/View/TwigViewIntegrationTest.php index 0eeafd2..9ef8869 100644 --- a/tests/View/TwigViewIntegrationTest.php +++ b/tests/View/TwigViewIntegrationTest.php @@ -81,7 +81,7 @@ protected function tearDown(): void } /** - * @return Pagerfanta + * @return Pagerfanta> */ private function createPagerfanta(): Pagerfanta {