forked from vimeo/psalm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBooleanNotAnalyzer.php
60 lines (46 loc) · 1.63 KB
/
BooleanNotAnalyzer.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
<?php
declare(strict_types=1);
namespace Psalm\Internal\Analyzer\Statements\Expression;
use PhpParser;
use Psalm\Context;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
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 BooleanNotAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\BooleanNot $stmt,
Context $context,
): bool {
$inside_negation = $context->inside_negation;
$context->inside_negation = !$inside_negation;
$result = ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context);
$context->inside_negation = $inside_negation;
$expr_type = $statements_analyzer->node_data->getType($stmt->expr);
if ($expr_type) {
if ($expr_type->isAlwaysTruthy()) {
$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);
return $result;
}
}