forked from vimeo/psalm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmptyAnalyzer.php
80 lines (69 loc) · 2.56 KB
/
EmptyAnalyzer.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
<?php
declare(strict_types=1);
namespace Psalm\Internal\Analyzer\Statements\Expression;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Issue\ForbiddenCode;
use Psalm\Issue\InvalidArgument;
use Psalm\IssueBuffer;
use Psalm\Type;
use Psalm\Type\Atomic\TBool;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TTrue;
use Psalm\Type\Union;
/**
* @internal
*/
final class EmptyAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\Empty_ $stmt,
Context $context,
): void {
IssetAnalyzer::analyzeIssetVar($statements_analyzer, $stmt->expr, $context);
$codebase = $statements_analyzer->getCodebase();
if (isset($codebase->config->forbidden_functions['empty'])) {
IssueBuffer::maybeAdd(
new ForbiddenCode(
'You have forbidden the use of empty',
new CodeLocation($statements_analyzer->getSource(), $stmt),
),
$statements_analyzer->getSuppressedIssues(),
);
}
$expr_type = $statements_analyzer->node_data->getType($stmt->expr);
if ($expr_type) {
if ($expr_type->hasBool()
&& $expr_type->isSingle()
&& !$expr_type->from_docblock
) {
IssueBuffer::maybeAdd(
new InvalidArgument(
'Calling empty on a boolean value is almost certainly unintended',
new CodeLocation($statements_analyzer->getSource(), $stmt->expr),
'empty',
),
$statements_analyzer->getSuppressedIssues(),
);
}
if ($expr_type->isAlwaysTruthy() && $expr_type->possibly_undefined === false) {
$stmt_type = new TFalse($expr_type->from_docblock);
} elseif ($expr_type->isAlwaysFalsy()) {
$stmt_type = new TTrue($expr_type->from_docblock);
} else {
ExpressionAnalyzer::checkRiskyTruthyFalsyComparison($expr_type, $statements_analyzer, $stmt);
$stmt_type = new TBool();
}
$stmt_type = new Union([$stmt_type], [
'parent_nodes' => $expr_type->parent_nodes,
]);
} else {
$stmt_type = Type::getBool();
}
$statements_analyzer->node_data->setType($stmt, $stmt_type);
}
}