Skip to content

Commit a0e007a

Browse files
staabmondrejmirtes
authored andcommitted
non-capturing catch support-detection is scope php-version dependent
1 parent bd0cf35 commit a0e007a

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/Php/PhpVersions.php

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public function __construct(
1818
{
1919
}
2020

21+
public function supportsNoncapturingCatches(): TrinaryLogic
22+
{
23+
return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
24+
}
25+
2126
public function producesWarningForFinalPrivateMethods(): TrinaryLogic
2227
{
2328
return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;

src/Rules/Exceptions/NoncapturingCatchRule.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7-
use PHPStan\Php\PhpVersion;
87
use PHPStan\Rules\Rule;
98
use PHPStan\Rules\RuleErrorBuilder;
109

@@ -14,10 +13,6 @@
1413
final class NoncapturingCatchRule implements Rule
1514
{
1615

17-
public function __construct(private PhpVersion $phpVersion)
18-
{
19-
}
20-
2116
public function getNodeType(): string
2217
{
2318
return Node\Stmt\Catch_::class;
@@ -28,7 +23,7 @@ public function getNodeType(): string
2823
*/
2924
public function processNode(Node $node, Scope $scope): array
3025
{
31-
if ($this->phpVersion->supportsNoncapturingCatches()) {
26+
if ($scope->getPhpVersion()->supportsNoncapturingCatches()->yes()) {
3227
return [];
3328
}
3429

tests/PHPStan/Rules/Exceptions/NoncapturingCatchRuleTest.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
use PHPStan\Php\PhpVersion;
66
use PHPStan\Rules\Rule;
77
use PHPStan\Testing\RuleTestCase;
8+
use const PHP_VERSION_ID;
89

910
/**
1011
* @extends RuleTestCase<NoncapturingCatchRule>
1112
*/
1213
class NoncapturingCatchRuleTest extends RuleTestCase
1314
{
1415

15-
private PhpVersion $phpVersion;
16-
1716
protected function getRule(): Rule
1817
{
19-
return new NoncapturingCatchRule($this->phpVersion);
18+
return new NoncapturingCatchRule();
2019
}
2120

2221
public function dataRule(): array
@@ -49,7 +48,14 @@ public function dataRule(): array
4948
*/
5049
public function testRule(int $phpVersion, array $expectedErrors): void
5150
{
52-
$this->phpVersion = new PhpVersion($phpVersion);
51+
$testVersion = new PhpVersion($phpVersion);
52+
$runtimeVersion = new PhpVersion(PHP_VERSION_ID);
53+
if (
54+
$testVersion->getMajorVersionId() !== $runtimeVersion->getMajorVersionId()
55+
|| $testVersion->getMinorVersionId() !== $runtimeVersion->getMinorVersionId()
56+
) {
57+
$this->markTestSkipped('Test requires PHP version ' . $testVersion->getMajorVersionId() . '.' . $testVersion->getMinorVersionId() . '.*');
58+
}
5359

5460
$this->analyse([
5561
__DIR__ . '/data/noncapturing-catch.php',

0 commit comments

Comments
 (0)