-
Notifications
You must be signed in to change notification settings - Fork 570
Expand file tree
/
Copy pathEmptyRule.php
More file actions
71 lines (57 loc) · 1.39 KB
/
EmptyRule.php
File metadata and controls
71 lines (57 loc) · 1.39 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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Variables;
use PhpParser\Node;
use PHPStan\Analyser\CollectedDataEmitter;
use PHPStan\Analyser\NodeCallbackInvoker;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Rules\IssetCheck;
use PHPStan\Rules\Rule;
use PHPStan\Type\Type;
/**
* @implements Rule<Node\Expr\Empty_>
*/
#[RegisteredRule(level: 1)]
final class EmptyRule implements Rule
{
public function __construct(private IssetCheck $issetCheck)
{
}
public function getNodeType(): string
{
return Node\Expr\Empty_::class;
}
public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataEmitter $scope): array
{
$error = $this->issetCheck->check($node->expr, $scope, 'in empty()', 'empty', static function (Type $type): ?string {
$isNull = $type->isNull();
if ($isNull->maybe()) {
return null;
}
$isFalsey = $type->toBoolean()->isFalse();
if ($isFalsey->maybe()) {
return null;
}
if ($isNull->yes()) {
if ($isFalsey->yes()) {
return 'is always falsy';
}
if ($isFalsey->no()) {
return 'is not falsy';
}
return 'is always null';
}
if ($isFalsey->yes()) {
return 'is always falsy';
}
if ($isFalsey->no()) {
return 'is not falsy';
}
return 'is not nullable';
}, self::class);
if ($error === null) {
return [];
}
return [$error];
}
}