File tree Expand file tree Collapse file tree 4 files changed +56
-9
lines changed Expand file tree Collapse file tree 4 files changed +56
-9
lines changed Original file line number Diff line number Diff line change @@ -429,4 +429,9 @@ public function supportsFinalPromotedProperties(): bool
429429 return $ this ->versionId >= 80500 ;
430430 }
431431
432+ public function supportsVoidCast (): bool
433+ {
434+ return $ this ->versionId >= 80500 ;
435+ }
436+
432437}
Original file line number Diff line number Diff line change 55use PhpParser \Node ;
66use PHPStan \Analyser \Scope ;
77use PHPStan \DependencyInjection \RegisteredRule ;
8+ use PHPStan \Php \PhpVersion ;
89use PHPStan \Rules \Rule ;
910use PHPStan \Rules \RuleErrorBuilder ;
1011
1516final class VoidCastRule implements Rule
1617{
1718
18- public function __construct ()
19+ public function __construct (
20+ private PhpVersion $ phpVersion ,
21+ )
1922 {
2023 }
2124
@@ -26,16 +29,24 @@ public function getNodeType(): string
2629
2730 public function processNode (Node $ node , Scope $ scope ): array
2831 {
32+ $ errors = [];
33+ if (!$ this ->phpVersion ->supportsVoidCast ()) {
34+ $ errors [] = RuleErrorBuilder::message ('The (void) cast is supported only on PHP 8.5 and later. ' )
35+ ->identifier ('cast.voidNotSupported ' )
36+ ->nonIgnorable ()
37+ ->build ();
38+ }
39+
2940 if ($ scope ->isInFirstLevelStatement ()) {
30- return [] ;
41+ return $ errors ;
3142 }
3243
33- return [
34- RuleErrorBuilder:: message ( ' The (void) cast cannot be used within an expression. ' )
35- -> identifier ( ' cast.void ' )
36- -> nonIgnorable ()
37- -> build (),
38- ] ;
44+ $ errors [] = RuleErrorBuilder:: message ( ' The (void) cast cannot be used within an expression. ' )
45+ -> identifier ( ' cast.void ' )
46+ -> nonIgnorable ( )
47+ -> build ();
48+
49+ return $ errors ;
3950 }
4051
4152}
Original file line number Diff line number Diff line change 22
33namespace PHPStan \Rules \Cast ;
44
5+ use PHPStan \Php \PhpVersion ;
56use PHPStan \Rules \Rule ;
67use PHPStan \Testing \RuleTestCase ;
8+ use PHPUnit \Framework \Attributes \RequiresPhp ;
9+ use const PHP_VERSION_ID ;
710
811/**
912 * @extends RuleTestCase<VoidCastRule>
@@ -13,9 +16,10 @@ class VoidCastRuleTest extends RuleTestCase
1316
1417 protected function getRule (): Rule
1518 {
16- return new VoidCastRule ();
19+ return new VoidCastRule (new PhpVersion ( PHP_VERSION_ID ) );
1720 }
1821
22+ #[RequiresPhp('>= 8.5 ' )]
1923 public function testPrintRule (): void
2024 {
2125 $ this ->analyse ([__DIR__ . '/data/void-cast.php ' ], [
@@ -34,4 +38,18 @@ public function testPrintRule(): void
3438 ]);
3539 }
3640
41+ public function testSupport (): void
42+ {
43+ $ errors = [];
44+ if (PHP_VERSION_ID < 80500 ) {
45+ $ errors = [
46+ [
47+ 'The (void) cast is supported only on PHP 8.5 and later. ' ,
48+ 10 ,
49+ ],
50+ ];
51+ }
52+ $ this ->analyse ([__DIR__ . '/data/void-cast-support.php ' ], $ errors );
53+ }
54+
3755}
Original file line number Diff line number Diff line change 1+ <?php // lint >= 8.5
2+
3+ namespace VoidCastSupport ;
4+
5+ class Foo
6+ {
7+
8+ public function doFoo (): void
9+ {
10+ (void) 1 ;
11+ }
12+
13+ }
You can’t perform that action at this time.
0 commit comments