Skip to content

Commit aa722f0

Browse files
authored
Gather assert types conditional on installed PHPStan version (#301)
* Gather assert types conditional on installed PHPStan version * Update DynamicReturnTypeExtensionTest.php
1 parent 1cdb6d5 commit aa722f0

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
"require": {
1414
"php": "^7.4 || ^8.0",
1515
"php-stubs/wordpress-stubs": "^6.6.2",
16-
"phpstan/phpstan": "^2.1.18"
16+
"phpstan/phpstan": "^2.0"
1717
},
1818
"require-dev": {
1919
"composer/composer": "^2.1.14",
20+
"composer/semver": "^3.4",
2021
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
2122
"php-parallel-lint/php-parallel-lint": "^1.1",
2223
"phpstan/phpstan-strict-rules": "^2.0",
@@ -42,7 +43,8 @@
4243
"config": {
4344
"allow-plugins": {
4445
"dealerdirect/phpcodesniffer-composer-installer": true
45-
}
46+
},
47+
"sort-packages": true
4648
},
4749
"extra": {
4850
"phpstan": {

tests/DynamicReturnTypeExtensionTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@ class DynamicReturnTypeExtensionTest extends \PHPStan\Testing\TypeInferenceTestC
1111
*/
1212
public function dataFileAsserts(): iterable
1313
{
14-
// Path to a file with actual asserts of expected types:
14+
$phpstanVersion = self::getContainer()->getByType(InstalledPhpStanVersion::class);
15+
16+
if ($phpstanVersion->satisfies('^2.1.18')) {
17+
// Improved rtrim handling in PHPStan 2.1.18 gives different results
18+
yield from self::gatherAssertTypes(__DIR__ . '/data/slashit-functions.php');
19+
}
20+
21+
// Include for all supported PHPStan versions
1522
yield from self::gatherAssertTypes(__DIR__ . '/data/apply-filters.php');
1623
yield from self::gatherAssertTypes(__DIR__ . '/data/ApplyFiltersTestClass.php');
1724
yield from self::gatherAssertTypes(__DIR__ . '/data/esc-sql.php');
1825
yield from self::gatherAssertTypes(__DIR__ . '/data/normalize-whitespace.php');
1926
yield from self::gatherAssertTypes(__DIR__ . '/data/shortcode-atts.php');
20-
yield from self::gatherAssertTypes(__DIR__ . '/data/slashit-functions.php');
2127
yield from self::gatherAssertTypes(__DIR__ . '/data/stripslashes-from-strings-only.php');
2228
yield from self::gatherAssertTypes(__DIR__ . '/data/wp-parse-url.php');
2329
yield from self::gatherAssertTypes(__DIR__ . '/data/wp-slash.php');
@@ -34,6 +40,9 @@ public function testFileAsserts(string $assertType, string $file, ...$args): voi
3440

3541
public static function getAdditionalConfigFiles(): array
3642
{
37-
return [dirname(__DIR__) . '/vendor/szepeviktor/phpstan-wordpress/extension.neon'];
43+
return [
44+
dirname(__DIR__) . '/vendor/szepeviktor/phpstan-wordpress/extension.neon',
45+
__DIR__ . '/test-services.neon',
46+
];
3847
}
3948
}

tests/InstalledPhpStanVersion.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SzepeViktor\PHPStan\WordPress\Tests;
6+
7+
use Composer\InstalledVersions;
8+
use Composer\Semver\Semver;
9+
10+
final class InstalledPhpStanVersion
11+
{
12+
private string $version;
13+
14+
public function __construct()
15+
{
16+
$version = InstalledVersions::getVersion('phpstan/phpstan-src');
17+
18+
if ($version === null) {
19+
throw new \RuntimeException('Cannot determine PHPStan version from Composer.');
20+
}
21+
22+
$this->version = $version;
23+
}
24+
25+
public function satisfies(string $constraints): bool
26+
{
27+
return Semver::satisfies($this->version, $constraints);
28+
}
29+
}

tests/test-services.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
services:
2+
-
3+
class: SzepeViktor\PHPStan\WordPress\Tests\InstalledPhpStanVersion

0 commit comments

Comments
 (0)