1use std::borrow::Cow;
2use std::hash::Hash;
3use std::hash::Hasher;
4use std::sync::Arc;
5
6use serde::Deserialize;
7use serde::Serialize;
8
9use mago_atom::Atom;
10use mago_atom::atom;
11use mago_atom::concat_atom;
12use mago_atom::empty_atom;
13
14use crate::metadata::CodebaseMetadata;
15use crate::reference::ReferenceSource;
16use crate::reference::SymbolReferences;
17use crate::symbol::Symbols;
18use crate::ttype::TType;
19use crate::ttype::TypeRef;
20use crate::ttype::atomic::TAtomic;
21use crate::ttype::atomic::array::TArray;
22use crate::ttype::atomic::array::key::ArrayKey;
23use crate::ttype::atomic::generic::TGenericParameter;
24use crate::ttype::atomic::mixed::truthiness::TMixedTruthiness;
25use crate::ttype::atomic::object::TObject;
26use crate::ttype::atomic::object::named::TNamedObject;
27use crate::ttype::atomic::object::with_properties::TObjectWithProperties;
28use crate::ttype::atomic::populate_atomic_type;
29use crate::ttype::atomic::scalar::TScalar;
30use crate::ttype::atomic::scalar::bool::TBool;
31use crate::ttype::atomic::scalar::class_like_string::TClassLikeString;
32use crate::ttype::atomic::scalar::int::TInteger;
33use crate::ttype::atomic::scalar::string::TString;
34use crate::ttype::atomic::scalar::string::TStringLiteral;
35use crate::ttype::flags::UnionFlags;
36use crate::ttype::get_arraykey;
37use crate::ttype::get_int;
38use crate::ttype::get_mixed;
39
40#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialOrd, Ord)]
41pub struct TUnion {
42 pub types: Cow<'static, [TAtomic]>,
43 pub flags: UnionFlags,
44}
45
46impl Hash for TUnion {
47 fn hash<H: Hasher>(&self, state: &mut H) {
48 for t in self.types.as_ref() {
49 t.hash(state);
50 }
51 }
52}
53
54impl TUnion {
55 #[must_use]
60 pub fn new(types: Cow<'static, [TAtomic]>) -> TUnion {
61 TUnion { types, flags: UnionFlags::empty() }
62 }
63
64 #[must_use]
75 pub fn from_vec(mut types: Vec<TAtomic>) -> TUnion {
76 if cfg!(debug_assertions) {
77 assert!(
78 !types.is_empty(),
79 "TUnion::from_vec() received an empty Vec. This indicates a logic error \
80 in type construction - unions must contain at least one type. \
81 Consider using TAtomic::Never for empty/impossible types."
82 );
83 }
84
85 if types.len() > 1 {
88 types.retain(|atomic| {
89 !atomic.is_never() && !atomic.map_generic_parameter_constraint(TUnion::is_never).unwrap_or(false)
90 });
91 }
92
93 if types.is_empty() {
96 types.push(TAtomic::Never);
97 }
98
99 Self::new(Cow::Owned(types))
100 }
101
102 #[must_use]
108 pub fn from_single(atomic: Cow<'static, TAtomic>) -> TUnion {
109 let types_cow = match atomic {
110 Cow::Borrowed(borrowed_atomic) => Cow::Borrowed(std::slice::from_ref(borrowed_atomic)),
111 Cow::Owned(owned_atomic) => Cow::Owned(vec![owned_atomic]),
112 };
113
114 TUnion::new(types_cow)
115 }
116
117 #[must_use]
119 pub fn from_atomic(atomic: TAtomic) -> TUnion {
120 TUnion::new(Cow::Owned(vec![atomic]))
121 }
122
123 #[inline]
124 pub fn set_possibly_undefined(&mut self, possibly_undefined: bool, from_try: Option<bool>) {
125 let from_try = from_try.unwrap_or(self.flags.contains(UnionFlags::POSSIBLY_UNDEFINED_FROM_TRY));
126
127 self.flags.set(UnionFlags::POSSIBLY_UNDEFINED, possibly_undefined);
128 self.flags.set(UnionFlags::POSSIBLY_UNDEFINED_FROM_TRY, from_try);
129 }
130
131 #[inline]
132 #[must_use]
133 pub const fn had_template(&self) -> bool {
134 self.flags.contains(UnionFlags::HAD_TEMPLATE)
135 }
136
137 #[inline]
138 #[must_use]
139 pub const fn by_reference(&self) -> bool {
140 self.flags.contains(UnionFlags::BY_REFERENCE)
141 }
142
143 #[inline]
144 #[must_use]
145 pub const fn reference_free(&self) -> bool {
146 self.flags.contains(UnionFlags::REFERENCE_FREE)
147 }
148
149 #[inline]
150 #[must_use]
151 pub const fn possibly_undefined_from_try(&self) -> bool {
152 self.flags.contains(UnionFlags::POSSIBLY_UNDEFINED_FROM_TRY)
153 }
154
155 #[inline]
156 #[must_use]
157 pub const fn possibly_undefined(&self) -> bool {
158 self.flags.contains(UnionFlags::POSSIBLY_UNDEFINED)
159 }
160
161 #[inline]
162 #[must_use]
163 pub const fn ignore_nullable_issues(&self) -> bool {
164 self.flags.contains(UnionFlags::IGNORE_NULLABLE_ISSUES)
165 }
166
167 #[inline]
168 #[must_use]
169 pub const fn ignore_falsable_issues(&self) -> bool {
170 self.flags.contains(UnionFlags::IGNORE_FALSABLE_ISSUES)
171 }
172
173 #[inline]
174 #[must_use]
175 pub const fn from_template_default(&self) -> bool {
176 self.flags.contains(UnionFlags::FROM_TEMPLATE_DEFAULT)
177 }
178
179 #[inline]
180 #[must_use]
181 pub const fn populated(&self) -> bool {
182 self.flags.contains(UnionFlags::POPULATED)
183 }
184
185 #[inline]
186 #[must_use]
187 pub const fn has_nullsafe_null(&self) -> bool {
188 self.flags.contains(UnionFlags::NULLSAFE_NULL)
189 }
190
191 #[inline]
192 pub fn set_had_template(&mut self, value: bool) {
193 self.flags.set(UnionFlags::HAD_TEMPLATE, value);
194 }
195
196 #[inline]
197 pub fn set_by_reference(&mut self, value: bool) {
198 self.flags.set(UnionFlags::BY_REFERENCE, value);
199 }
200
201 #[inline]
202 pub fn set_reference_free(&mut self, value: bool) {
203 self.flags.set(UnionFlags::REFERENCE_FREE, value);
204 }
205
206 #[inline]
207 pub fn set_possibly_undefined_from_try(&mut self, value: bool) {
208 self.flags.set(UnionFlags::POSSIBLY_UNDEFINED_FROM_TRY, value);
209 }
210
211 #[inline]
212 pub fn set_ignore_nullable_issues(&mut self, value: bool) {
213 self.flags.set(UnionFlags::IGNORE_NULLABLE_ISSUES, value);
214 }
215
216 #[inline]
217 pub fn set_ignore_falsable_issues(&mut self, value: bool) {
218 self.flags.set(UnionFlags::IGNORE_FALSABLE_ISSUES, value);
219 }
220
221 #[inline]
222 pub fn set_from_template_default(&mut self, value: bool) {
223 self.flags.set(UnionFlags::FROM_TEMPLATE_DEFAULT, value);
224 }
225
226 #[inline]
227 pub fn set_populated(&mut self, value: bool) {
228 self.flags.set(UnionFlags::POPULATED, value);
229 }
230
231 #[inline]
232 pub fn set_nullsafe_null(&mut self, value: bool) {
233 self.flags.set(UnionFlags::NULLSAFE_NULL, value);
234 }
235
236 #[must_use]
238 pub fn clone_with_types(&self, types: Vec<TAtomic>) -> TUnion {
239 TUnion { types: Cow::Owned(types), flags: self.flags }
240 }
241
242 #[must_use]
243 pub fn to_non_nullable(&self) -> TUnion {
244 TUnion { types: Cow::Owned(self.get_non_nullable_types()), flags: self.flags & !UnionFlags::NULLSAFE_NULL }
245 }
246
247 #[must_use]
248 pub fn to_truthy(&self) -> TUnion {
249 TUnion { types: Cow::Owned(self.get_truthy_types()), flags: self.flags }
250 }
251
252 #[must_use]
253 pub fn get_non_nullable_types(&self) -> Vec<TAtomic> {
254 self.types
255 .iter()
256 .filter_map(|t| match t {
257 TAtomic::Null | TAtomic::Void => None,
258 TAtomic::GenericParameter(parameter) => Some(TAtomic::GenericParameter(TGenericParameter {
259 parameter_name: parameter.parameter_name,
260 defining_entity: parameter.defining_entity,
261 intersection_types: parameter.intersection_types.clone(),
262 constraint: Arc::new(parameter.constraint.to_non_nullable()),
263 })),
264 TAtomic::Mixed(mixed) => Some(TAtomic::Mixed(mixed.with_is_non_null(true))),
265 atomic => Some(atomic.clone()),
266 })
267 .collect()
268 }
269
270 #[must_use]
271 pub fn get_truthy_types(&self) -> Vec<TAtomic> {
272 self.types
273 .iter()
274 .filter_map(|t| match t {
275 TAtomic::GenericParameter(parameter) => Some(TAtomic::GenericParameter(TGenericParameter {
276 parameter_name: parameter.parameter_name,
277 defining_entity: parameter.defining_entity,
278 intersection_types: parameter.intersection_types.clone(),
279 constraint: Arc::new(parameter.constraint.to_truthy()),
280 })),
281 TAtomic::Mixed(mixed) => Some(TAtomic::Mixed(mixed.with_truthiness(TMixedTruthiness::Truthy))),
282 atomic => {
283 if atomic.is_falsy() {
284 None
285 } else {
286 Some(atomic.clone())
287 }
288 }
289 })
290 .collect()
291 }
292
293 #[must_use]
295 pub fn as_nullable(mut self) -> TUnion {
296 let types = self.types.to_mut();
297
298 for atomic in types.iter_mut() {
299 if let TAtomic::Mixed(mixed) = atomic {
300 *mixed = mixed.with_is_non_null(false);
301 }
302 }
303
304 if !types.iter().any(|atomic| atomic.is_null() || atomic.is_mixed()) {
305 types.push(TAtomic::Null);
306 }
307
308 self
309 }
310
311 pub fn remove_type(&mut self, bad_type: &TAtomic) {
313 self.types.to_mut().retain(|t| t != bad_type);
314 }
315
316 pub fn replace_type(&mut self, remove_type: &TAtomic, add_type: TAtomic) {
318 let types = self.types.to_mut();
319
320 if let Some(index) = types.iter().position(|t| t == remove_type) {
321 types[index] = add_type;
322 } else {
323 types.push(add_type);
324 }
325 }
326
327 #[must_use]
328 pub fn is_int(&self) -> bool {
329 for atomic in self.types.as_ref() {
330 if !atomic.is_int() {
331 return false;
332 }
333 }
334
335 true
336 }
337
338 #[must_use]
339 pub fn has_int_or_float(&self) -> bool {
340 for atomic in self.types.as_ref() {
341 if atomic.is_int_or_float() {
342 return true;
343 }
344 }
345
346 false
347 }
348
349 #[must_use]
350 pub fn has_int_and_float(&self) -> bool {
351 let mut has_int = false;
352 let mut has_float = false;
353
354 for atomic in self.types.as_ref() {
355 if atomic.is_int() {
356 has_int = true;
357 } else if atomic.is_float() {
358 has_float = true;
359 } else if atomic.is_int_or_float() {
360 has_int = true;
361 has_float = true;
362 }
363
364 if has_int && has_float {
365 return true;
366 }
367 }
368
369 false
370 }
371
372 #[must_use]
373 pub fn has_int_and_string(&self) -> bool {
374 let mut has_int = false;
375 let mut has_string = false;
376
377 for atomic in self.types.as_ref() {
378 if atomic.is_int() {
379 has_int = true;
380 } else if atomic.is_string() {
381 has_string = true;
382 } else if atomic.is_array_key() {
383 has_int = true;
384 has_string = true;
385 }
386
387 if has_int && has_string {
388 return true;
389 }
390 }
391
392 false
393 }
394
395 #[must_use]
396 pub fn has_int(&self) -> bool {
397 for atomic in self.types.as_ref() {
398 if atomic.is_int() || atomic.is_array_key() || atomic.is_numeric() {
399 return true;
400 }
401 }
402
403 false
404 }
405
406 #[must_use]
407 pub fn has_float(&self) -> bool {
408 for atomic in self.types.as_ref() {
409 if atomic.is_float() {
410 return true;
411 }
412 }
413
414 false
415 }
416
417 #[must_use]
418 pub fn is_array_key(&self) -> bool {
419 for atomic in self.types.as_ref() {
420 if atomic.is_array_key() {
421 continue;
422 }
423
424 return false;
425 }
426
427 true
428 }
429
430 #[must_use]
431 pub fn is_any_string(&self) -> bool {
432 for atomic in self.types.as_ref() {
433 if !atomic.is_any_string() {
434 return false;
435 }
436 }
437
438 true
439 }
440
441 pub fn is_string(&self) -> bool {
442 self.types.iter().all(TAtomic::is_string) && !self.types.is_empty()
443 }
444
445 #[must_use]
446 pub fn is_always_array_key(&self, ignore_never: bool) -> bool {
447 self.types.iter().all(|atomic| match atomic {
448 TAtomic::Never => ignore_never,
449 TAtomic::Scalar(scalar) => matches!(
450 scalar,
451 TScalar::ArrayKey | TScalar::Integer(_) | TScalar::String(_) | TScalar::ClassLikeString(_)
452 ),
453 TAtomic::GenericParameter(generic_parameter) => {
454 generic_parameter.constraint.is_always_array_key(ignore_never)
455 }
456 _ => false,
457 })
458 }
459
460 pub fn is_non_empty_string(&self) -> bool {
461 self.types.iter().all(TAtomic::is_non_empty_string) && !self.types.is_empty()
462 }
463
464 pub fn is_empty_array(&self) -> bool {
465 self.types.iter().all(TAtomic::is_empty_array) && !self.types.is_empty()
466 }
467
468 pub fn has_string(&self) -> bool {
469 self.types.iter().any(TAtomic::is_string) && !self.types.is_empty()
470 }
471
472 pub fn is_float(&self) -> bool {
473 self.types.iter().all(TAtomic::is_float) && !self.types.is_empty()
474 }
475
476 pub fn is_bool(&self) -> bool {
477 self.types.iter().all(TAtomic::is_bool) && !self.types.is_empty()
478 }
479
480 pub fn is_never(&self) -> bool {
481 self.types.iter().all(TAtomic::is_never) || self.types.is_empty()
482 }
483
484 pub fn is_never_template(&self) -> bool {
485 self.types.iter().all(TAtomic::is_templated_as_never) && !self.types.is_empty()
486 }
487
488 #[must_use]
489 pub fn is_placeholder(&self) -> bool {
490 self.types.iter().all(|t| matches!(t, TAtomic::Placeholder)) && !self.types.is_empty()
491 }
492
493 pub fn is_true(&self) -> bool {
494 self.types.iter().all(TAtomic::is_true) && !self.types.is_empty()
495 }
496
497 pub fn is_false(&self) -> bool {
498 self.types.iter().all(TAtomic::is_false) && !self.types.is_empty()
499 }
500
501 #[must_use]
502 pub fn is_nonnull(&self) -> bool {
503 self.types.len() == 1 && matches!(self.types[0], TAtomic::Mixed(mixed) if mixed.is_non_null())
504 }
505
506 pub fn is_numeric(&self) -> bool {
507 self.types.iter().all(TAtomic::is_numeric) && !self.types.is_empty()
508 }
509
510 pub fn is_int_or_float(&self) -> bool {
511 self.types.iter().all(TAtomic::is_int_or_float) && !self.types.is_empty()
512 }
513
514 #[must_use]
517 pub fn effective_int_or_float(&self) -> Option<bool> {
518 let mut result: Option<bool> = None;
519 for atomic in self.types.as_ref() {
520 match atomic.effective_int_or_float() {
521 Some(is_int) => {
522 if let Some(prev) = result {
523 if prev != is_int {
524 return None;
525 }
526 } else {
527 result = Some(is_int);
528 }
529 }
530 None => return None,
531 }
532 }
533
534 result
535 }
536
537 #[must_use]
538 pub fn is_mixed(&self) -> bool {
539 self.types.iter().all(|t| matches!(t, TAtomic::Mixed(_))) && !self.types.is_empty()
540 }
541
542 pub fn is_mixed_template(&self) -> bool {
543 self.types.iter().all(TAtomic::is_templated_as_mixed) && !self.types.is_empty()
544 }
545
546 #[must_use]
547 pub fn has_mixed(&self) -> bool {
548 self.types.iter().any(|t| matches!(t, TAtomic::Mixed(_))) && !self.types.is_empty()
549 }
550
551 pub fn has_mixed_template(&self) -> bool {
552 self.types.iter().any(TAtomic::is_templated_as_mixed) && !self.types.is_empty()
553 }
554
555 #[must_use]
556 pub fn has_nullable_mixed(&self) -> bool {
557 self.types.iter().any(|t| matches!(t, TAtomic::Mixed(mixed) if !mixed.is_non_null())) && !self.types.is_empty()
558 }
559
560 #[must_use]
561 pub fn has_void(&self) -> bool {
562 self.types.iter().any(|t| matches!(t, TAtomic::Void)) && !self.types.is_empty()
563 }
564
565 #[must_use]
566 pub fn has_null(&self) -> bool {
567 self.types.iter().any(|t| matches!(t, TAtomic::Null)) && !self.types.is_empty()
568 }
569
570 #[must_use]
571 pub fn has_nullish(&self) -> bool {
572 self.types.iter().any(|t| match t {
573 TAtomic::Null | TAtomic::Void => true,
574 TAtomic::Mixed(mixed) => !mixed.is_non_null(),
575 TAtomic::GenericParameter(parameter) => parameter.constraint.has_nullish(),
576 _ => false,
577 }) && !self.types.is_empty()
578 }
579
580 #[must_use]
581 pub fn is_nullable_mixed(&self) -> bool {
582 if self.types.len() != 1 {
583 return false;
584 }
585
586 match &self.types[0] {
587 TAtomic::Mixed(mixed) => !mixed.is_non_null(),
588 _ => false,
589 }
590 }
591
592 #[must_use]
593 pub fn is_falsy_mixed(&self) -> bool {
594 if self.types.len() != 1 {
595 return false;
596 }
597
598 matches!(&self.types[0], &TAtomic::Mixed(mixed) if mixed.is_falsy())
599 }
600
601 #[must_use]
602 pub fn is_vanilla_mixed(&self) -> bool {
603 if self.types.len() != 1 {
604 return false;
605 }
606
607 self.types[0].is_vanilla_mixed()
608 }
609
610 #[must_use]
611 pub fn is_templated_as_vanilla_mixed(&self) -> bool {
612 if self.types.len() != 1 {
613 return false;
614 }
615
616 self.types[0].is_templated_as_vanilla_mixed()
617 }
618
619 #[must_use]
620 pub fn has_template_or_static(&self) -> bool {
621 for atomic in self.types.as_ref() {
622 if let TAtomic::GenericParameter(_) = atomic {
623 return true;
624 }
625
626 if let TAtomic::Object(TObject::Named(named_object)) = atomic {
627 if named_object.is_this() {
628 return true;
629 }
630
631 if let Some(intersections) = named_object.get_intersection_types() {
632 for intersection in intersections {
633 if let TAtomic::GenericParameter(_) = intersection {
634 return true;
635 }
636 }
637 }
638 }
639 }
640
641 false
642 }
643
644 #[must_use]
645 pub fn has_template(&self) -> bool {
646 for atomic in self.types.as_ref() {
647 if let TAtomic::GenericParameter(_) = atomic {
648 return true;
649 }
650
651 if let Some(intersections) = atomic.get_intersection_types() {
652 for intersection in intersections {
653 if let TAtomic::GenericParameter(_) = intersection {
654 return true;
655 }
656 }
657 }
658 }
659
660 false
661 }
662
663 #[must_use]
664 pub fn has_template_types(&self) -> bool {
665 let all_child_nodes = self.get_all_child_nodes();
666
667 for child_node in all_child_nodes {
668 if let TypeRef::Atomic(
669 TAtomic::GenericParameter(_)
670 | TAtomic::Scalar(TScalar::ClassLikeString(TClassLikeString::Generic { .. })),
671 ) = child_node
672 {
673 return true;
674 }
675 }
676
677 false
678 }
679
680 #[must_use]
681 pub fn get_template_types(&self) -> Vec<&TAtomic> {
682 let all_child_nodes = self.get_all_child_nodes();
683
684 let mut template_types = Vec::new();
685
686 for child_node in all_child_nodes {
687 if let TypeRef::Atomic(inner) = child_node {
688 match inner {
689 TAtomic::GenericParameter(_) => {
690 template_types.push(inner);
691 }
692 TAtomic::Scalar(TScalar::ClassLikeString(TClassLikeString::Generic { .. })) => {
693 template_types.push(inner);
694 }
695 _ => {}
696 }
697 }
698 }
699
700 template_types
701 }
702
703 pub fn is_objecty(&self) -> bool {
704 for atomic in self.types.as_ref() {
705 if let &TAtomic::Object(_) = atomic {
706 continue;
707 }
708
709 if let TAtomic::Callable(callable) = atomic
710 && callable.get_signature().is_none_or(super::atomic::callable::TCallableSignature::is_closure)
711 {
712 continue;
713 }
714
715 return false;
716 }
717
718 true
719 }
720
721 #[must_use]
722 pub fn is_generator(&self) -> bool {
723 for atomic in self.types.as_ref() {
724 if atomic.is_generator() {
725 continue;
726 }
727
728 return false;
729 }
730
731 true
732 }
733
734 #[must_use]
735 pub fn extends_or_implements(&self, codebase: &CodebaseMetadata, interface: &str) -> bool {
736 for atomic in self.types.as_ref() {
737 if !atomic.extends_or_implements(codebase, interface) {
738 return false;
739 }
740 }
741
742 true
743 }
744
745 #[must_use]
746 pub fn is_generic_parameter(&self) -> bool {
747 self.types.len() == 1 && matches!(self.types[0], TAtomic::GenericParameter(_))
748 }
749
750 #[must_use]
751 pub fn get_generic_parameter_constraint(&self) -> Option<&TUnion> {
752 if self.is_generic_parameter()
753 && let TAtomic::GenericParameter(parameter) = &self.types[0]
754 {
755 return Some(¶meter.constraint);
756 }
757
758 None
759 }
760
761 #[must_use]
762 pub fn is_null(&self) -> bool {
763 self.types.iter().all(|t| matches!(t, TAtomic::Null)) && !self.types.is_empty()
764 }
765
766 #[must_use]
767 pub fn is_nullable(&self) -> bool {
768 self.types.iter().any(|t| match t {
769 TAtomic::Null => self.types.len() >= 2,
770 TAtomic::GenericParameter(param) => param.constraint.is_nullable(),
771 _ => false,
772 })
773 }
774
775 #[must_use]
776 pub fn can_be_null(&self) -> bool {
777 self.types.iter().any(|t| match t {
778 TAtomic::Null => true,
779 TAtomic::Void => true,
780 TAtomic::Mixed(mixed) if !mixed.is_non_null() => true,
781 TAtomic::GenericParameter(param) => param.constraint.can_be_null(),
782 _ => false,
783 })
784 }
785
786 #[must_use]
787 pub fn is_void(&self) -> bool {
788 self.types.iter().all(|t| matches!(t, TAtomic::Void)) && !self.types.is_empty()
789 }
790
791 #[must_use]
792 pub fn is_voidable(&self) -> bool {
793 self.types.iter().any(|t| matches!(t, TAtomic::Void)) && !self.types.is_empty()
794 }
795
796 pub fn has_resource(&self) -> bool {
797 self.types.iter().any(TAtomic::is_resource)
798 }
799
800 pub fn is_resource(&self) -> bool {
801 self.types.iter().all(TAtomic::is_resource) && !self.types.is_empty()
802 }
803
804 pub fn is_array(&self) -> bool {
805 self.types.iter().all(TAtomic::is_array) && !self.types.is_empty()
806 }
807
808 pub fn is_list(&self) -> bool {
809 self.types.iter().all(TAtomic::is_list) && !self.types.is_empty()
810 }
811
812 pub fn is_vanilla_array(&self) -> bool {
813 self.types.iter().all(TAtomic::is_vanilla_array) && !self.types.is_empty()
814 }
815
816 pub fn is_keyed_array(&self) -> bool {
817 self.types.iter().all(TAtomic::is_keyed_array) && !self.types.is_empty()
818 }
819
820 pub fn is_falsable(&self) -> bool {
821 self.types.len() >= 2 && self.types.iter().any(TAtomic::is_false)
822 }
823
824 #[must_use]
825 pub fn has_bool(&self) -> bool {
826 self.types.iter().any(|t| t.is_bool() || t.is_generic_scalar()) && !self.types.is_empty()
827 }
828
829 pub fn has_scalar(&self) -> bool {
835 self.types.iter().any(TAtomic::is_generic_scalar)
836 }
837
838 #[must_use]
841 pub fn has_scalar_combination(&self) -> bool {
842 const HAS_INT: u8 = 1 << 0;
843 const HAS_FLOAT: u8 = 1 << 1;
844 const HAS_BOOL: u8 = 1 << 2;
845 const HAS_STRING: u8 = 1 << 3;
846 const ALL_SCALARS: u8 = HAS_INT | HAS_FLOAT | HAS_BOOL | HAS_STRING;
847
848 let mut flags = 0u8;
849
850 for atomic in self.types.as_ref() {
851 if atomic.is_int() {
852 flags |= HAS_INT;
853 } else if atomic.is_float() {
854 flags |= HAS_FLOAT;
855 } else if atomic.is_bool() {
856 flags |= HAS_BOOL;
857 } else if atomic.is_string() {
858 flags |= HAS_STRING;
859 } else if atomic.is_array_key() {
860 flags |= HAS_INT | HAS_STRING;
861 } else if atomic.is_numeric() {
862 flags |= HAS_INT | HAS_FLOAT;
864 } else if atomic.is_generic_scalar() {
865 return true;
866 }
867
868 if flags == ALL_SCALARS {
870 return true;
871 }
872 }
873
874 flags == ALL_SCALARS
875 }
876 pub fn has_array_key(&self) -> bool {
877 self.types.iter().any(TAtomic::is_array_key)
878 }
879
880 pub fn has_iterable(&self) -> bool {
881 self.types.iter().any(TAtomic::is_iterable) && !self.types.is_empty()
882 }
883
884 pub fn has_array(&self) -> bool {
885 self.types.iter().any(TAtomic::is_array) && !self.types.is_empty()
886 }
887
888 #[must_use]
889 pub fn has_traversable(&self, codebase: &CodebaseMetadata) -> bool {
890 self.types.iter().any(|atomic| atomic.is_traversable(codebase)) && !self.types.is_empty()
891 }
892
893 #[must_use]
894 pub fn has_array_key_like(&self) -> bool {
895 self.types.iter().any(|atomic| atomic.is_array_key() || atomic.is_int() || atomic.is_string())
896 }
897
898 pub fn has_numeric(&self) -> bool {
899 self.types.iter().any(TAtomic::is_numeric) && !self.types.is_empty()
900 }
901
902 pub fn is_always_truthy(&self) -> bool {
903 self.types.iter().all(TAtomic::is_truthy) && !self.types.is_empty()
904 }
905
906 pub fn is_always_falsy(&self) -> bool {
907 self.types.iter().all(TAtomic::is_falsy) && !self.types.is_empty()
908 }
909
910 #[must_use]
911 pub fn is_literal_of(&self, other: &TUnion) -> bool {
912 let Some(other_atomic_type) = other.types.first() else {
913 return false;
914 };
915
916 match other_atomic_type {
917 TAtomic::Scalar(TScalar::String(_)) => {
918 for self_atomic_type in self.types.as_ref() {
919 if self_atomic_type.is_string_of_literal_origin() {
920 continue;
921 }
922
923 return false;
924 }
925
926 true
927 }
928 TAtomic::Scalar(TScalar::Integer(_)) => {
929 for self_atomic_type in self.types.as_ref() {
930 if self_atomic_type.is_literal_int() {
931 continue;
932 }
933
934 return false;
935 }
936
937 true
938 }
939 TAtomic::Scalar(TScalar::Float(_)) => {
940 for self_atomic_type in self.types.as_ref() {
941 if self_atomic_type.is_literal_float() {
942 continue;
943 }
944
945 return false;
946 }
947
948 true
949 }
950 _ => false,
951 }
952 }
953
954 #[must_use]
955 pub fn all_literals(&self) -> bool {
956 self.types
957 .iter()
958 .all(|atomic| atomic.is_string_of_literal_origin() || atomic.is_literal_int() || atomic.is_literal_float())
959 }
960
961 #[must_use]
962 pub fn has_static_object(&self) -> bool {
963 self.types
964 .iter()
965 .any(|atomic| matches!(atomic, TAtomic::Object(TObject::Named(named_object)) if named_object.is_this()))
966 }
967
968 #[must_use]
969 pub fn is_static_object(&self) -> bool {
970 self.types
971 .iter()
972 .all(|atomic| matches!(atomic, TAtomic::Object(TObject::Named(named_object)) if named_object.is_this()))
973 }
974
975 #[inline]
976 #[must_use]
977 pub fn is_single(&self) -> bool {
978 self.types.len() == 1
979 }
980
981 #[inline]
982 #[must_use]
983 pub fn get_single_string(&self) -> Option<&TString> {
984 if self.is_single()
985 && let TAtomic::Scalar(TScalar::String(string)) = &self.types[0]
986 {
987 Some(string)
988 } else {
989 None
990 }
991 }
992
993 #[inline]
994 #[must_use]
995 pub fn get_single_array(&self) -> Option<&TArray> {
996 if self.is_single()
997 && let TAtomic::Array(array) = &self.types[0]
998 {
999 Some(array)
1000 } else {
1001 None
1002 }
1003 }
1004
1005 #[inline]
1006 #[must_use]
1007 pub fn get_single_bool(&self) -> Option<&TBool> {
1008 if self.is_single()
1009 && let TAtomic::Scalar(TScalar::Bool(bool)) = &self.types[0]
1010 {
1011 Some(bool)
1012 } else {
1013 None
1014 }
1015 }
1016
1017 #[inline]
1018 #[must_use]
1019 pub fn get_single_named_object(&self) -> Option<&TNamedObject> {
1020 if self.is_single()
1021 && let TAtomic::Object(TObject::Named(named_object)) = &self.types[0]
1022 {
1023 Some(named_object)
1024 } else {
1025 None
1026 }
1027 }
1028
1029 #[inline]
1030 #[must_use]
1031 pub fn get_single_shaped_object(&self) -> Option<&TObjectWithProperties> {
1032 if self.is_single()
1033 && let TAtomic::Object(TObject::WithProperties(shaped_object)) = &self.types[0]
1034 {
1035 Some(shaped_object)
1036 } else {
1037 None
1038 }
1039 }
1040
1041 #[inline]
1042 #[must_use]
1043 pub fn get_single(&self) -> &TAtomic {
1044 &self.types[0]
1045 }
1046
1047 #[inline]
1048 #[must_use]
1049 pub fn get_single_owned(self) -> TAtomic {
1050 self.types[0].clone()
1051 }
1052
1053 #[inline]
1054 #[must_use]
1055 pub fn is_named_object(&self) -> bool {
1056 self.types.iter().all(|t| matches!(t, TAtomic::Object(TObject::Named(_))))
1057 }
1058
1059 #[must_use]
1060 pub fn is_enum(&self) -> bool {
1061 self.types.iter().all(|t| matches!(t, TAtomic::Object(TObject::Enum(_))))
1062 }
1063
1064 #[must_use]
1065 pub fn is_enum_case(&self) -> bool {
1066 self.types.iter().all(|t| matches!(t, TAtomic::Object(TObject::Enum(r#enum)) if r#enum.case.is_some()))
1067 }
1068
1069 #[must_use]
1070 pub fn is_single_enum_case(&self) -> bool {
1071 self.is_single()
1072 && self.types.iter().all(|t| matches!(t, TAtomic::Object(TObject::Enum(r#enum)) if r#enum.case.is_some()))
1073 }
1074
1075 #[inline]
1076 #[must_use]
1077 pub fn has_named_object(&self) -> bool {
1078 self.types.iter().any(|t| matches!(t, TAtomic::Object(TObject::Named(_))))
1079 }
1080
1081 #[inline]
1082 #[must_use]
1083 pub fn has_object(&self) -> bool {
1084 self.types.iter().any(|t| matches!(t, TAtomic::Object(TObject::Any | TObject::WithProperties(_))))
1085 }
1086
1087 #[inline]
1088 #[must_use]
1089 pub fn has_callable(&self) -> bool {
1090 self.types.iter().any(|t| matches!(t, TAtomic::Callable(_)))
1091 }
1092
1093 #[inline]
1094 #[must_use]
1095 pub fn is_callable(&self) -> bool {
1096 self.types.iter().all(|t| matches!(t, TAtomic::Callable(_)))
1097 }
1098
1099 #[inline]
1100 #[must_use]
1101 pub fn has_object_type(&self) -> bool {
1102 self.types.iter().any(|t| matches!(t, TAtomic::Object(_)))
1103 }
1104
1105 #[must_use]
1108 pub fn get_enum_cases(&self) -> Vec<(Atom, Option<Atom>)> {
1109 self.types
1110 .iter()
1111 .filter_map(|t| match t {
1112 TAtomic::Object(TObject::Enum(enum_object)) => Some((enum_object.name, enum_object.case)),
1113 _ => None,
1114 })
1115 .collect()
1116 }
1117
1118 #[must_use]
1119 pub fn get_single_int(&self) -> Option<TInteger> {
1120 if self.is_single() { self.get_single().get_integer() } else { None }
1121 }
1122
1123 #[must_use]
1124 pub fn get_single_literal_int_value(&self) -> Option<i64> {
1125 if self.is_single() { self.get_single().get_literal_int_value() } else { None }
1126 }
1127
1128 #[must_use]
1129 pub fn get_single_maximum_int_value(&self) -> Option<i64> {
1130 if self.is_single() { self.get_single().get_maximum_int_value() } else { None }
1131 }
1132
1133 #[must_use]
1134 pub fn get_single_minimum_int_value(&self) -> Option<i64> {
1135 if self.is_single() { self.get_single().get_minimum_int_value() } else { None }
1136 }
1137
1138 #[must_use]
1139 pub fn get_single_literal_float_value(&self) -> Option<f64> {
1140 if self.is_single() { self.get_single().get_literal_float_value() } else { None }
1141 }
1142
1143 #[must_use]
1144 pub fn get_single_literal_string_value(&self) -> Option<&str> {
1145 if self.is_single() { self.get_single().get_literal_string_value() } else { None }
1146 }
1147
1148 #[must_use]
1149 pub fn get_single_class_string_value(&self) -> Option<Atom> {
1150 if self.is_single() { self.get_single().get_class_string_value() } else { None }
1151 }
1152
1153 #[must_use]
1154 pub fn get_single_array_key(&self) -> Option<ArrayKey> {
1155 if self.is_single() { self.get_single().to_array_key() } else { None }
1156 }
1157
1158 #[must_use]
1159 pub fn get_single_key_of_array_like(&self) -> Option<TUnion> {
1160 if !self.is_single() {
1161 return None;
1162 }
1163
1164 match self.get_single() {
1165 TAtomic::Array(array) => match array {
1166 TArray::List(_) => Some(get_int()),
1167 TArray::Keyed(keyed_array) => match &keyed_array.parameters {
1168 Some((k, _)) => Some((**k).clone()),
1169 None => Some(get_arraykey()),
1170 },
1171 },
1172 _ => None,
1173 }
1174 }
1175
1176 #[must_use]
1177 pub fn get_single_value_of_array_like(&self) -> Option<Cow<'_, TUnion>> {
1178 if !self.is_single() {
1179 return None;
1180 }
1181
1182 match self.get_single() {
1183 TAtomic::Array(array) => match array {
1184 TArray::List(list) => Some(Cow::Borrowed(&list.element_type)),
1185 TArray::Keyed(keyed_array) => match &keyed_array.parameters {
1186 Some((_, v)) => Some(Cow::Borrowed(v)),
1187 None => Some(Cow::Owned(get_mixed())),
1188 },
1189 },
1190 _ => None,
1191 }
1192 }
1193
1194 #[must_use]
1195 pub fn get_literal_ints(&self) -> Vec<&TAtomic> {
1196 self.types.iter().filter(|a| a.is_literal_int()).collect()
1197 }
1198
1199 #[must_use]
1200 pub fn get_literal_strings(&self) -> Vec<&TAtomic> {
1201 self.types.iter().filter(|a| a.is_known_literal_string()).collect()
1202 }
1203
1204 #[must_use]
1205 pub fn get_literal_string_values(&self) -> Vec<Option<Atom>> {
1206 self.get_literal_strings()
1207 .into_iter()
1208 .map(|atom| match atom {
1209 TAtomic::Scalar(TScalar::String(TString { literal: Some(TStringLiteral::Value(value)), .. })) => {
1210 Some(*value)
1211 }
1212 _ => None,
1213 })
1214 .collect()
1215 }
1216
1217 #[must_use]
1218 pub fn has_literal_float(&self) -> bool {
1219 self.types.iter().any(|atomic| match atomic {
1220 TAtomic::Scalar(scalar) => scalar.is_literal_float(),
1221 _ => false,
1222 })
1223 }
1224
1225 #[must_use]
1226 pub fn has_literal_int(&self) -> bool {
1227 self.types.iter().any(|atomic| match atomic {
1228 TAtomic::Scalar(scalar) => scalar.is_literal_int(),
1229 _ => false,
1230 })
1231 }
1232
1233 #[must_use]
1234 pub fn has_literal_string(&self) -> bool {
1235 self.types.iter().any(|atomic| match atomic {
1236 TAtomic::Scalar(scalar) => scalar.is_known_literal_string(),
1237 _ => false,
1238 })
1239 }
1240
1241 #[must_use]
1242 pub fn has_literal_value(&self) -> bool {
1243 self.types.iter().any(|atomic| match atomic {
1244 TAtomic::Scalar(scalar) => scalar.is_literal_value(),
1245 _ => false,
1246 })
1247 }
1248
1249 #[must_use]
1250 pub fn accepts_false(&self) -> bool {
1251 self.types.iter().any(|t| match t {
1252 TAtomic::GenericParameter(parameter) => parameter.constraint.accepts_false(),
1253 TAtomic::Mixed(mixed) if !mixed.is_truthy() => true,
1254 TAtomic::Scalar(TScalar::Generic | TScalar::Bool(TBool { value: None | Some(false) })) => true,
1255 _ => false,
1256 })
1257 }
1258
1259 #[must_use]
1260 pub fn accepts_null(&self) -> bool {
1261 self.types.iter().any(|t| match t {
1262 TAtomic::GenericParameter(generic_parameter) => generic_parameter.constraint.accepts_null(),
1263 TAtomic::Mixed(mixed) if !mixed.is_non_null() => true,
1264 TAtomic::Null => true,
1265 _ => false,
1266 })
1267 }
1268}
1269
1270impl TType for TUnion {
1271 fn get_child_nodes(&self) -> Vec<TypeRef<'_>> {
1272 self.types.iter().map(TypeRef::Atomic).collect()
1273 }
1274
1275 fn needs_population(&self) -> bool {
1276 !self.flags.contains(UnionFlags::POPULATED) && self.types.iter().any(super::TType::needs_population)
1277 }
1278
1279 fn is_expandable(&self) -> bool {
1280 if self.types.is_empty() {
1281 return true;
1282 }
1283
1284 self.types.iter().any(super::TType::is_expandable)
1285 }
1286
1287 fn is_complex(&self) -> bool {
1288 self.types.len() > 3 || self.types.iter().any(super::TType::is_complex)
1289 }
1290
1291 fn get_id(&self) -> Atom {
1292 let len = self.types.len();
1293
1294 let mut atomic_ids: Vec<Atom> = self
1295 .types
1296 .as_ref()
1297 .iter()
1298 .map(|atomic| {
1299 let id = atomic.get_id();
1300 if atomic.is_generic_parameter() || atomic.has_intersection_types() && len > 1 {
1301 concat_atom!("(", id.as_str(), ")")
1302 } else {
1303 id
1304 }
1305 })
1306 .collect();
1307
1308 if len <= 1 {
1309 return atomic_ids.pop().unwrap_or_else(empty_atom);
1310 }
1311
1312 atomic_ids.sort_unstable();
1313 let mut result = atomic_ids[0];
1314 for id in &atomic_ids[1..] {
1315 result = concat_atom!(result.as_str(), "|", id.as_str());
1316 }
1317
1318 result
1319 }
1320
1321 fn get_pretty_id_with_indent(&self, indent: usize) -> Atom {
1322 let len = self.types.len();
1323
1324 if len <= 1 {
1325 return self.types.first().map_or_else(empty_atom, |atomic| atomic.get_pretty_id_with_indent(indent));
1326 }
1327
1328 if len > 3 {
1330 let mut atomic_ids: Vec<Atom> = self
1331 .types
1332 .as_ref()
1333 .iter()
1334 .map(|atomic| {
1335 let id = atomic.get_pretty_id_with_indent(indent + 2);
1336 if atomic.has_intersection_types() { concat_atom!("(", id.as_str(), ")") } else { id }
1337 })
1338 .collect();
1339
1340 atomic_ids.sort_unstable();
1341
1342 let mut result = String::new();
1343 result += &atomic_ids[0];
1344 for id in &atomic_ids[1..] {
1345 result += "\n";
1346 result += &" ".repeat(indent);
1347 result += "| ";
1348 result += id.as_str();
1349 }
1350
1351 atom(&result)
1352 } else {
1353 let mut atomic_ids: Vec<Atom> = self
1355 .types
1356 .as_ref()
1357 .iter()
1358 .map(|atomic| {
1359 let id = atomic.get_pretty_id_with_indent(indent);
1360 if atomic.has_intersection_types() && len > 1 { concat_atom!("(", id.as_str(), ")") } else { id }
1361 })
1362 .collect();
1363
1364 atomic_ids.sort_unstable();
1365 let mut result = atomic_ids[0];
1366 for id in &atomic_ids[1..] {
1367 result = concat_atom!(result.as_str(), " | ", id.as_str());
1368 }
1369
1370 result
1371 }
1372 }
1373}
1374
1375impl PartialEq for TUnion {
1376 fn eq(&self, other: &TUnion) -> bool {
1377 if std::ptr::eq(self, other) {
1378 return true;
1379 }
1380
1381 const SEMANTIC_FLAGS: UnionFlags = UnionFlags::HAD_TEMPLATE
1382 .union(UnionFlags::BY_REFERENCE)
1383 .union(UnionFlags::REFERENCE_FREE)
1384 .union(UnionFlags::POSSIBLY_UNDEFINED_FROM_TRY)
1385 .union(UnionFlags::POSSIBLY_UNDEFINED)
1386 .union(UnionFlags::IGNORE_NULLABLE_ISSUES)
1387 .union(UnionFlags::IGNORE_FALSABLE_ISSUES)
1388 .union(UnionFlags::FROM_TEMPLATE_DEFAULT);
1389
1390 if self.flags.intersection(SEMANTIC_FLAGS) != other.flags.intersection(SEMANTIC_FLAGS) {
1391 return false;
1392 }
1393
1394 let len = self.types.len();
1395 if len != other.types.len() {
1396 return false;
1397 }
1398
1399 if self.types == other.types {
1402 return true;
1403 }
1404
1405 for i in 0..len {
1407 let mut has_match = false;
1408 for j in 0..len {
1409 if self.types[i] == other.types[j] {
1410 has_match = true;
1411 break;
1412 }
1413 }
1414
1415 if !has_match {
1416 return false;
1417 }
1418 }
1419
1420 for i in 0..len {
1422 let mut has_match = false;
1423 for j in 0..len {
1424 if other.types[i] == self.types[j] {
1425 has_match = true;
1426 break;
1427 }
1428 }
1429
1430 if !has_match {
1431 return false;
1432 }
1433 }
1434
1435 true
1436 }
1437}
1438
1439pub fn populate_union_type(
1440 unpopulated_union: &mut TUnion,
1441 codebase_symbols: &Symbols,
1442 reference_source: Option<&ReferenceSource>,
1443 symbol_references: &mut SymbolReferences,
1444 force: bool,
1445) {
1446 if unpopulated_union.flags.contains(UnionFlags::POPULATED) && !force {
1447 return;
1448 }
1449
1450 if !unpopulated_union.needs_population() {
1451 return;
1452 }
1453
1454 unpopulated_union.flags.insert(UnionFlags::POPULATED);
1455 let unpopulated_atomics = unpopulated_union.types.to_mut();
1456 for unpopulated_atomic in unpopulated_atomics {
1457 match unpopulated_atomic {
1458 TAtomic::Scalar(TScalar::ClassLikeString(
1459 TClassLikeString::Generic { constraint, .. } | TClassLikeString::OfType { constraint, .. },
1460 )) => {
1461 populate_atomic_type(
1462 Arc::make_mut(constraint),
1463 codebase_symbols,
1464 reference_source,
1465 symbol_references,
1466 force,
1467 );
1468 }
1469 _ => {
1470 populate_atomic_type(unpopulated_atomic, codebase_symbols, reference_source, symbol_references, force);
1471 }
1472 }
1473 }
1474}