1use std::borrow::Cow;
2use std::hash::BuildHasher;
3use std::hash::Hash;
4
5use foldhash::fast::FixedState;
6
7use mago_word::Word;
8use mago_word::concat_word;
9use mago_word::i64_word;
10use mago_word::usize_word;
11use mago_word::word;
12
13use crate::metadata::CodebaseMetadata;
14use crate::ttype::TType;
15use crate::ttype::atomic::TAtomic;
16use crate::ttype::atomic::array::key::ArrayKey;
17use crate::ttype::template::TemplateResult;
18use crate::ttype::template::inferred_type_replacer;
19use crate::ttype::union::TUnion;
20
21#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
22#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
23pub enum Assertion {
24 Any,
25 IsType(TAtomic),
26 IsNotType(TAtomic),
27 Falsy,
28 Truthy,
29 IsIdentical(TAtomic),
30 IsNotIdentical(TAtomic),
31 IsEqual(TAtomic),
32 IsNotEqual(TAtomic),
33 IsEqualIsset,
34 IsIsset,
35 IsNotIsset,
36 HasStringArrayAccess,
37 HasIntOrStringArrayAccess,
38 ArrayKeyExists,
39 ArrayKeyDoesNotExist,
40 InArray(TUnion),
41 NotInArray(TUnion),
42 HasArrayKey(ArrayKey),
43 DoesNotHaveArrayKey(ArrayKey),
44 HasNonnullEntryForKey(ArrayKey),
45 DoesNotHaveNonnullEntryForKey(ArrayKey),
46 Empty,
47 NonEmpty,
48 NonEmptyCountable(bool),
49 EmptyCountable,
50 HasExactCount(usize),
51 HasAtLeastCount(usize),
52 DoesNotHaveExactCount(usize),
53 DoesNotHasAtLeastCount(usize),
54 IsLessThan(i64),
55 IsLessThanOrEqual(i64),
56 IsGreaterThan(i64),
57 IsGreaterThanOrEqual(i64),
58 IsLessThanFromBound(i64),
64 IsLessThanOrEqualFromBound(i64),
65 IsGreaterThanFromBound(i64),
66 IsGreaterThanOrEqualFromBound(i64),
67 IsLessThanVariable(Word),
73 IsLessThanOrEqualVariable(Word),
74 IsGreaterThanVariable(Word),
75 IsGreaterThanOrEqualVariable(Word),
76 StringLengthLessThan(i64),
77 StringLengthGreaterThanOrEqual(i64),
78 Countable,
79 NotCountable(bool),
80}
81
82impl Assertion {
83 #[must_use]
84 pub fn to_atom(&self) -> Word {
85 match self {
86 Assertion::Any => word("any"),
87 Assertion::Falsy => word("falsy"),
88 Assertion::Truthy => word("truthy"),
89 Assertion::IsEqualIsset => word("=isset"),
90 Assertion::IsIsset => word("isset"),
91 Assertion::IsNotIsset => word("!isset"),
92 Assertion::HasStringArrayAccess => word("=string-array-access"),
93 Assertion::HasIntOrStringArrayAccess => word("=int-or-string-array-access"),
94 Assertion::ArrayKeyExists => word("array-key-exists"),
95 Assertion::ArrayKeyDoesNotExist => word("!array-key-exists"),
96 Assertion::EmptyCountable => word("empty-countable"),
97 Assertion::Empty => word("empty"),
98 Assertion::NonEmpty => word("non-empty"),
99 Assertion::Countable => word("countable"),
100 Assertion::NotCountable(_) => word("!countable"),
101 Assertion::IsType(atomic) => atomic.get_id(),
102 Assertion::IsNotType(atomic) => concat_word!(b"!", atomic.get_id()),
103 Assertion::IsIdentical(atomic) => concat_word!(b"=", atomic.get_id()),
104 Assertion::IsNotIdentical(atomic) => concat_word!(b"!=", atomic.get_id()),
105 Assertion::IsEqual(atomic) => concat_word!(b"~", atomic.get_id()),
106 Assertion::IsNotEqual(atomic) => concat_word!(b"!~", atomic.get_id()),
107 Assertion::InArray(union) => concat_word!(b"=in-array-", union.get_id()),
108 Assertion::NotInArray(union) => concat_word!(b"!=in-array-", union.get_id()),
109 Assertion::HasArrayKey(key) => concat_word!(b"=has-array-key-", key.to_atom()),
110 Assertion::DoesNotHaveArrayKey(key) => concat_word!(b"!=has-array-key-", key.to_atom()),
111 Assertion::HasNonnullEntryForKey(key) => concat_word!(b"=has-nonnull-entry-for-", key.to_atom()),
112 Assertion::DoesNotHaveNonnullEntryForKey(key) => {
113 concat_word!(b"!=has-nonnull-entry-for-", key.to_atom())
114 }
115 Assertion::HasExactCount(number) => concat_word!(b"has-exactly-", usize_word(*number)),
116 Assertion::HasAtLeastCount(number) => concat_word!(b"has-at-least-", usize_word(*number)),
117 Assertion::DoesNotHaveExactCount(number) => concat_word!(b"!has-exactly-", usize_word(*number)),
118 Assertion::DoesNotHasAtLeastCount(number) => concat_word!(b"has-at-most-", usize_word(*number)),
119 Assertion::IsLessThan(number) => concat_word!(b"is-less-than-", i64_word(*number)),
120 Assertion::IsLessThanOrEqual(number) => concat_word!(b"is-less-than-or-equal-", i64_word(*number)),
121 Assertion::IsGreaterThan(number) => concat_word!(b"is-greater-than-", i64_word(*number)),
122 Assertion::IsGreaterThanOrEqual(number) => concat_word!(b"is-greater-than-or-equal-", i64_word(*number)),
123 Assertion::IsLessThanFromBound(number) => concat_word!(b"is-less-than-from-bound-", i64_word(*number)),
124 Assertion::IsLessThanOrEqualFromBound(number) => {
125 concat_word!(b"is-less-than-or-equal-from-bound-", i64_word(*number))
126 }
127 Assertion::IsGreaterThanFromBound(number) => {
128 concat_word!(b"is-greater-than-from-bound-", i64_word(*number))
129 }
130 Assertion::IsGreaterThanOrEqualFromBound(number) => {
131 concat_word!(b"is-greater-than-or-equal-from-bound-", i64_word(*number))
132 }
133 Assertion::IsLessThanVariable(variable) => concat_word!(b"is-less-than-variable-", variable),
134 Assertion::IsLessThanOrEqualVariable(variable) => {
135 concat_word!(b"is-less-than-or-equal-variable-", variable)
136 }
137 Assertion::IsGreaterThanVariable(variable) => {
138 concat_word!(b"is-greater-than-variable-", variable)
139 }
140 Assertion::IsGreaterThanOrEqualVariable(variable) => {
141 concat_word!(b"is-greater-than-or-equal-variable-", variable)
142 }
143 Assertion::StringLengthLessThan(number) => {
144 concat_word!(b"string-length-is-less-than-", i64_word(*number))
145 }
146 Assertion::StringLengthGreaterThanOrEqual(number) => {
147 concat_word!(b"string-length-is-greater-than-or-equal-", i64_word(*number))
148 }
149 Assertion::NonEmptyCountable(negatable) => {
150 if *negatable {
151 word("non-empty-countable")
152 } else {
153 word("=non-empty-countable")
154 }
155 }
156 }
157 }
158
159 #[must_use]
160 pub fn to_hash(&self) -> u64 {
161 FixedState::default().hash_one(self.to_atom())
162 }
163
164 #[must_use]
166 pub fn negated_hash(&self) -> u64 {
167 let atom = match self {
168 Self::IsType(atomic) => concat_word!(b"!", atomic.get_id()),
169 Self::IsNotType(atomic) => atomic.get_id(),
170 Self::IsIdentical(atomic) => concat_word!(b"!=", atomic.get_id()),
171 Self::IsNotIdentical(atomic) => concat_word!(b"=", atomic.get_id()),
172 Self::IsEqual(atomic) => concat_word!(b"!~", atomic.get_id()),
173 Self::IsNotEqual(atomic) => concat_word!(b"~", atomic.get_id()),
174 Self::InArray(union) => concat_word!(b"!=in-array-", union.get_id()),
175 Self::NotInArray(union) => concat_word!(b"=in-array-", union.get_id()),
176 _ => self.get_negation().to_atom(),
177 };
178
179 FixedState::default().hash_one(atom)
180 }
181
182 #[must_use]
183 pub fn is_negation(&self) -> bool {
184 matches!(
185 self,
186 Assertion::Falsy
187 | Assertion::IsNotType(_)
188 | Assertion::IsNotEqual(_)
189 | Assertion::IsNotIdentical(_)
190 | Assertion::IsNotIsset
191 | Assertion::NotInArray(..)
192 | Assertion::ArrayKeyDoesNotExist
193 | Assertion::DoesNotHaveArrayKey(_)
194 | Assertion::DoesNotHaveExactCount(_)
195 | Assertion::DoesNotHaveNonnullEntryForKey(_)
196 | Assertion::DoesNotHasAtLeastCount(_)
197 | Assertion::EmptyCountable
198 | Assertion::Empty
199 | Assertion::NotCountable(_)
200 )
201 }
202
203 #[must_use]
204 pub fn has_isset(&self) -> bool {
205 matches!(
206 self,
207 Assertion::IsIsset | Assertion::ArrayKeyExists | Assertion::HasStringArrayAccess | Assertion::IsEqualIsset
208 )
209 }
210
211 #[must_use]
212 pub fn has_equality(&self) -> bool {
213 matches!(
214 self,
215 Assertion::InArray(_)
216 | Assertion::HasIntOrStringArrayAccess
217 | Assertion::HasStringArrayAccess
218 | Assertion::IsEqualIsset
219 | Assertion::IsIdentical(_)
220 | Assertion::IsNotIdentical(_)
221 | Assertion::IsEqual(_)
222 | Assertion::IsNotEqual(_)
223 | Assertion::HasExactCount(_)
224 )
225 }
226
227 #[must_use]
228 pub fn has_literal_value(&self) -> bool {
229 self.get_type().is_some_and(|atomic| {
230 atomic.is_literal_int()
231 || atomic.is_literal_float()
232 || atomic.is_known_literal_string()
233 || atomic.is_literal_class_string()
234 })
235 }
236
237 #[must_use]
238 pub fn with_type(&self, atomic: TAtomic) -> Self {
239 match self {
240 Assertion::IsType(_) => Assertion::IsType(atomic),
241 Assertion::IsNotType(_) => Assertion::IsNotType(atomic),
242 Assertion::IsIdentical(_) => Assertion::IsIdentical(atomic),
243 Assertion::IsNotIdentical(_) => Assertion::IsNotIdentical(atomic),
244 Assertion::IsEqual(_) => Assertion::IsEqual(atomic),
245 Assertion::IsNotEqual(_) => Assertion::IsNotEqual(atomic),
246 _ => self.clone(),
247 }
248 }
249
250 #[must_use]
251 pub fn get_type(&self) -> Option<&TAtomic> {
252 match self {
253 Assertion::IsIdentical(atomic)
254 | Assertion::IsNotIdentical(atomic)
255 | Assertion::IsType(atomic)
256 | Assertion::IsNotType(atomic)
257 | Assertion::IsEqual(atomic)
258 | Assertion::IsNotEqual(atomic) => Some(atomic),
259 _ => None,
260 }
261 }
262
263 pub fn get_type_mut(&mut self) -> Option<&mut TAtomic> {
264 match self {
265 Assertion::IsIdentical(atomic)
266 | Assertion::IsNotIdentical(atomic)
267 | Assertion::IsType(atomic)
268 | Assertion::IsNotType(atomic)
269 | Assertion::IsEqual(atomic)
270 | Assertion::IsNotEqual(atomic) => Some(atomic),
271 _ => None,
272 }
273 }
274
275 #[must_use]
276 pub fn resolve_templates(&self, codebase: &CodebaseMetadata, template_result: &TemplateResult) -> Vec<Self> {
277 match self {
278 Assertion::IsType(atomic) => {
279 let union = TUnion::from_single(Cow::Owned(atomic.clone()));
280 let resolved_union = inferred_type_replacer::replace(&union, template_result, codebase);
281
282 let mut result = vec![];
283 for resolved_atomic in resolved_union.types.into_owned() {
284 result.push(Assertion::IsType(resolved_atomic));
285 }
286
287 if result.is_empty() {
288 result.push(Assertion::IsType(TAtomic::Never));
289 }
290
291 result
292 }
293 Assertion::IsNotType(atomic) => {
294 let union = TUnion::from_single(Cow::Owned(atomic.clone()));
295 let resolved_union = inferred_type_replacer::replace(&union, template_result, codebase);
296
297 let mut result = vec![];
298 for resolved_atomic in resolved_union.types.into_owned() {
299 result.push(Assertion::IsNotType(resolved_atomic));
300 }
301
302 if result.is_empty() {
303 result.push(Assertion::IsNotType(TAtomic::Never));
304 }
305
306 result
307 }
308 Assertion::InArray(union) => {
309 let resolved_union = inferred_type_replacer::replace(union, template_result, codebase);
310
311 vec![Assertion::InArray(resolved_union)]
312 }
313 Assertion::NotInArray(union) => {
314 let resolved_union = inferred_type_replacer::replace(union, template_result, codebase);
315
316 vec![Assertion::NotInArray(resolved_union)]
317 }
318 _ => {
319 vec![self.clone()]
320 }
321 }
322 }
323
324 #[must_use]
325 pub fn is_negation_of(&self, other: &Assertion) -> bool {
326 match self {
327 Assertion::Any => false,
328 Assertion::Falsy => matches!(other, Assertion::Truthy),
329 Assertion::Truthy => matches!(other, Assertion::Falsy),
330 Assertion::IsType(atomic) => match other {
331 Assertion::IsNotType(other_atomic) => other_atomic == atomic,
332 _ => false,
333 },
334 Assertion::IsNotType(atomic) => match other {
335 Assertion::IsType(other_atomic) => other_atomic == atomic,
336 _ => false,
337 },
338 Assertion::IsIdentical(atomic) => match other {
339 Assertion::IsNotIdentical(other_atomic) => other_atomic == atomic,
340 _ => false,
341 },
342 Assertion::IsNotIdentical(atomic) => match other {
343 Assertion::IsIdentical(other_atomic) => other_atomic == atomic,
344 _ => false,
345 },
346 Assertion::IsEqual(atomic) => match other {
347 Assertion::IsNotEqual(other_atomic) => other_atomic == atomic,
348 _ => false,
349 },
350 Assertion::IsNotEqual(atomic) => match other {
351 Assertion::IsEqual(other_atomic) => other_atomic == atomic,
352 _ => false,
353 },
354 Assertion::IsEqualIsset => false,
355 Assertion::IsIsset => matches!(other, Assertion::IsNotIsset),
356 Assertion::IsNotIsset => matches!(other, Assertion::IsIsset),
357 Assertion::HasStringArrayAccess => false,
358 Assertion::HasIntOrStringArrayAccess => false,
359 Assertion::ArrayKeyExists => matches!(other, Assertion::ArrayKeyDoesNotExist),
360 Assertion::ArrayKeyDoesNotExist => matches!(other, Assertion::ArrayKeyExists),
361 Assertion::HasArrayKey(str) => match other {
362 Assertion::DoesNotHaveArrayKey(other_str) => other_str == str,
363 _ => false,
364 },
365 Assertion::DoesNotHaveArrayKey(str) => match other {
366 Assertion::HasArrayKey(other_str) => other_str == str,
367 _ => false,
368 },
369 Assertion::HasNonnullEntryForKey(str) => match other {
370 Assertion::DoesNotHaveNonnullEntryForKey(other_str) => other_str == str,
371 _ => false,
372 },
373 Assertion::DoesNotHaveNonnullEntryForKey(str) => match other {
374 Assertion::HasNonnullEntryForKey(other_str) => other_str == str,
375 _ => false,
376 },
377 Assertion::InArray(union) => match other {
378 Assertion::NotInArray(other_union) => other_union == union,
379 _ => false,
380 },
381 Assertion::NotInArray(union) => match other {
382 Assertion::InArray(other_union) => other_union == union,
383 _ => false,
384 },
385 Assertion::Empty => matches!(other, Assertion::NonEmpty),
386 Assertion::NonEmpty => matches!(other, Assertion::Empty),
387 Assertion::NonEmptyCountable(negatable) => {
388 if *negatable {
389 matches!(other, Assertion::EmptyCountable)
390 } else {
391 false
392 }
393 }
394 Assertion::EmptyCountable => matches!(other, Assertion::NonEmptyCountable(true)),
395 Assertion::HasExactCount(number) => match other {
396 Assertion::DoesNotHaveExactCount(other_number) => other_number == number,
397 _ => false,
398 },
399 Assertion::DoesNotHaveExactCount(number) => match other {
400 Assertion::HasExactCount(other_number) => other_number == number,
401 _ => false,
402 },
403 Assertion::HasAtLeastCount(number) => match other {
404 Assertion::DoesNotHasAtLeastCount(other_number) => other_number == number,
405 _ => false,
406 },
407 Assertion::DoesNotHasAtLeastCount(number) => match other {
408 Assertion::HasAtLeastCount(other_number) => other_number == number,
409 _ => false,
410 },
411 Assertion::IsLessThan(number) => match other {
412 Assertion::IsGreaterThanOrEqual(other_number) => other_number == number,
413 _ => false,
414 },
415 Assertion::IsLessThanOrEqual(number) => match other {
416 Assertion::IsGreaterThan(other_number) => other_number == number,
417 _ => false,
418 },
419 Assertion::IsGreaterThan(number) => match other {
420 Assertion::IsLessThanOrEqual(other_number) => other_number == number,
421 _ => false,
422 },
423 Assertion::IsGreaterThanOrEqual(number) => match other {
424 Assertion::IsLessThan(other_number) => other_number == number,
425 _ => false,
426 },
427 Assertion::IsLessThanFromBound(_)
428 | Assertion::IsLessThanOrEqualFromBound(_)
429 | Assertion::IsGreaterThanFromBound(_)
430 | Assertion::IsGreaterThanOrEqualFromBound(_) => false,
431 Assertion::IsLessThanVariable(variable) => match other {
432 Assertion::IsGreaterThanOrEqualVariable(other_variable) => other_variable == variable,
433 _ => false,
434 },
435 Assertion::IsLessThanOrEqualVariable(variable) => match other {
436 Assertion::IsGreaterThanVariable(other_variable) => other_variable == variable,
437 _ => false,
438 },
439 Assertion::IsGreaterThanVariable(variable) => match other {
440 Assertion::IsLessThanOrEqualVariable(other_variable) => other_variable == variable,
441 _ => false,
442 },
443 Assertion::IsGreaterThanOrEqualVariable(variable) => match other {
444 Assertion::IsLessThanVariable(other_variable) => other_variable == variable,
445 _ => false,
446 },
447 Assertion::StringLengthLessThan(number) => match other {
448 Assertion::StringLengthGreaterThanOrEqual(other_number) => other_number == number,
449 _ => false,
450 },
451 Assertion::StringLengthGreaterThanOrEqual(number) => match other {
452 Assertion::StringLengthLessThan(other_number) => other_number == number,
453 _ => false,
454 },
455 Assertion::Countable => matches!(other, Assertion::NotCountable(negatable) if *negatable),
456 Assertion::NotCountable(_) => matches!(other, Assertion::Countable),
457 }
458 }
459
460 #[must_use]
461 pub fn get_negation(&self) -> Self {
462 match self {
463 Assertion::Any => Assertion::Any,
464 Assertion::Falsy => Assertion::Truthy,
465 Assertion::IsType(atomic) => Assertion::IsNotType(atomic.clone()),
466 Assertion::IsNotType(atomic) => Assertion::IsType(atomic.clone()),
467 Assertion::Truthy => Assertion::Falsy,
468 Assertion::IsIdentical(atomic) => Assertion::IsNotIdentical(atomic.clone()),
469 Assertion::IsNotIdentical(atomic) => Assertion::IsIdentical(atomic.clone()),
470 Assertion::IsEqual(atomic) => Assertion::IsNotEqual(atomic.clone()),
471 Assertion::IsNotEqual(atomic) => Assertion::IsEqual(atomic.clone()),
472 Assertion::IsIsset => Assertion::IsNotIsset,
473 Assertion::IsNotIsset => Assertion::IsIsset,
474 Assertion::Empty => Assertion::NonEmpty,
475 Assertion::NonEmpty => Assertion::Empty,
476 Assertion::NonEmptyCountable(negatable) => {
477 if *negatable {
478 Assertion::EmptyCountable
479 } else {
480 Assertion::Any
481 }
482 }
483 Assertion::EmptyCountable => Assertion::NonEmptyCountable(true),
484 Assertion::ArrayKeyExists => Assertion::ArrayKeyDoesNotExist,
485 Assertion::ArrayKeyDoesNotExist => Assertion::ArrayKeyExists,
486 Assertion::InArray(union) => Assertion::NotInArray(union.clone()),
487 Assertion::NotInArray(union) => Assertion::InArray(union.clone()),
488 Assertion::HasExactCount(size) => Assertion::DoesNotHaveExactCount(*size),
489 Assertion::DoesNotHaveExactCount(size) => Assertion::HasExactCount(*size),
490 Assertion::HasAtLeastCount(size) => Assertion::DoesNotHasAtLeastCount(*size),
491 Assertion::DoesNotHasAtLeastCount(size) => Assertion::HasAtLeastCount(*size),
492 Assertion::HasArrayKey(str) => Assertion::DoesNotHaveArrayKey(*str),
493 Assertion::DoesNotHaveArrayKey(str) => Assertion::HasArrayKey(*str),
494 Assertion::HasNonnullEntryForKey(str) => Assertion::DoesNotHaveNonnullEntryForKey(*str),
495 Assertion::DoesNotHaveNonnullEntryForKey(str) => Assertion::HasNonnullEntryForKey(*str),
496 Assertion::HasStringArrayAccess => Assertion::Any,
497 Assertion::HasIntOrStringArrayAccess => Assertion::Any,
498 Assertion::IsEqualIsset => Assertion::Any,
499 Assertion::IsLessThan(number) => Assertion::IsGreaterThanOrEqual(*number),
500 Assertion::IsLessThanOrEqual(number) => Assertion::IsGreaterThan(*number),
501 Assertion::IsGreaterThan(number) => Assertion::IsLessThanOrEqual(*number),
502 Assertion::IsGreaterThanOrEqual(number) => Assertion::IsLessThan(*number),
503 Assertion::IsLessThanFromBound(_)
504 | Assertion::IsLessThanOrEqualFromBound(_)
505 | Assertion::IsGreaterThanFromBound(_)
506 | Assertion::IsGreaterThanOrEqualFromBound(_) => Assertion::Any,
507 Assertion::IsLessThanVariable(variable) => Assertion::IsGreaterThanOrEqualVariable(*variable),
508 Assertion::IsLessThanOrEqualVariable(variable) => Assertion::IsGreaterThanVariable(*variable),
509 Assertion::IsGreaterThanVariable(variable) => Assertion::IsLessThanOrEqualVariable(*variable),
510 Assertion::IsGreaterThanOrEqualVariable(variable) => Assertion::IsLessThanVariable(*variable),
511 Assertion::StringLengthLessThan(number) => Assertion::StringLengthGreaterThanOrEqual(*number),
512 Assertion::StringLengthGreaterThanOrEqual(number) => Assertion::StringLengthLessThan(*number),
513 Assertion::Countable => Assertion::NotCountable(true),
514 Assertion::NotCountable(_) => Assertion::Countable,
515 }
516 }
517
518 #[inline]
525 #[must_use]
526 pub const fn is_negatable(&self) -> bool {
527 !matches!(
528 self,
529 Self::IsLessThanFromBound(_)
530 | Self::IsLessThanOrEqualFromBound(_)
531 | Self::IsGreaterThanFromBound(_)
532 | Self::IsGreaterThanOrEqualFromBound(_)
533 )
534 }
535
536 #[inline]
538 #[must_use]
539 pub const fn referenced_variable(&self) -> Option<Word> {
540 match self {
541 Self::IsLessThanVariable(variable)
542 | Self::IsLessThanOrEqualVariable(variable)
543 | Self::IsGreaterThanVariable(variable)
544 | Self::IsGreaterThanOrEqualVariable(variable) => Some(*variable),
545 _ => None,
546 }
547 }
548}