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 is_void(&self) -> bool {
777 self.types.iter().all(|t| matches!(t, TAtomic::Void)) && !self.types.is_empty()
778 }
779
780 #[must_use]
781 pub fn is_voidable(&self) -> bool {
782 self.types.iter().any(|t| matches!(t, TAtomic::Void)) && !self.types.is_empty()
783 }
784
785 pub fn has_resource(&self) -> bool {
786 self.types.iter().any(TAtomic::is_resource)
787 }
788
789 pub fn is_resource(&self) -> bool {
790 self.types.iter().all(TAtomic::is_resource) && !self.types.is_empty()
791 }
792
793 pub fn is_array(&self) -> bool {
794 self.types.iter().all(TAtomic::is_array) && !self.types.is_empty()
795 }
796
797 pub fn is_list(&self) -> bool {
798 self.types.iter().all(TAtomic::is_list) && !self.types.is_empty()
799 }
800
801 pub fn is_vanilla_array(&self) -> bool {
802 self.types.iter().all(TAtomic::is_vanilla_array) && !self.types.is_empty()
803 }
804
805 pub fn is_keyed_array(&self) -> bool {
806 self.types.iter().all(TAtomic::is_keyed_array) && !self.types.is_empty()
807 }
808
809 pub fn is_falsable(&self) -> bool {
810 self.types.len() >= 2 && self.types.iter().any(TAtomic::is_false)
811 }
812
813 #[must_use]
814 pub fn has_bool(&self) -> bool {
815 self.types.iter().any(|t| t.is_bool() || t.is_generic_scalar()) && !self.types.is_empty()
816 }
817
818 pub fn has_scalar(&self) -> bool {
824 self.types.iter().any(TAtomic::is_generic_scalar)
825 }
826
827 #[must_use]
830 pub fn has_scalar_combination(&self) -> bool {
831 const HAS_INT: u8 = 1 << 0;
832 const HAS_FLOAT: u8 = 1 << 1;
833 const HAS_BOOL: u8 = 1 << 2;
834 const HAS_STRING: u8 = 1 << 3;
835 const ALL_SCALARS: u8 = HAS_INT | HAS_FLOAT | HAS_BOOL | HAS_STRING;
836
837 let mut flags = 0u8;
838
839 for atomic in self.types.as_ref() {
840 if atomic.is_int() {
841 flags |= HAS_INT;
842 } else if atomic.is_float() {
843 flags |= HAS_FLOAT;
844 } else if atomic.is_bool() {
845 flags |= HAS_BOOL;
846 } else if atomic.is_string() {
847 flags |= HAS_STRING;
848 } else if atomic.is_array_key() {
849 flags |= HAS_INT | HAS_STRING;
850 } else if atomic.is_numeric() {
851 flags |= HAS_INT | HAS_FLOAT;
853 } else if atomic.is_generic_scalar() {
854 return true;
855 }
856
857 if flags == ALL_SCALARS {
859 return true;
860 }
861 }
862
863 flags == ALL_SCALARS
864 }
865 pub fn has_array_key(&self) -> bool {
866 self.types.iter().any(TAtomic::is_array_key)
867 }
868
869 pub fn has_iterable(&self) -> bool {
870 self.types.iter().any(TAtomic::is_iterable) && !self.types.is_empty()
871 }
872
873 pub fn has_array(&self) -> bool {
874 self.types.iter().any(TAtomic::is_array) && !self.types.is_empty()
875 }
876
877 #[must_use]
878 pub fn has_traversable(&self, codebase: &CodebaseMetadata) -> bool {
879 self.types.iter().any(|atomic| atomic.is_traversable(codebase)) && !self.types.is_empty()
880 }
881
882 #[must_use]
883 pub fn has_array_key_like(&self) -> bool {
884 self.types.iter().any(|atomic| atomic.is_array_key() || atomic.is_int() || atomic.is_string())
885 }
886
887 pub fn has_numeric(&self) -> bool {
888 self.types.iter().any(TAtomic::is_numeric) && !self.types.is_empty()
889 }
890
891 pub fn is_always_truthy(&self) -> bool {
892 self.types.iter().all(TAtomic::is_truthy) && !self.types.is_empty()
893 }
894
895 pub fn is_always_falsy(&self) -> bool {
896 self.types.iter().all(TAtomic::is_falsy) && !self.types.is_empty()
897 }
898
899 #[must_use]
900 pub fn is_literal_of(&self, other: &TUnion) -> bool {
901 let Some(other_atomic_type) = other.types.first() else {
902 return false;
903 };
904
905 match other_atomic_type {
906 TAtomic::Scalar(TScalar::String(_)) => {
907 for self_atomic_type in self.types.as_ref() {
908 if self_atomic_type.is_string_of_literal_origin() {
909 continue;
910 }
911
912 return false;
913 }
914
915 true
916 }
917 TAtomic::Scalar(TScalar::Integer(_)) => {
918 for self_atomic_type in self.types.as_ref() {
919 if self_atomic_type.is_literal_int() {
920 continue;
921 }
922
923 return false;
924 }
925
926 true
927 }
928 TAtomic::Scalar(TScalar::Float(_)) => {
929 for self_atomic_type in self.types.as_ref() {
930 if self_atomic_type.is_literal_float() {
931 continue;
932 }
933
934 return false;
935 }
936
937 true
938 }
939 _ => false,
940 }
941 }
942
943 #[must_use]
944 pub fn all_literals(&self) -> bool {
945 self.types
946 .iter()
947 .all(|atomic| atomic.is_string_of_literal_origin() || atomic.is_literal_int() || atomic.is_literal_float())
948 }
949
950 #[must_use]
951 pub fn has_static_object(&self) -> bool {
952 self.types
953 .iter()
954 .any(|atomic| matches!(atomic, TAtomic::Object(TObject::Named(named_object)) if named_object.is_this()))
955 }
956
957 #[must_use]
958 pub fn is_static_object(&self) -> bool {
959 self.types
960 .iter()
961 .all(|atomic| matches!(atomic, TAtomic::Object(TObject::Named(named_object)) if named_object.is_this()))
962 }
963
964 #[inline]
965 #[must_use]
966 pub fn is_single(&self) -> bool {
967 self.types.len() == 1
968 }
969
970 #[inline]
971 #[must_use]
972 pub fn get_single_string(&self) -> Option<&TString> {
973 if self.is_single()
974 && let TAtomic::Scalar(TScalar::String(string)) = &self.types[0]
975 {
976 Some(string)
977 } else {
978 None
979 }
980 }
981
982 #[inline]
983 #[must_use]
984 pub fn get_single_array(&self) -> Option<&TArray> {
985 if self.is_single()
986 && let TAtomic::Array(array) = &self.types[0]
987 {
988 Some(array)
989 } else {
990 None
991 }
992 }
993
994 #[inline]
995 #[must_use]
996 pub fn get_single_bool(&self) -> Option<&TBool> {
997 if self.is_single()
998 && let TAtomic::Scalar(TScalar::Bool(bool)) = &self.types[0]
999 {
1000 Some(bool)
1001 } else {
1002 None
1003 }
1004 }
1005
1006 #[inline]
1007 #[must_use]
1008 pub fn get_single_named_object(&self) -> Option<&TNamedObject> {
1009 if self.is_single()
1010 && let TAtomic::Object(TObject::Named(named_object)) = &self.types[0]
1011 {
1012 Some(named_object)
1013 } else {
1014 None
1015 }
1016 }
1017
1018 #[inline]
1019 #[must_use]
1020 pub fn get_single_shaped_object(&self) -> Option<&TObjectWithProperties> {
1021 if self.is_single()
1022 && let TAtomic::Object(TObject::WithProperties(shaped_object)) = &self.types[0]
1023 {
1024 Some(shaped_object)
1025 } else {
1026 None
1027 }
1028 }
1029
1030 #[inline]
1031 #[must_use]
1032 pub fn get_single(&self) -> &TAtomic {
1033 &self.types[0]
1034 }
1035
1036 #[inline]
1037 #[must_use]
1038 pub fn get_single_owned(self) -> TAtomic {
1039 self.types[0].clone()
1040 }
1041
1042 #[inline]
1043 #[must_use]
1044 pub fn is_named_object(&self) -> bool {
1045 self.types.iter().all(|t| matches!(t, TAtomic::Object(TObject::Named(_))))
1046 }
1047
1048 #[must_use]
1049 pub fn is_enum(&self) -> bool {
1050 self.types.iter().all(|t| matches!(t, TAtomic::Object(TObject::Enum(_))))
1051 }
1052
1053 #[must_use]
1054 pub fn is_enum_case(&self) -> bool {
1055 self.types.iter().all(|t| matches!(t, TAtomic::Object(TObject::Enum(r#enum)) if r#enum.case.is_some()))
1056 }
1057
1058 #[must_use]
1059 pub fn is_single_enum_case(&self) -> bool {
1060 self.is_single()
1061 && self.types.iter().all(|t| matches!(t, TAtomic::Object(TObject::Enum(r#enum)) if r#enum.case.is_some()))
1062 }
1063
1064 #[inline]
1065 #[must_use]
1066 pub fn has_named_object(&self) -> bool {
1067 self.types.iter().any(|t| matches!(t, TAtomic::Object(TObject::Named(_))))
1068 }
1069
1070 #[inline]
1071 #[must_use]
1072 pub fn has_object(&self) -> bool {
1073 self.types.iter().any(|t| matches!(t, TAtomic::Object(TObject::Any | TObject::WithProperties(_))))
1074 }
1075
1076 #[inline]
1077 #[must_use]
1078 pub fn has_callable(&self) -> bool {
1079 self.types.iter().any(|t| matches!(t, TAtomic::Callable(_)))
1080 }
1081
1082 #[inline]
1083 #[must_use]
1084 pub fn is_callable(&self) -> bool {
1085 self.types.iter().all(|t| matches!(t, TAtomic::Callable(_)))
1086 }
1087
1088 #[inline]
1089 #[must_use]
1090 pub fn has_object_type(&self) -> bool {
1091 self.types.iter().any(|t| matches!(t, TAtomic::Object(_)))
1092 }
1093
1094 #[must_use]
1097 pub fn get_enum_cases(&self) -> Vec<(Atom, Option<Atom>)> {
1098 self.types
1099 .iter()
1100 .filter_map(|t| match t {
1101 TAtomic::Object(TObject::Enum(enum_object)) => Some((enum_object.name, enum_object.case)),
1102 _ => None,
1103 })
1104 .collect()
1105 }
1106
1107 #[must_use]
1108 pub fn get_single_int(&self) -> Option<TInteger> {
1109 if self.is_single() { self.get_single().get_integer() } else { None }
1110 }
1111
1112 #[must_use]
1113 pub fn get_single_literal_int_value(&self) -> Option<i64> {
1114 if self.is_single() { self.get_single().get_literal_int_value() } else { None }
1115 }
1116
1117 #[must_use]
1118 pub fn get_single_maximum_int_value(&self) -> Option<i64> {
1119 if self.is_single() { self.get_single().get_maximum_int_value() } else { None }
1120 }
1121
1122 #[must_use]
1123 pub fn get_single_minimum_int_value(&self) -> Option<i64> {
1124 if self.is_single() { self.get_single().get_minimum_int_value() } else { None }
1125 }
1126
1127 #[must_use]
1128 pub fn get_single_literal_float_value(&self) -> Option<f64> {
1129 if self.is_single() { self.get_single().get_literal_float_value() } else { None }
1130 }
1131
1132 #[must_use]
1133 pub fn get_single_literal_string_value(&self) -> Option<&str> {
1134 if self.is_single() { self.get_single().get_literal_string_value() } else { None }
1135 }
1136
1137 #[must_use]
1138 pub fn get_single_class_string_value(&self) -> Option<Atom> {
1139 if self.is_single() { self.get_single().get_class_string_value() } else { None }
1140 }
1141
1142 #[must_use]
1143 pub fn get_single_array_key(&self) -> Option<ArrayKey> {
1144 if self.is_single() { self.get_single().to_array_key() } else { None }
1145 }
1146
1147 #[must_use]
1148 pub fn get_single_key_of_array_like(&self) -> Option<TUnion> {
1149 if !self.is_single() {
1150 return None;
1151 }
1152
1153 match self.get_single() {
1154 TAtomic::Array(array) => match array {
1155 TArray::List(_) => Some(get_int()),
1156 TArray::Keyed(keyed_array) => match &keyed_array.parameters {
1157 Some((k, _)) => Some((**k).clone()),
1158 None => Some(get_arraykey()),
1159 },
1160 },
1161 _ => None,
1162 }
1163 }
1164
1165 #[must_use]
1166 pub fn get_single_value_of_array_like(&self) -> Option<Cow<'_, TUnion>> {
1167 if !self.is_single() {
1168 return None;
1169 }
1170
1171 match self.get_single() {
1172 TAtomic::Array(array) => match array {
1173 TArray::List(list) => Some(Cow::Borrowed(&list.element_type)),
1174 TArray::Keyed(keyed_array) => match &keyed_array.parameters {
1175 Some((_, v)) => Some(Cow::Borrowed(v)),
1176 None => Some(Cow::Owned(get_mixed())),
1177 },
1178 },
1179 _ => None,
1180 }
1181 }
1182
1183 #[must_use]
1184 pub fn get_literal_ints(&self) -> Vec<&TAtomic> {
1185 self.types.iter().filter(|a| a.is_literal_int()).collect()
1186 }
1187
1188 #[must_use]
1189 pub fn get_literal_strings(&self) -> Vec<&TAtomic> {
1190 self.types.iter().filter(|a| a.is_known_literal_string()).collect()
1191 }
1192
1193 #[must_use]
1194 pub fn get_literal_string_values(&self) -> Vec<Option<Atom>> {
1195 self.get_literal_strings()
1196 .into_iter()
1197 .map(|atom| match atom {
1198 TAtomic::Scalar(TScalar::String(TString { literal: Some(TStringLiteral::Value(value)), .. })) => {
1199 Some(*value)
1200 }
1201 _ => None,
1202 })
1203 .collect()
1204 }
1205
1206 #[must_use]
1207 pub fn has_literal_float(&self) -> bool {
1208 self.types.iter().any(|atomic| match atomic {
1209 TAtomic::Scalar(scalar) => scalar.is_literal_float(),
1210 _ => false,
1211 })
1212 }
1213
1214 #[must_use]
1215 pub fn has_literal_int(&self) -> bool {
1216 self.types.iter().any(|atomic| match atomic {
1217 TAtomic::Scalar(scalar) => scalar.is_literal_int(),
1218 _ => false,
1219 })
1220 }
1221
1222 #[must_use]
1223 pub fn has_literal_string(&self) -> bool {
1224 self.types.iter().any(|atomic| match atomic {
1225 TAtomic::Scalar(scalar) => scalar.is_known_literal_string(),
1226 _ => false,
1227 })
1228 }
1229
1230 #[must_use]
1231 pub fn has_literal_value(&self) -> bool {
1232 self.types.iter().any(|atomic| match atomic {
1233 TAtomic::Scalar(scalar) => scalar.is_literal_value(),
1234 _ => false,
1235 })
1236 }
1237
1238 #[must_use]
1239 pub fn accepts_false(&self) -> bool {
1240 self.types.iter().any(|t| match t {
1241 TAtomic::GenericParameter(parameter) => parameter.constraint.accepts_false(),
1242 TAtomic::Mixed(mixed) if !mixed.is_truthy() => true,
1243 TAtomic::Scalar(TScalar::Generic | TScalar::Bool(TBool { value: None | Some(false) })) => true,
1244 _ => false,
1245 })
1246 }
1247
1248 #[must_use]
1249 pub fn accepts_null(&self) -> bool {
1250 self.types.iter().any(|t| match t {
1251 TAtomic::GenericParameter(generic_parameter) => generic_parameter.constraint.accepts_null(),
1252 TAtomic::Mixed(mixed) if !mixed.is_non_null() => true,
1253 TAtomic::Null => true,
1254 _ => false,
1255 })
1256 }
1257}
1258
1259impl TType for TUnion {
1260 fn get_child_nodes(&self) -> Vec<TypeRef<'_>> {
1261 self.types.iter().map(TypeRef::Atomic).collect()
1262 }
1263
1264 fn needs_population(&self) -> bool {
1265 !self.flags.contains(UnionFlags::POPULATED) && self.types.iter().any(super::TType::needs_population)
1266 }
1267
1268 fn is_expandable(&self) -> bool {
1269 if self.types.is_empty() {
1270 return true;
1271 }
1272
1273 self.types.iter().any(super::TType::is_expandable)
1274 }
1275
1276 fn is_complex(&self) -> bool {
1277 self.types.len() > 3 || self.types.iter().any(super::TType::is_complex)
1278 }
1279
1280 fn get_id(&self) -> Atom {
1281 let len = self.types.len();
1282
1283 let mut atomic_ids: Vec<Atom> = self
1284 .types
1285 .as_ref()
1286 .iter()
1287 .map(|atomic| {
1288 let id = atomic.get_id();
1289 if atomic.is_generic_parameter() || atomic.has_intersection_types() && len > 1 {
1290 concat_atom!("(", id.as_str(), ")")
1291 } else {
1292 id
1293 }
1294 })
1295 .collect();
1296
1297 if len <= 1 {
1298 return atomic_ids.pop().unwrap_or_else(empty_atom);
1299 }
1300
1301 atomic_ids.sort_unstable();
1302 let mut result = atomic_ids[0];
1303 for id in &atomic_ids[1..] {
1304 result = concat_atom!(result.as_str(), "|", id.as_str());
1305 }
1306
1307 result
1308 }
1309
1310 fn get_pretty_id_with_indent(&self, indent: usize) -> Atom {
1311 let len = self.types.len();
1312
1313 if len <= 1 {
1314 return self.types.first().map_or_else(empty_atom, |atomic| atomic.get_pretty_id_with_indent(indent));
1315 }
1316
1317 if len > 3 {
1319 let mut atomic_ids: Vec<Atom> = self
1320 .types
1321 .as_ref()
1322 .iter()
1323 .map(|atomic| {
1324 let id = atomic.get_pretty_id_with_indent(indent + 2);
1325 if atomic.has_intersection_types() { concat_atom!("(", id.as_str(), ")") } else { id }
1326 })
1327 .collect();
1328
1329 atomic_ids.sort_unstable();
1330
1331 let mut result = String::new();
1332 result += &atomic_ids[0];
1333 for id in &atomic_ids[1..] {
1334 result += "\n";
1335 result += &" ".repeat(indent);
1336 result += "| ";
1337 result += id.as_str();
1338 }
1339
1340 atom(&result)
1341 } else {
1342 let mut atomic_ids: Vec<Atom> = self
1344 .types
1345 .as_ref()
1346 .iter()
1347 .map(|atomic| {
1348 let id = atomic.get_pretty_id_with_indent(indent);
1349 if atomic.has_intersection_types() && len > 1 { concat_atom!("(", id.as_str(), ")") } else { id }
1350 })
1351 .collect();
1352
1353 atomic_ids.sort_unstable();
1354 let mut result = atomic_ids[0];
1355 for id in &atomic_ids[1..] {
1356 result = concat_atom!(result.as_str(), " | ", id.as_str());
1357 }
1358
1359 result
1360 }
1361 }
1362}
1363
1364impl PartialEq for TUnion {
1365 fn eq(&self, other: &TUnion) -> bool {
1366 const SEMANTIC_FLAGS: UnionFlags = UnionFlags::HAD_TEMPLATE
1367 .union(UnionFlags::BY_REFERENCE)
1368 .union(UnionFlags::REFERENCE_FREE)
1369 .union(UnionFlags::POSSIBLY_UNDEFINED_FROM_TRY)
1370 .union(UnionFlags::POSSIBLY_UNDEFINED)
1371 .union(UnionFlags::IGNORE_NULLABLE_ISSUES)
1372 .union(UnionFlags::IGNORE_FALSABLE_ISSUES)
1373 .union(UnionFlags::FROM_TEMPLATE_DEFAULT);
1374
1375 if self.flags.intersection(SEMANTIC_FLAGS) != other.flags.intersection(SEMANTIC_FLAGS) {
1376 return false;
1377 }
1378
1379 let len = self.types.len();
1380 if len != other.types.len() {
1381 return false;
1382 }
1383
1384 for i in 0..len {
1386 let mut has_match = false;
1387 for j in 0..len {
1388 if self.types[i] == other.types[j] {
1389 has_match = true;
1390 break;
1391 }
1392 }
1393
1394 if !has_match {
1395 return false;
1396 }
1397 }
1398
1399 for i in 0..len {
1401 let mut has_match = false;
1402 for j in 0..len {
1403 if other.types[i] == self.types[j] {
1404 has_match = true;
1405 break;
1406 }
1407 }
1408
1409 if !has_match {
1410 return false;
1411 }
1412 }
1413
1414 true
1415 }
1416}
1417
1418pub fn populate_union_type(
1419 unpopulated_union: &mut TUnion,
1420 codebase_symbols: &Symbols,
1421 reference_source: Option<&ReferenceSource>,
1422 symbol_references: &mut SymbolReferences,
1423 force: bool,
1424) {
1425 if unpopulated_union.flags.contains(UnionFlags::POPULATED) && !force {
1426 return;
1427 }
1428
1429 if !unpopulated_union.needs_population() {
1430 return;
1431 }
1432
1433 unpopulated_union.flags.insert(UnionFlags::POPULATED);
1434 let unpopulated_atomics = unpopulated_union.types.to_mut();
1435 for unpopulated_atomic in unpopulated_atomics {
1436 match unpopulated_atomic {
1437 TAtomic::Scalar(TScalar::ClassLikeString(
1438 TClassLikeString::Generic { constraint, .. } | TClassLikeString::OfType { constraint, .. },
1439 )) => {
1440 populate_atomic_type(
1441 Arc::make_mut(constraint),
1442 codebase_symbols,
1443 reference_source,
1444 symbol_references,
1445 force,
1446 );
1447 }
1448 _ => {
1449 populate_atomic_type(unpopulated_atomic, codebase_symbols, reference_source, symbol_references, force);
1450 }
1451 }
1452 }
1453}