From e6c221977ad249c4f587a4eeaeea5dcbd00e8f5f Mon Sep 17 00:00:00 2001 From: Marian <42134098+IanDelMar@users.noreply.github.com> Date: Sun, 14 Sep 2025 00:43:58 +0200 Subject: [PATCH 1/2] Gather assert types conditional on installed PHPStan version --- composer.json | 6 +++-- tests/DynamicReturnTypeExtensionTest.php | 14 +++++++++--- tests/InstalledPhpStanVersion.php | 29 ++++++++++++++++++++++++ tests/test-services.neon | 3 +++ 4 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 tests/InstalledPhpStanVersion.php create mode 100644 tests/test-services.neon diff --git a/composer.json b/composer.json index 602a473..e7230f4 100644 --- a/composer.json +++ b/composer.json @@ -13,10 +13,11 @@ "require": { "php": "^7.4 || ^8.0", "php-stubs/wordpress-stubs": "^6.6.2", - "phpstan/phpstan": "^2.1.18" + "phpstan/phpstan": "^2.0" }, "require-dev": { "composer/composer": "^2.1.14", + "composer/semver": "^3.4", "dealerdirect/phpcodesniffer-composer-installer": "^1.0", "php-parallel-lint/php-parallel-lint": "^1.1", "phpstan/phpstan-strict-rules": "^2.0", @@ -42,7 +43,8 @@ "config": { "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true - } + }, + "sort-packages": true }, "extra": { "phpstan": { diff --git a/tests/DynamicReturnTypeExtensionTest.php b/tests/DynamicReturnTypeExtensionTest.php index 4f3df6c..3a96f49 100644 --- a/tests/DynamicReturnTypeExtensionTest.php +++ b/tests/DynamicReturnTypeExtensionTest.php @@ -11,16 +11,21 @@ class DynamicReturnTypeExtensionTest extends \PHPStan\Testing\TypeInferenceTestC */ public function dataFileAsserts(): iterable { - // Path to a file with actual asserts of expected types: yield from self::gatherAssertTypes(__DIR__ . '/data/apply-filters.php'); yield from self::gatherAssertTypes(__DIR__ . '/data/ApplyFiltersTestClass.php'); yield from self::gatherAssertTypes(__DIR__ . '/data/esc-sql.php'); yield from self::gatherAssertTypes(__DIR__ . '/data/normalize-whitespace.php'); yield from self::gatherAssertTypes(__DIR__ . '/data/shortcode-atts.php'); - yield from self::gatherAssertTypes(__DIR__ . '/data/slashit-functions.php'); yield from self::gatherAssertTypes(__DIR__ . '/data/stripslashes-from-strings-only.php'); yield from self::gatherAssertTypes(__DIR__ . '/data/wp-parse-url.php'); yield from self::gatherAssertTypes(__DIR__ . '/data/wp-slash.php'); + + $phpstanVersion = self::getContainer()->getByType(InstalledPhpStanVersion::class); + + if ($phpstanVersion->satisfies('^2.1.18')) { + // Improved rtrim handling in PHPStan 2.1.18 gives different results + yield from self::gatherAssertTypes(__DIR__ . '/data/slashit-functions.php'); + } } /** @@ -34,6 +39,9 @@ public function testFileAsserts(string $assertType, string $file, ...$args): voi public static function getAdditionalConfigFiles(): array { - return [dirname(__DIR__) . '/vendor/szepeviktor/phpstan-wordpress/extension.neon']; + return [ + dirname(__DIR__) . '/vendor/szepeviktor/phpstan-wordpress/extension.neon', + __DIR__ . '/test-services.neon', + ]; } } diff --git a/tests/InstalledPhpStanVersion.php b/tests/InstalledPhpStanVersion.php new file mode 100644 index 0000000..90b4eae --- /dev/null +++ b/tests/InstalledPhpStanVersion.php @@ -0,0 +1,29 @@ +version = $version; + } + + public function satisfies(string $constraints): bool + { + return Semver::satisfies($this->version, $constraints); + } +} diff --git a/tests/test-services.neon b/tests/test-services.neon new file mode 100644 index 0000000..e453d3b --- /dev/null +++ b/tests/test-services.neon @@ -0,0 +1,3 @@ +services: + - + class: SzepeViktor\PHPStan\WordPress\Tests\InstalledPhpStanVersion From 8d69a1d6a59b11ca364f00a0e5f47e8ce57cb564 Mon Sep 17 00:00:00 2001 From: Marian <42134098+IanDelMar@users.noreply.github.com> Date: Sun, 14 Sep 2025 01:34:07 +0200 Subject: [PATCH 2/2] Update DynamicReturnTypeExtensionTest.php --- tests/DynamicReturnTypeExtensionTest.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/DynamicReturnTypeExtensionTest.php b/tests/DynamicReturnTypeExtensionTest.php index 3a96f49..5f1c3bb 100644 --- a/tests/DynamicReturnTypeExtensionTest.php +++ b/tests/DynamicReturnTypeExtensionTest.php @@ -11,6 +11,14 @@ class DynamicReturnTypeExtensionTest extends \PHPStan\Testing\TypeInferenceTestC */ public function dataFileAsserts(): iterable { + $phpstanVersion = self::getContainer()->getByType(InstalledPhpStanVersion::class); + + if ($phpstanVersion->satisfies('^2.1.18')) { + // Improved rtrim handling in PHPStan 2.1.18 gives different results + yield from self::gatherAssertTypes(__DIR__ . '/data/slashit-functions.php'); + } + + // Include for all supported PHPStan versions yield from self::gatherAssertTypes(__DIR__ . '/data/apply-filters.php'); yield from self::gatherAssertTypes(__DIR__ . '/data/ApplyFiltersTestClass.php'); yield from self::gatherAssertTypes(__DIR__ . '/data/esc-sql.php'); @@ -19,13 +27,6 @@ public function dataFileAsserts(): iterable yield from self::gatherAssertTypes(__DIR__ . '/data/stripslashes-from-strings-only.php'); yield from self::gatherAssertTypes(__DIR__ . '/data/wp-parse-url.php'); yield from self::gatherAssertTypes(__DIR__ . '/data/wp-slash.php'); - - $phpstanVersion = self::getContainer()->getByType(InstalledPhpStanVersion::class); - - if ($phpstanVersion->satisfies('^2.1.18')) { - // Improved rtrim handling in PHPStan 2.1.18 gives different results - yield from self::gatherAssertTypes(__DIR__ . '/data/slashit-functions.php'); - } } /**