/
CoalesceRule.php
90 lines (71 loc) · 2.35 KB
/
CoalesceRule.php
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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Variables;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\NullType;
use PHPStan\Type\VerbosityLevel;
/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\BinaryOp\Coalesce>
*/
class CoalesceRule implements \PHPStan\Rules\Rule
{
public function getNodeType(): string
{
return Node\Expr\BinaryOp\Coalesce::class;
}
public function processNode(Node $node, Scope $scope): array
{
$error = $this->canBeCoalesced($node->left, $scope);
if ($error === null) {
return [];
}
return [$error];
}
private function canBeCoalesced(Node $node, Scope $scope): ?RuleError
{
if ($node instanceof Node\Expr\Variable && is_string($node->name)) {
$hasVariable = $scope->hasVariableType($node->name);
if ($hasVariable->no()) {
return RuleErrorBuilder::message(
sprintf('Coalesce of undefined variable $%s.', $node->name)
)->line($node->getLine())->build();
}
$variableType = $scope->getVariableType($node->name);
// For hasVariable->maybe a coalesce is valid
if ($hasVariable->yes() && $variableType->isSuperTypeOf(new NullType())->no()) {
return RuleErrorBuilder::message(
sprintf('Coalesce of variable $%s, which cannot be null.', $node->name)
)->line($node->getLine())->build();
}
} elseif ($node instanceof Node\Expr\ArrayDimFetch && $node->dim !== null) {
$type = $scope->getType($node->var);
$dimType = $scope->getType($node->dim);
$hasOffsetValue = $type->hasOffsetValueType($dimType);
if ($type->isOffsetAccessible()->no() || $hasOffsetValue->no()) {
return RuleErrorBuilder::message(
sprintf(
'Coalesce of invalid offset %s on %s.',
$dimType->describe(VerbosityLevel::value()),
$type->describe(VerbosityLevel::value())
)
)->line($node->getLine())->build();
}
if ($hasOffsetValue->maybe()) {
return null;
}
if ($hasOffsetValue->yes() && $type->isSuperTypeOf(new NullType())->no()) {
return RuleErrorBuilder::message(
sprintf(
'Coalesce of offset %s on %s, which cannot be null.',
$dimType->describe(VerbosityLevel::value()),
$type->describe(VerbosityLevel::value())
)
)->line($node->getLine())->build();
}
return $this->canBeCoalesced($node->var, $scope);
}
return null;
}
}