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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use super::ExpressionAnalyzer;
use crate::context::Context;
use mir_issues::{IssueKind, Severity};
use mir_types::{Atomic, Union};
use php_ast::ast::{CastKind, Expr};
impl<'a> ExpressionAnalyzer<'a> {
pub(super) fn analyze_cast<'arena, 'src>(
&mut self,
kind: &CastKind,
inner: &Expr<'arena, 'src>,
ctx: &mut Context,
) -> Union {
let inner_ty = self.analyze(inner, ctx);
match kind {
CastKind::Int => {
// Check for RedundantCast when already int
if inner_ty.is_single() && inner_ty.contains(|t| t.is_int()) {
self.emit(
IssueKind::RedundantCast {
from: inner_ty.to_string(),
to: "int".to_string(),
},
Severity::Info,
inner.span,
);
}
// Check for InvalidCast from array/object
else if inner_ty.contains(|t| {
matches!(
t,
Atomic::TArray { .. }
| Atomic::TNonEmptyArray { .. }
| Atomic::TList { .. }
| Atomic::TNonEmptyList { .. }
| Atomic::TKeyedArray { .. }
| Atomic::TNamedObject { .. }
| Atomic::TObject
)
}) {
self.emit(
IssueKind::InvalidCast {
from: inner_ty.to_string(),
to: "int".to_string(),
},
Severity::Warning,
inner.span,
);
}
Union::single(Atomic::TInt)
}
CastKind::Float => {
// Check for RedundantCast when already float
if inner_ty.is_single()
&& inner_ty
.contains(|t| matches!(t, Atomic::TFloat | Atomic::TLiteralFloat(..)))
{
self.emit(
IssueKind::RedundantCast {
from: inner_ty.to_string(),
to: "float".to_string(),
},
Severity::Info,
inner.span,
);
}
// Check for InvalidCast from array/object
else if inner_ty.contains(|t| {
matches!(
t,
Atomic::TArray { .. }
| Atomic::TNonEmptyArray { .. }
| Atomic::TList { .. }
| Atomic::TNonEmptyList { .. }
| Atomic::TKeyedArray { .. }
| Atomic::TNamedObject { .. }
| Atomic::TObject
)
}) {
self.emit(
IssueKind::InvalidCast {
from: inner_ty.to_string(),
to: "float".to_string(),
},
Severity::Warning,
inner.span,
);
}
Union::single(Atomic::TFloat)
}
CastKind::String => {
// Check for RedundantCast when already string
if inner_ty.is_single() && inner_ty.contains(|t| t.is_string()) {
self.emit(
IssueKind::RedundantCast {
from: inner_ty.to_string(),
to: "string".to_string(),
},
Severity::Info,
inner.span,
);
}
// Check for InvalidCast from array
else if inner_ty.contains(|t| {
matches!(
t,
Atomic::TArray { .. }
| Atomic::TNonEmptyArray { .. }
| Atomic::TList { .. }
| Atomic::TNonEmptyList { .. }
| Atomic::TKeyedArray { .. }
)
}) {
self.emit(
IssueKind::InvalidCast {
from: inner_ty.to_string(),
to: "string".to_string(),
},
Severity::Warning,
inner.span,
);
}
Union::single(Atomic::TString)
}
CastKind::Bool => {
// Check for RedundantCast when already bool
if inner_ty.is_single()
&& inner_ty
.contains(|t| matches!(t, Atomic::TBool | Atomic::TTrue | Atomic::TFalse))
{
self.emit(
IssueKind::RedundantCast {
from: inner_ty.to_string(),
to: "bool".to_string(),
},
Severity::Info,
inner.span,
);
}
Union::single(Atomic::TBool)
}
CastKind::Array => {
// Check for RedundantCast when already array
if inner_ty.is_single()
&& inner_ty.contains(|t| {
matches!(
t,
Atomic::TArray { .. }
| Atomic::TNonEmptyArray { .. }
| Atomic::TList { .. }
| Atomic::TNonEmptyList { .. }
| Atomic::TKeyedArray { .. }
)
})
{
self.emit(
IssueKind::RedundantCast {
from: inner_ty.to_string(),
to: "array".to_string(),
},
Severity::Info,
inner.span,
);
}
Union::single(Atomic::TArray {
key: Box::new(Union::single(Atomic::TMixed)),
value: Box::new(Union::mixed()),
})
}
CastKind::Object => Union::single(Atomic::TObject),
CastKind::Unset | CastKind::Void => Union::single(Atomic::TNull),
}
}
}