From 2c0dda3212bdd63df57d146822aa07ff0d412840 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Mon, 29 Mar 2021 17:43:49 +0200 Subject: [PATCH] Match expression - report usage of void --- conf/config.level2.neon | 1 + .../UsageOfVoidMatchExpressionRule.php | 35 +++++++++++++++++++ .../UsageOfVoidMatchExpressionRuleTest.php | 33 +++++++++++++++++ .../Rules/Comparison/data/void-match.php | 28 +++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 src/Rules/Comparison/UsageOfVoidMatchExpressionRule.php create mode 100644 tests/PHPStan/Rules/Comparison/UsageOfVoidMatchExpressionRuleTest.php create mode 100644 tests/PHPStan/Rules/Comparison/data/void-match.php diff --git a/conf/config.level2.neon b/conf/config.level2.neon index bb27e9271e..c284232e28 100644 --- a/conf/config.level2.neon +++ b/conf/config.level2.neon @@ -11,6 +11,7 @@ rules: - PHPStan\Rules\Cast\InvalidCastRule - PHPStan\Rules\Cast\InvalidPartOfEncapsedStringRule - PHPStan\Rules\Cast\PrintRule + - PHPStan\Rules\Comparison\UsageOfVoidMatchExpressionRule - PHPStan\Rules\Functions\IncompatibleDefaultParameterTypeRule - PHPStan\Rules\Generics\ClassAncestorsRule - PHPStan\Rules\Generics\ClassTemplateTypeRule diff --git a/src/Rules/Comparison/UsageOfVoidMatchExpressionRule.php b/src/Rules/Comparison/UsageOfVoidMatchExpressionRule.php new file mode 100644 index 0000000000..8494d0c2b2 --- /dev/null +++ b/src/Rules/Comparison/UsageOfVoidMatchExpressionRule.php @@ -0,0 +1,35 @@ + + */ +class UsageOfVoidMatchExpressionRule implements Rule +{ + + public function getNodeType(): string + { + return Node\Expr\Match_::class; + } + + public function processNode(Node $node, Scope $scope): array + { + $matchResultType = $scope->getType($node); + if ( + $matchResultType instanceof VoidType + && !$scope->isInFirstLevelStatement() + ) { + return [RuleErrorBuilder::message('Result of match expression (void) is used.')->build()]; + } + + return []; + } + +} diff --git a/tests/PHPStan/Rules/Comparison/UsageOfVoidMatchExpressionRuleTest.php b/tests/PHPStan/Rules/Comparison/UsageOfVoidMatchExpressionRuleTest.php new file mode 100644 index 0000000000..bf873a965c --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/UsageOfVoidMatchExpressionRuleTest.php @@ -0,0 +1,33 @@ + + */ +class UsageOfVoidMatchExpressionRuleTest extends RuleTestCase +{ + + protected function getRule(): Rule + { + return new UsageOfVoidMatchExpressionRule(); + } + + public function testRule(): void + { + if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) { + $this->markTestSkipped('Test requires PHP 8.0.'); + } + + $this->analyse([__DIR__ . '/data/void-match.php'], [ + [ + 'Result of match expression (void) is used.', + 21, + ], + ]); + } + +} diff --git a/tests/PHPStan/Rules/Comparison/data/void-match.php b/tests/PHPStan/Rules/Comparison/data/void-match.php new file mode 100644 index 0000000000..f2cfbb22ff --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/void-match.php @@ -0,0 +1,28 @@ += 8.0 + +namespace VoidMatch; + +class Foo +{ + + public function doFoo(): void + { + + } + + public function doBar(int $i): void + { + match ($i) { + 1 => $this->doFoo(), + 2 => $this->doFoo(), + default => $this->doFoo(), + }; + + $a = match ($i) { + 1 => $this->doFoo(), + 2 => $this->doFoo(), + default => $this->doFoo(), + }; + } + +}