Skip to content

Commit 53b87e0

Browse files
committed
The (void) cast is supported only on PHP 8.5 and later.
1 parent e5de6b8 commit 53b87e0

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

src/Php/PhpVersion.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

src/Rules/Cast/VoidCastRule.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\RegisteredRule;
8+
use PHPStan\Php\PhpVersion;
89
use PHPStan\Rules\Rule;
910
use PHPStan\Rules\RuleErrorBuilder;
1011

@@ -15,7 +16,9 @@
1516
final 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
}

tests/PHPStan/Rules/Cast/VoidCastRuleTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace PHPStan\Rules\Cast;
44

5+
use PHPStan\Php\PhpVersion;
56
use PHPStan\Rules\Rule;
67
use 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
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

0 commit comments

Comments
 (0)