-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Match expression - report usage of void
- Loading branch information
1 parent
90e49f7
commit 2c0dda3
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Comparison; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\VoidType; | ||
|
||
/** | ||
* @implements Rule<Node\Expr\Match_> | ||
*/ | ||
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 []; | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
tests/PHPStan/Rules/Comparison/UsageOfVoidMatchExpressionRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Comparison; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<UsageOfVoidMatchExpressionRule> | ||
*/ | ||
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, | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php // lint >= 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(), | ||
}; | ||
} | ||
|
||
} |