forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InvalidNullCoalesce.php
49 lines (44 loc) · 1.23 KB
/
InvalidNullCoalesce.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
<?php declare(strict_types=1);
namespace PHPStan\Rules\Comparison;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
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 InvalidNullCoalesce implements Rule
{
/**
* @phpstan-return class-string<\PhpParser\Node\Expr\BinaryOp\Coalesce>
* @return string
*/
public function getNodeType(): string
{
return Node\Expr\BinaryOp\Coalesce::class;
}
/**
* @phpstan-param \PhpParser\Node\Expr\BinaryOp\Coalesce $node
* @param \PhpParser\Node $node
* @param \PHPStan\Analyser\Scope $scope
* @return RuleError[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
$type = $scope->getType($node->left);
$onlyNull = $type instanceof NullType;
if($onlyNull || $type->accepts(new NullType(), $scope->isDeclareStrictTypes())->no()) {
return [
RuleErrorBuilder::message(sprintf(
'Null coalesce on type %s is always %s.',
$type->describe(VerbosityLevel::value()),
$onlyNull ? 'true' : 'false'
))->line($node->left->getLine())->build(),
];
}
return [];
}
}