Skip to content

Commit d2a29e3

Browse files
authored
DeprecatedCastRule: Utilize Scope::getPhpVersion() (#6600)
1 parent a0d6b4c commit d2a29e3

4 files changed

Lines changed: 41 additions & 8 deletions

File tree

‎src/Php/PhpVersions.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ public function supportsVoidCast(): TrinaryLogic
183183
return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
184184
}
185185

186+
public function deprecatesNonStandardCasts(): TrinaryLogic
187+
{
188+
return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
189+
}
190+
186191
public function supportsDeprecatedTraits(): TrinaryLogic
187192
{
188193
return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;

‎src/Rules/Cast/DeprecatedCastRule.php‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PhpParser\Node\Expr\Cast;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\DependencyInjection\RegisteredRule;
9-
use PHPStan\Php\PhpVersion;
109
use PHPStan\Rules\Rule;
1110
use PHPStan\Rules\RuleErrorBuilder;
1211

@@ -17,18 +16,14 @@
1716
final class DeprecatedCastRule implements Rule
1817
{
1918

20-
public function __construct(private PhpVersion $phpVersion)
21-
{
22-
}
23-
2419
public function getNodeType(): string
2520
{
2621
return Cast::class;
2722
}
2823

2924
public function processNode(Node $node, Scope $scope): array
3025
{
31-
if (!$this->phpVersion->deprecatesNonStandardCasts()) {
26+
if (!$scope->getPhpVersion()->deprecatesNonStandardCasts()->yes()) {
3227
return [];
3328
}
3429

‎tests/PHPStan/Rules/Cast/DeprecatedCastRuleTest.php‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PHPStan\Rules\Cast;
44

5-
use PHPStan\Php\PhpVersion;
65
use PHPStan\Rules\Rule;
76
use PHPStan\Testing\RuleTestCase;
87
use const PHP_VERSION_ID;
@@ -15,7 +14,7 @@ class DeprecatedCastRuleTest extends RuleTestCase
1514

1615
protected function getRule(): Rule
1716
{
18-
return new DeprecatedCastRule(new PhpVersion(PHP_VERSION_ID));
17+
return new DeprecatedCastRule();
1918
}
2019

2120
public function testRule(): void
@@ -45,4 +44,22 @@ public function testRule(): void
4544
$this->analyse([__DIR__ . '/data/deprecated-cast.php'], $errors);
4645
}
4746

47+
public function testConditionallyExecutedCode(): void
48+
{
49+
$errors = [
50+
[
51+
'Non-standard (integer) cast is deprecated in PHP 8.5. Use (int) instead.',
52+
8,
53+
],
54+
];
55+
if (PHP_VERSION_ID >= 80500) {
56+
$errors[] = [
57+
'Non-standard (integer) cast is deprecated in PHP 8.5. Use (int) instead.',
58+
15,
59+
];
60+
}
61+
62+
$this->analyse([__DIR__ . '/data/deprecated-cast-php-versions.php'], $errors);
63+
}
64+
4865
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php // lint >= 8.5
2+
3+
namespace DeprecatedCastPhpVersions;
4+
5+
function doFoo($value): void
6+
{
7+
if (PHP_VERSION_ID >= 80500) {
8+
(integer) $value;
9+
}
10+
11+
if (PHP_VERSION_ID < 80500) {
12+
(integer) $value;
13+
}
14+
15+
(integer) $value;
16+
}

0 commit comments

Comments
 (0)