-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathMatchExpressionRule.php
More file actions
131 lines (115 loc) · 3.85 KB
/
MatchExpressionRule.php
File metadata and controls
131 lines (115 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Comparison;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\MatchExpressionNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use UnhandledMatchError;
use function array_map;
use function count;
use function sprintf;
/**
* @implements Rule<MatchExpressionNode>
*/
class MatchExpressionRule implements Rule
{
public function __construct(private bool $checkAlwaysTrueStrictComparison)
{
}
public function getNodeType(): string
{
return MatchExpressionNode::class;
}
public function processNode(Node $node, Scope $scope): array
{
$matchCondition = $node->getCondition();
$nextArmIsDead = false;
$errors = [];
$armsCount = count($node->getArms());
$hasDefault = false;
foreach ($node->getArms() as $i => $arm) {
if ($nextArmIsDead) {
$errors[] = RuleErrorBuilder::message('Match arm is unreachable because previous comparison is always true.')->line($arm->getLine())->build();
continue;
}
$armConditions = $arm->getConditions();
if (count($armConditions) === 0) {
$hasDefault = true;
}
foreach ($armConditions as $armCondition) {
$armConditionScope = $armCondition->getScope();
$armConditionExpr = new Node\Expr\BinaryOp\Identical(
$matchCondition,
$armCondition->getCondition(),
);
$armConditionResult = $armConditionScope->getType($armConditionExpr);
if (!$armConditionResult instanceof ConstantBooleanType) {
continue;
}
$armLine = $armCondition->getLine();
if (!$armConditionResult->getValue()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Match arm comparison between %s and %s is always false.',
$armConditionScope->getType($matchCondition)->describe(VerbosityLevel::value()),
$armConditionScope->getType($armCondition->getCondition())->describe(VerbosityLevel::value()),
))->line($armLine)->build();
} else {
$nextArmIsDead = true;
if (
$this->checkAlwaysTrueStrictComparison
&& ($i !== $armsCount - 1 || $i === 0)
) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Match arm comparison between %s and %s is always true.',
$armConditionScope->getType($matchCondition)->describe(VerbosityLevel::value()),
$armConditionScope->getType($armCondition->getCondition())->describe(VerbosityLevel::value()),
))->line($armLine)->build();
}
}
}
}
if (!$hasDefault && !$nextArmIsDead) {
$remainingType = $node->getEndScope()->getType($matchCondition);
if (
!$remainingType instanceof NeverType
&& !$this->isUnhandledMatchErrorCaught($node)
&& !$this->hasUnhandledMatchErrorThrowsTag($scope)
) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Match expression does not handle remaining %s: %s',
$remainingType instanceof UnionType ? 'values' : 'value',
$remainingType->describe(VerbosityLevel::value()),
))->build();
}
}
return $errors;
}
private function isUnhandledMatchErrorCaught(Node $node): bool
{
$tryCatchTypes = $node->getAttribute('tryCatchTypes');
if ($tryCatchTypes === null) {
return false;
}
$tryCatchType = TypeCombinator::union(...array_map(static fn (string $class) => new ObjectType($class), $tryCatchTypes));
return $tryCatchType->isSuperTypeOf(new ObjectType(UnhandledMatchError::class))->yes();
}
private function hasUnhandledMatchErrorThrowsTag(Scope $scope): bool
{
$function = $scope->getFunction();
if ($function === null) {
return false;
}
$throwsType = $function->getThrowType();
if ($throwsType === null) {
return false;
}
return $throwsType->isSuperTypeOf(new ObjectType(UnhandledMatchError::class))->yes();
}
}