Skip to content

Commit 2c0dda3

Browse files
committed
Match expression - report usage of void
1 parent 90e49f7 commit 2c0dda3

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

conf/config.level2.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ rules:
1111
- PHPStan\Rules\Cast\InvalidCastRule
1212
- PHPStan\Rules\Cast\InvalidPartOfEncapsedStringRule
1313
- PHPStan\Rules\Cast\PrintRule
14+
- PHPStan\Rules\Comparison\UsageOfVoidMatchExpressionRule
1415
- PHPStan\Rules\Functions\IncompatibleDefaultParameterTypeRule
1516
- PHPStan\Rules\Generics\ClassAncestorsRule
1617
- PHPStan\Rules\Generics\ClassTemplateTypeRule
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Comparison;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Rules\Rule;
8+
use PHPStan\Rules\RuleErrorBuilder;
9+
use PHPStan\Type\VoidType;
10+
11+
/**
12+
* @implements Rule<Node\Expr\Match_>
13+
*/
14+
class UsageOfVoidMatchExpressionRule implements Rule
15+
{
16+
17+
public function getNodeType(): string
18+
{
19+
return Node\Expr\Match_::class;
20+
}
21+
22+
public function processNode(Node $node, Scope $scope): array
23+
{
24+
$matchResultType = $scope->getType($node);
25+
if (
26+
$matchResultType instanceof VoidType
27+
&& !$scope->isInFirstLevelStatement()
28+
) {
29+
return [RuleErrorBuilder::message('Result of match expression (void) is used.')->build()];
30+
}
31+
32+
return [];
33+
}
34+
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Comparison;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
8+
/**
9+
* @extends RuleTestCase<UsageOfVoidMatchExpressionRule>
10+
*/
11+
class UsageOfVoidMatchExpressionRuleTest extends RuleTestCase
12+
{
13+
14+
protected function getRule(): Rule
15+
{
16+
return new UsageOfVoidMatchExpressionRule();
17+
}
18+
19+
public function testRule(): void
20+
{
21+
if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) {
22+
$this->markTestSkipped('Test requires PHP 8.0.');
23+
}
24+
25+
$this->analyse([__DIR__ . '/data/void-match.php'], [
26+
[
27+
'Result of match expression (void) is used.',
28+
21,
29+
],
30+
]);
31+
}
32+
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php // lint >= 8.0
2+
3+
namespace VoidMatch;
4+
5+
class Foo
6+
{
7+
8+
public function doFoo(): void
9+
{
10+
11+
}
12+
13+
public function doBar(int $i): void
14+
{
15+
match ($i) {
16+
1 => $this->doFoo(),
17+
2 => $this->doFoo(),
18+
default => $this->doFoo(),
19+
};
20+
21+
$a = match ($i) {
22+
1 => $this->doFoo(),
23+
2 => $this->doFoo(),
24+
default => $this->doFoo(),
25+
};
26+
}
27+
28+
}

0 commit comments

Comments
 (0)