File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff line change 66use PhpParser \Node \Expr \Cast ;
77use PHPStan \Analyser \Scope ;
88use PHPStan \DependencyInjection \RegisteredRule ;
9- use PHPStan \Php \PhpVersion ;
109use PHPStan \Rules \Rule ;
1110use PHPStan \Rules \RuleErrorBuilder ;
1211
1716final 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
Original file line number Diff line number Diff line change 22
33namespace PHPStan \Rules \Cast ;
44
5- use PHPStan \Php \PhpVersion ;
65use PHPStan \Rules \Rule ;
76use PHPStan \Testing \RuleTestCase ;
87use 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments