-
Notifications
You must be signed in to change notification settings - Fork 653
/
NonComparisonOpAnalyzer.php
134 lines (114 loc) · 4.05 KB
/
NonComparisonOpAnalyzer.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace Psalm\Internal\Analyzer\Statements\Expression\BinaryOp;
use PhpParser;
use Psalm\Context;
use Psalm\Internal\Analyzer\Statements\Expression\BinaryOpAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Type;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Union;
/**
* @internal
*/
class NonComparisonOpAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\BinaryOp $stmt,
Context $context
): void {
$stmt_left_type = $statements_analyzer->node_data->getType($stmt->left);
$stmt_right_type = $statements_analyzer->node_data->getType($stmt->right);
if (!$stmt_left_type || !$stmt_right_type) {
return;
}
if (($stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseXor
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd
)
&& $stmt_left_type->hasString()
&& $stmt_right_type->hasString()
) {
$stmt_type = Type::getString();
$statements_analyzer->node_data->setType($stmt, $stmt_type);
BinaryOpAnalyzer::addDataFlow(
$statements_analyzer,
$stmt,
$stmt->left,
$stmt->right,
'nondivop'
);
return;
}
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Plus
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Minus
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Mod
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Mul
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Pow
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseXor
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\ShiftLeft
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\ShiftRight
) {
ArithmeticOpAnalyzer::analyze(
$statements_analyzer,
$statements_analyzer->node_data,
$stmt->left,
$stmt->right,
$stmt,
$result_type,
$context
);
if (!$result_type) {
$result_type = new Union([new TInt(), new TFloat()]);
}
$statements_analyzer->node_data->setType($stmt, $result_type);
BinaryOpAnalyzer::addDataFlow(
$statements_analyzer,
$stmt,
$stmt->left,
$stmt->right,
'nondivop'
);
return;
}
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalXor) {
if ($stmt_left_type->hasBool() || $stmt_right_type->hasBool()) {
$statements_analyzer->node_data->setType($stmt, Type::getBool());
}
BinaryOpAnalyzer::addDataFlow(
$statements_analyzer,
$stmt,
$stmt->left,
$stmt->right,
'xor'
);
return;
}
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Div) {
ArithmeticOpAnalyzer::analyze(
$statements_analyzer,
$statements_analyzer->node_data,
$stmt->left,
$stmt->right,
$stmt,
$result_type,
$context
);
if (!$result_type) {
$result_type = new Union([new TInt(), new TFloat()]);
}
$statements_analyzer->node_data->setType($stmt, $result_type);
BinaryOpAnalyzer::addDataFlow(
$statements_analyzer,
$stmt,
$stmt->left,
$stmt->right,
'div'
);
return;
}
}
}