1use std::borrow::Cow;
2use std::cell::RefCell;
3use std::sync::Arc;
4
5use std::collections::HashSet;
6
7use foldhash::fast::FixedState;
8use mago_word::Word;
9use mago_word::ascii_lowercase_word;
10
11use crate::identifier::function_like::FunctionLikeIdentifier;
12use crate::metadata::CodebaseMetadata;
13use crate::metadata::function_like::FunctionLikeMetadata;
14use crate::ttype::TType;
15use crate::ttype::atomic::TAtomic;
16use crate::ttype::atomic::alias::TAlias;
17use crate::ttype::atomic::array::TArray;
18use crate::ttype::atomic::array::key::ArrayKey;
19use crate::ttype::atomic::callable::TCallable;
20use crate::ttype::atomic::callable::TCallableSignature;
21use crate::ttype::atomic::callable::parameter::TCallableParameter;
22use crate::ttype::atomic::derived::TDerived;
23use crate::ttype::atomic::derived::index_access::TIndexAccess;
24use crate::ttype::atomic::derived::int_mask::TIntMask;
25use crate::ttype::atomic::derived::int_mask_of::TIntMaskOf;
26use crate::ttype::atomic::derived::key_of::TKeyOf;
27use crate::ttype::atomic::derived::new::TNew;
28use crate::ttype::atomic::derived::properties_of::TPropertiesOf;
29use crate::ttype::atomic::derived::template_type::TTemplateType;
30use crate::ttype::atomic::derived::value_of::TValueOf;
31use crate::ttype::atomic::mixed::TMixed;
32use crate::ttype::atomic::object::TObject;
33use crate::ttype::atomic::object::named::TNamedObject;
34use crate::ttype::atomic::reference::TGlobalReferenceSelector;
35use crate::ttype::atomic::reference::TReference;
36use crate::ttype::atomic::reference::TReferenceMemberSelector;
37use crate::ttype::atomic::scalar::TScalar;
38use crate::ttype::atomic::scalar::class_like_string::TClassLikeString;
39use crate::ttype::atomic::scalar::int::TInteger;
40use crate::ttype::atomic::scalar::string::TString;
41use crate::ttype::atomic::scalar::string::TStringLiteral;
42use crate::ttype::combiner;
43use crate::ttype::union::TUnion;
44
45thread_local! {
46 static EXPANDING_ALIASES: RefCell<HashSet<(Word, Word), FixedState>> = const { RefCell::new(HashSet::with_hasher(FixedState::with_seed(0))) };
49
50 static EXPANDING_OBJECT_PARAMS: RefCell<HashSet<Word, FixedState>> = const { RefCell::new(HashSet::with_hasher(FixedState::with_seed(0))) };
52
53 static EXPANDING_CONSTANTS: RefCell<HashSet<(Word, Word), FixedState>> = const { RefCell::new(HashSet::with_hasher(FixedState::with_seed(0))) };
57}
58
59#[inline]
64pub fn reset_expansion_state() {
65 EXPANDING_ALIASES.with(|set| set.borrow_mut().clear());
66 EXPANDING_OBJECT_PARAMS.with(|set| set.borrow_mut().clear());
67 EXPANDING_CONSTANTS.with(|set| set.borrow_mut().clear());
68}
69
70struct AliasExpansionGuard {
73 class_name: Word,
74 alias_name: Word,
75}
76
77impl AliasExpansionGuard {
78 #[must_use]
79 fn new(class_name: Word, alias_name: Word) -> Self {
80 EXPANDING_ALIASES.with(|set| set.borrow_mut().insert((class_name, alias_name)));
81 Self { class_name, alias_name }
82 }
83}
84
85impl Drop for AliasExpansionGuard {
86 fn drop(&mut self) {
87 EXPANDING_ALIASES.with(|set| set.borrow_mut().remove(&(self.class_name, self.alias_name)));
88 }
89}
90
91struct ObjectParamsExpansionGuard {
93 object_name: Word,
94}
95
96impl ObjectParamsExpansionGuard {
97 #[must_use]
98 fn try_new(object_name: Word) -> Option<Self> {
99 EXPANDING_OBJECT_PARAMS.with(|set| {
100 let mut set = set.borrow_mut();
101 if set.contains(&object_name) {
102 None
103 } else {
104 set.insert(object_name);
105 Some(Self { object_name })
106 }
107 })
108 }
109}
110
111impl Drop for ObjectParamsExpansionGuard {
112 fn drop(&mut self) {
113 EXPANDING_OBJECT_PARAMS.with(|set| set.borrow_mut().remove(&self.object_name));
114 }
115}
116
117struct ConstantExpansionGuard {
124 class_name: Word,
125 constant_name: Word,
126}
127
128impl ConstantExpansionGuard {
129 #[must_use]
130 fn try_new(class_name: Word, constant_name: Word) -> Option<Self> {
131 EXPANDING_CONSTANTS.with(|set| {
132 let mut set = set.borrow_mut();
133 if set.contains(&(class_name, constant_name)) {
134 None
135 } else {
136 set.insert((class_name, constant_name));
137 Some(Self { class_name, constant_name })
138 }
139 })
140 }
141}
142
143impl Drop for ConstantExpansionGuard {
144 fn drop(&mut self) {
145 EXPANDING_CONSTANTS.with(|set| set.borrow_mut().remove(&(self.class_name, self.constant_name)));
146 }
147}
148
149#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
150pub enum StaticClassType {
151 #[default]
152 None,
153 Name(Word),
154 Object(TObject),
155}
156
157#[derive(Debug)]
158pub struct TypeExpansionOptions {
159 pub self_class: Option<Word>,
160 pub static_class_type: StaticClassType,
161 pub parent_class: Option<Word>,
162 pub evaluate_class_constants: bool,
163 pub evaluate_conditional_types: bool,
164 pub function_is_final: bool,
165 pub expand_generic: bool,
166 pub expand_templates: bool,
167}
168
169impl Default for TypeExpansionOptions {
170 fn default() -> Self {
171 Self {
172 self_class: None,
173 static_class_type: StaticClassType::default(),
174 parent_class: None,
175 evaluate_class_constants: true,
176 evaluate_conditional_types: false,
177 function_is_final: false,
178 expand_generic: false,
179 expand_templates: true,
180 }
181 }
182}
183
184pub fn expand_union(codebase: &CodebaseMetadata, return_type: &mut TUnion, options: &TypeExpansionOptions) {
187 if !return_type.is_expandable() {
188 return;
189 }
190
191 let mut types = std::mem::take(&mut return_type.types).into_owned();
192 let mut new_return_type_parts: Vec<TAtomic> = Vec::new();
193 let mut skip_mask: u64 = 0;
194
195 for (i, return_type_part) in types.iter_mut().enumerate() {
196 let mut skip_key = false;
197 expand_atomic(return_type_part, codebase, options, &mut skip_key, &mut new_return_type_parts);
198
199 if skip_key && i < 64 {
200 skip_mask |= 1u64 << i;
201 }
202 }
203
204 if skip_mask != 0 {
205 let mut idx = 0usize;
206 types.retain(|_| {
207 let retain = idx >= 64 || (skip_mask & (1u64 << idx)) == 0;
208 idx += 1;
209 retain
210 });
211
212 new_return_type_parts.append(&mut types);
213
214 if new_return_type_parts.is_empty() {
215 new_return_type_parts.push(TAtomic::Mixed(TMixed::new()));
216 }
217
218 types = if new_return_type_parts.len() > 1 {
219 combiner::combine(new_return_type_parts, codebase, combiner::CombinerOptions::default())
220 } else {
221 new_return_type_parts
222 };
223 } else if types.len() > 1 {
224 types = combiner::combine(types, codebase, combiner::CombinerOptions::default());
225 }
226
227 return_type.types = Cow::Owned(types);
228}
229
230pub(crate) fn expand_atomic(
231 return_type_part: &mut TAtomic,
232 codebase: &CodebaseMetadata,
233 options: &TypeExpansionOptions,
234 skip_key: &mut bool,
235 new_return_type_parts: &mut Vec<TAtomic>,
236) {
237 match return_type_part {
238 TAtomic::Array(array_type) => match array_type {
239 TArray::Keyed(keyed_data) => {
240 if let Some((key_parameter, value_parameter)) = &mut keyed_data.parameters {
241 expand_union(codebase, Arc::make_mut(key_parameter), options);
242 expand_union(codebase, Arc::make_mut(value_parameter), options);
243 }
244
245 if let Some(known_items) = &mut keyed_data.known_items {
246 let needs_key_resolution = known_items.keys().any(|k| k.is_class_like_constant());
248
249 if needs_key_resolution {
250 let old_items = std::mem::take(known_items);
251 for (key, (is_optional, mut value_type)) in old_items {
252 expand_union(codebase, &mut value_type, options);
253 let resolved_key = resolve_array_key(key, codebase, options);
254 known_items.insert(resolved_key, (is_optional, value_type));
255 }
256 } else {
257 for (_, item_type) in known_items.values_mut() {
258 expand_union(codebase, item_type, options);
259 }
260 }
261 }
262 }
263 TArray::List(list_data) => {
264 expand_union(codebase, Arc::make_mut(&mut list_data.element_type), options);
265
266 if let Some(known_elements) = &mut list_data.known_elements {
267 for (_, element_type) in known_elements.values_mut() {
268 expand_union(codebase, element_type, options);
269 }
270 }
271 }
272 },
273 TAtomic::Object(object) => {
274 expand_object(object, codebase, options);
275 }
276 TAtomic::Callable(TCallable::Signature(signature)) => {
277 if let Some(return_type) = signature.get_return_type_mut() {
278 expand_union(codebase, return_type, options);
279 }
280
281 for param in signature.get_parameters_mut() {
282 if let Some(param_type) = param.get_type_signature_mut() {
283 expand_union(codebase, param_type, options);
284 }
285 }
286 }
287 TAtomic::GenericParameter(parameter) => {
288 expand_union(codebase, Arc::make_mut(&mut parameter.constraint), options);
289 }
290 TAtomic::Scalar(TScalar::ClassLikeString(TClassLikeString::OfType { constraint, .. })) => {
291 let mut atomic_return_type_parts = vec![];
292 expand_atomic(Arc::make_mut(constraint), codebase, options, &mut false, &mut atomic_return_type_parts);
293
294 if !atomic_return_type_parts.is_empty() {
295 *Arc::make_mut(constraint) = atomic_return_type_parts.remove(0);
296 }
297 }
298 TAtomic::Reference(TReference::Member { class_like_name, member_selector }) => {
299 *skip_key = true;
300 expand_member_reference(*class_like_name, member_selector, codebase, options, new_return_type_parts);
301 }
302 TAtomic::Reference(TReference::Global { selector }) => {
303 *skip_key = true;
304 expand_global_reference(selector, codebase, options, new_return_type_parts);
305 }
306 TAtomic::Callable(TCallable::Alias(id)) => {
307 if let Some(value) = get_atomic_of_function_like_identifier(id, codebase) {
308 *skip_key = true;
309 new_return_type_parts.push(value);
310 }
311 }
312 TAtomic::Conditional(conditional) => {
313 *skip_key = true;
314
315 let mut then = (*conditional.then).clone();
316 let mut otherwise = (*conditional.otherwise).clone();
317
318 expand_union(codebase, &mut then, options);
319 expand_union(codebase, &mut otherwise, options);
320
321 new_return_type_parts.extend(then.types.into_owned());
322 new_return_type_parts.extend(otherwise.types.into_owned());
323 }
324 TAtomic::Alias(alias) => {
325 *skip_key = true;
326 new_return_type_parts.extend(expand_alias(alias, codebase, options));
327 }
328 TAtomic::Derived(derived) => match derived {
329 TDerived::KeyOf(key_of) => {
330 *skip_key = true;
331 new_return_type_parts.extend(expand_key_of(key_of, codebase, options));
332 }
333 TDerived::ValueOf(value_of) => {
334 *skip_key = true;
335 new_return_type_parts.extend(expand_value_of(value_of, codebase, options));
336 }
337 TDerived::IndexAccess(index_access) => {
338 *skip_key = true;
339 new_return_type_parts.extend(expand_index_access(index_access, codebase, options));
340 }
341 TDerived::IntMask(int_mask) => {
342 *skip_key = true;
343 new_return_type_parts.extend(expand_int_mask(int_mask, codebase, options));
344 }
345 TDerived::IntMaskOf(int_mask_of) => {
346 *skip_key = true;
347 new_return_type_parts.extend(expand_int_mask_of(int_mask_of, codebase, options));
348 }
349 TDerived::PropertiesOf(properties_of) => {
350 *skip_key = true;
351 new_return_type_parts.extend(expand_properties_of(properties_of, codebase, options));
352 }
353 TDerived::New(new_type) => {
354 *skip_key = true;
355 new_return_type_parts.extend(expand_new(new_type, codebase, options));
356 }
357 TDerived::TemplateType(template_type) => {
358 *skip_key = true;
359 new_return_type_parts.extend(expand_template_type(template_type, codebase, options));
360 }
361 },
362 TAtomic::Iterable(iterable) => {
363 expand_union(codebase, Arc::make_mut(&mut iterable.key_type), options);
364 expand_union(codebase, Arc::make_mut(&mut iterable.value_type), options);
365 }
366 _ => {}
367 }
368}
369
370fn resolve_array_key(key: ArrayKey, codebase: &CodebaseMetadata, options: &TypeExpansionOptions) -> ArrayKey {
377 let ArrayKey::ClassLikeConstant { class_like_name, constant_name } = key else {
378 return key;
379 };
380
381 let resolved_class_name = {
383 let name_lc = ascii_lowercase_word(class_like_name.as_bytes());
384 match name_lc.as_bytes() {
385 b"self" => options.self_class.unwrap_or(class_like_name),
386 b"static" | b"$this" => {
387 if let StaticClassType::Name(name) = &options.static_class_type {
388 *name
389 } else {
390 options.self_class.unwrap_or(class_like_name)
391 }
392 }
393 b"parent" => {
394 if let Some(self_class) = options.self_class
395 && let Some(class_metadata) = codebase.get_class_like(self_class.as_bytes())
396 && let Some(parent) = class_metadata.direct_parent_class
397 {
398 parent
399 } else {
400 class_like_name
401 }
402 }
403 _ => class_like_name,
404 }
405 };
406
407 let Some(class_like) = codebase.get_class_like(resolved_class_name.as_bytes()) else {
408 return ArrayKey::ClassLikeConstant { class_like_name, constant_name };
409 };
410
411 if let Some(constant) = class_like.constants.get(&constant_name)
413 && let Some(inferred) = &constant.inferred_type
414 {
415 match inferred {
416 TAtomic::Scalar(TScalar::Integer(TInteger::Literal(i))) => {
417 return ArrayKey::Integer(*i);
418 }
419 TAtomic::Scalar(TScalar::String(TString { literal: Some(TStringLiteral::Value(s)), .. })) => {
420 return ArrayKey::String(*s);
421 }
422 _ => {}
423 }
424 }
425
426 if let Some(enum_case) = class_like.enum_cases.get(&constant_name)
428 && let Some(value_type) = &enum_case.value_type
429 {
430 match value_type {
431 TAtomic::Scalar(TScalar::Integer(TInteger::Literal(i))) => {
432 return ArrayKey::Integer(*i);
433 }
434 TAtomic::Scalar(TScalar::String(TString { literal: Some(TStringLiteral::Value(s)), .. })) => {
435 return ArrayKey::String(*s);
436 }
437 _ => {}
438 }
439 }
440
441 ArrayKey::ClassLikeConstant { class_like_name, constant_name }
443}
444
445#[cold]
446fn expand_member_reference(
447 class_like_name: Word,
448 member_selector: &TReferenceMemberSelector,
449 codebase: &CodebaseMetadata,
450 options: &TypeExpansionOptions,
451 new_return_type_parts: &mut Vec<TAtomic>,
452) {
453 if let TReferenceMemberSelector::Identifier(member_name) = member_selector
454 && member_name.as_bytes().eq_ignore_ascii_case(b"class")
455 {
456 new_return_type_parts.push(TAtomic::Scalar(TScalar::literal_class_string(class_like_name)));
457 return;
458 }
459
460 let Some(class_like) = codebase.get_class_like(class_like_name.as_bytes()) else {
461 new_return_type_parts.push(TAtomic::Mixed(TMixed::new()));
462 return;
463 };
464
465 for (constant_name, constant) in &class_like.constants {
466 if !member_selector.matches(*constant_name) {
467 continue;
468 }
469
470 if let Some(inferred_type) = constant.inferred_type.as_ref() {
471 let Some(_guard) = ConstantExpansionGuard::try_new(class_like_name, *constant_name) else {
472 new_return_type_parts.push(TAtomic::Never);
473 continue;
474 };
475
476 let mut inferred_type = inferred_type.clone();
477 let mut skip_inferred_type = false;
478 expand_atomic(&mut inferred_type, codebase, options, &mut skip_inferred_type, new_return_type_parts);
479
480 if !skip_inferred_type {
481 new_return_type_parts.push(inferred_type);
482 }
483 } else if let Some(type_metadata) = constant.type_metadata.as_ref() {
484 let mut constant_type = type_metadata.type_union.clone();
485 expand_union(codebase, &mut constant_type, options);
486 new_return_type_parts.extend(constant_type.types.into_owned());
487 } else {
488 new_return_type_parts.push(TAtomic::Mixed(TMixed::new()));
489 }
490 }
491
492 for enum_case_name in class_like.enum_cases.keys() {
493 if !member_selector.matches(*enum_case_name) {
494 continue;
495 }
496 new_return_type_parts.push(TAtomic::Object(TObject::new_enum_case(class_like.original_name, *enum_case_name)));
497 }
498
499 if let TReferenceMemberSelector::Identifier(member_name) = member_selector
500 && let Some(type_alias) = class_like.type_aliases.get(member_name)
501 {
502 let mut alias_type = type_alias.type_union.clone();
503 expand_union(codebase, &mut alias_type, options);
504 new_return_type_parts.extend(alias_type.types.into_owned());
505 }
506
507 if new_return_type_parts.is_empty() {
508 new_return_type_parts.push(TAtomic::Mixed(TMixed::new()));
509 }
510}
511
512fn expand_global_reference(
513 selector: &TGlobalReferenceSelector,
514 codebase: &CodebaseMetadata,
515 options: &TypeExpansionOptions,
516 new_return_type_parts: &mut Vec<TAtomic>,
517) {
518 for (constant_name, constant) in &codebase.constants {
519 if !selector.matches(*constant_name) {
520 continue;
521 }
522
523 if let Some(inferred_type) = constant.inferred_type.as_ref() {
524 let mut inferred_type = inferred_type.clone();
525 expand_union(codebase, &mut inferred_type, options);
526 new_return_type_parts.extend(inferred_type.types.into_owned());
527 } else if let Some(type_metadata) = constant.type_metadata.as_ref() {
528 let mut constant_type = type_metadata.type_union.clone();
529 expand_union(codebase, &mut constant_type, options);
530 new_return_type_parts.extend(constant_type.types.into_owned());
531 } else {
532 new_return_type_parts.push(TAtomic::Mixed(TMixed::new()));
533 }
534 }
535
536 if new_return_type_parts.is_empty() {
537 new_return_type_parts.push(TAtomic::Mixed(TMixed::new()));
538 }
539}
540
541fn expand_object(object: &mut TObject, codebase: &CodebaseMetadata, options: &TypeExpansionOptions) {
542 resolve_special_class_names(object, codebase, options);
543
544 let TObject::Named(named) = object else {
545 return;
546 };
547
548 let has_params = named.type_parameters.as_ref().is_some_and(|p| !p.is_empty());
549 let class_metadata = codebase.get_class_like(named.name.as_bytes());
550 let has_required_intersections =
551 class_metadata.map(|m| !m.require_extends.is_empty() || !m.require_implements.is_empty()).unwrap_or(false);
552 let needs_default_params = !has_params && class_metadata.map(|m| !m.template_types.is_empty()).unwrap_or(false);
553
554 if !has_params && !has_required_intersections && !needs_default_params {
555 return;
556 }
557
558 let Some(_guard) = ObjectParamsExpansionGuard::try_new(named.name) else {
559 return;
560 };
561
562 if has_required_intersections && let Some(class_metadata) = class_metadata {
563 for &required in class_metadata.require_extends.iter().chain(&class_metadata.require_implements) {
564 named.add_intersection_type(TAtomic::Object(TObject::Named(TNamedObject::new(required))));
565 }
566 }
567
568 expand_or_fill_type_parameters(named, codebase, options);
569}
570
571#[derive(Copy, Clone, Eq, PartialEq)]
576enum SpecialClassName {
577 None,
578 SelfType,
579 Static,
580 Parent,
581 This,
582}
583
584#[inline]
585fn classify_special_class_name(name: &[u8]) -> SpecialClassName {
586 match name.len() {
587 4 => {
588 if name.eq_ignore_ascii_case(b"self") {
589 SpecialClassName::SelfType
590 } else {
591 SpecialClassName::None
592 }
593 }
594 5 => {
595 if name == b"$this" || name.eq_ignore_ascii_case(b"$this") {
596 SpecialClassName::This
597 } else {
598 SpecialClassName::None
599 }
600 }
601 6 => {
602 if name.eq_ignore_ascii_case(b"static") {
603 SpecialClassName::Static
604 } else if name.eq_ignore_ascii_case(b"parent") {
605 SpecialClassName::Parent
606 } else {
607 SpecialClassName::None
608 }
609 }
610 _ => SpecialClassName::None,
611 }
612}
613
614fn resolve_special_class_names(object: &mut TObject, codebase: &CodebaseMetadata, options: &TypeExpansionOptions) {
616 let TObject::Named(named) = object else {
617 return;
618 };
619
620 let special = classify_special_class_name(named.name.as_bytes());
621 if matches!(special, SpecialClassName::None) && !named.is_static && !named.is_this {
622 return;
623 }
624
625 let needs_static_resolution = matches!(special, SpecialClassName::Static | SpecialClassName::This) || named.is_this;
626
627 if needs_static_resolution && let StaticClassType::Object(TObject::Enum(static_enum)) = &options.static_class_type {
628 *object = TObject::Enum(static_enum.clone());
629 return;
630 }
631
632 let TObject::Named(named) = object else {
633 return;
634 };
635
636 let was_this = named.is_this;
637 match special {
638 SpecialClassName::Static | SpecialClassName::This => {
639 resolve_static_type(named, was_this, false, codebase, options)
640 }
641 SpecialClassName::SelfType => {
642 if let Some(self_class) = options.self_class {
643 named.name = self_class;
644 }
645 }
646 SpecialClassName::Parent => {
647 if let Some(self_class) = options.self_class
648 && let Some(class_metadata) = codebase.get_class_like(self_class.as_bytes())
649 && let Some(parent) = class_metadata.direct_parent_class
650 {
651 named.name = parent;
652 }
653 }
654 SpecialClassName::None if named.is_static => resolve_static_type(named, was_this, true, codebase, options),
655 SpecialClassName::None => {}
656 }
657}
658
659fn resolve_static_type(
664 named: &mut TNamedObject,
665 is_this_type: bool,
666 check_compatibility: bool,
667 codebase: &CodebaseMetadata,
668 options: &TypeExpansionOptions,
669) {
670 match &options.static_class_type {
671 StaticClassType::Object(TObject::Named(static_obj)) => {
672 if check_compatibility && !is_static_type_compatible(named, static_obj, codebase) {
673 return;
674 }
675
676 if let Some(intersections) = &static_obj.intersection_types {
677 named.intersection_types.get_or_insert_with(Vec::new).extend(intersections.iter().cloned());
678 }
679
680 if static_obj.type_parameters.is_some() && should_use_static_type_params(named, static_obj, codebase) {
681 named.type_parameters.clone_from(&static_obj.type_parameters);
682 }
683
684 named.name = static_obj.name;
685 let effectively_final = is_effectively_final(&static_obj.name, codebase, options);
686 named.is_static = !effectively_final;
687 named.is_this = !effectively_final && is_this_type;
688 }
689 StaticClassType::Name(static_class)
690 if (!check_compatibility || codebase.is_instance_of(static_class.as_bytes(), named.name.as_bytes())) =>
691 {
692 named.name = *static_class;
693 let effectively_final = is_effectively_final(static_class, codebase, options);
694 named.is_static = !effectively_final;
695 named.is_this = !effectively_final && is_this_type;
696 }
697 _ => {}
698 }
699}
700
701fn is_effectively_final(class_name: &Word, codebase: &CodebaseMetadata, options: &TypeExpansionOptions) -> bool {
709 if options.function_is_final {
710 return true;
711 }
712
713 codebase.get_class_like(class_name.as_bytes()).is_some_and(|meta| meta.name_span.is_none() || meta.flags.is_final())
714}
715
716fn is_static_type_compatible(named: &TNamedObject, static_obj: &TNamedObject, codebase: &CodebaseMetadata) -> bool {
718 codebase.is_instance_of(static_obj.name.as_bytes(), named.name.as_bytes())
719 || static_obj
720 .intersection_types
721 .iter()
722 .flatten()
723 .filter_map(|t| if let TAtomic::Object(obj) = t { obj.get_name() } else { None })
724 .any(|name| codebase.is_instance_of(name.as_bytes(), named.name.as_bytes()))
725}
726
727fn should_use_static_type_params(named: &TNamedObject, static_obj: &TNamedObject, codebase: &CodebaseMetadata) -> bool {
730 let Some(current_params) = &named.type_parameters else {
731 return true;
732 };
733
734 let Some(class_metadata) = codebase.get_class_like(static_obj.name.as_bytes()) else {
735 return false;
736 };
737
738 let templates = &class_metadata.template_types;
739
740 current_params.len() == templates.len()
741 && current_params.iter().zip(templates.values()).all(|(current, template)| {
742 current == &template.constraint || template.default.as_ref().is_some_and(|default| current == default)
743 })
744}
745
746fn expand_or_fill_type_parameters(
748 named: &mut TNamedObject,
749 codebase: &CodebaseMetadata,
750 options: &TypeExpansionOptions,
751) {
752 if let Some(class_metadata) = codebase.get_class_like(named.name.as_bytes()) {
753 let template_count = class_metadata.template_types.len();
754 let supplied_count = named.type_parameters.as_ref().map_or(0, Vec::len);
755
756 if supplied_count < template_count {
757 let mut params = named.type_parameters.take().unwrap_or_default();
758 params.extend(class_metadata.template_types.values().skip(supplied_count).map(|template| {
759 let mut fallback = template.default.clone().unwrap_or_else(|| template.constraint.clone());
760 fallback.set_from_template_default(true);
761 fallback
762 }));
763 named.type_parameters = Some(params);
764 }
765 }
766
767 if let Some(params) = &mut named.type_parameters {
768 for param in params.iter_mut() {
769 expand_union(codebase, param, options);
770 }
771 }
772}
773
774#[must_use]
775pub fn get_signature_of_function_like_identifier(
776 function_like_identifier: &FunctionLikeIdentifier,
777 codebase: &CodebaseMetadata,
778) -> Option<TCallableSignature> {
779 Some(match function_like_identifier {
780 FunctionLikeIdentifier::Function(name) => {
781 let function_like_metadata = codebase.get_function(name.as_bytes())?;
782
783 get_signature_of_function_like_metadata(
784 function_like_identifier,
785 function_like_metadata,
786 codebase,
787 &TypeExpansionOptions::default(),
788 )
789 }
790 FunctionLikeIdentifier::Closure(name) => {
791 let function_like_metadata = codebase.get_closure(name)?;
792
793 get_signature_of_function_like_metadata(
794 function_like_identifier,
795 function_like_metadata,
796 codebase,
797 &TypeExpansionOptions::default(),
798 )
799 }
800 FunctionLikeIdentifier::Method(classlike_name, method_name) => {
801 let function_like_metadata =
802 codebase.get_declaring_method(classlike_name.as_bytes(), method_name.as_bytes())?;
803
804 get_signature_of_function_like_metadata(
805 function_like_identifier,
806 function_like_metadata,
807 codebase,
808 &TypeExpansionOptions {
809 self_class: Some(*classlike_name),
810 static_class_type: StaticClassType::Name(*classlike_name),
811 ..Default::default()
812 },
813 )
814 }
815 })
816}
817
818#[must_use]
819pub fn get_atomic_of_function_like_identifier(
820 function_like_identifier: &FunctionLikeIdentifier,
821 codebase: &CodebaseMetadata,
822) -> Option<TAtomic> {
823 let signature = get_signature_of_function_like_identifier(function_like_identifier, codebase)?;
824
825 Some(TAtomic::Callable(TCallable::Signature(signature)))
826}
827
828#[must_use]
829pub fn get_signature_of_function_like_metadata(
830 function_like_identifier: &FunctionLikeIdentifier,
831 function_like_metadata: &FunctionLikeMetadata,
832 codebase: &CodebaseMetadata,
833 options: &TypeExpansionOptions,
834) -> TCallableSignature {
835 let parameters: Vec<_> = function_like_metadata
836 .parameters
837 .iter()
838 .map(|parameter_metadata| {
839 let type_signature = if let Some(t) = parameter_metadata.get_type_metadata() {
840 let mut t = t.type_union.clone();
841 expand_union(codebase, &mut t, options);
842 Some(Arc::new(t))
843 } else {
844 None
845 };
846
847 TCallableParameter::new(
848 type_signature,
849 parameter_metadata.flags.is_by_reference(),
850 parameter_metadata.flags.is_variadic(),
851 parameter_metadata.flags.has_default(),
852 )
853 })
854 .collect();
855
856 let return_type = if let Some(type_metadata) = function_like_metadata.return_type_metadata.as_ref() {
857 let mut return_type = type_metadata.type_union.clone();
858 expand_union(codebase, &mut return_type, options);
859 Some(Arc::new(return_type))
860 } else {
861 None
862 };
863
864 let is_closure = matches!(function_like_identifier, FunctionLikeIdentifier::Closure(_));
865 TCallableSignature::new(function_like_metadata.flags.is_pure(), is_closure)
866 .with_parameters(parameters)
867 .with_return_type(return_type)
868 .with_source(Some(*function_like_identifier))
869}
870
871#[cold]
872fn expand_key_of(
873 return_type_key_of: &TKeyOf,
874 codebase: &CodebaseMetadata,
875 options: &TypeExpansionOptions,
876) -> Vec<TAtomic> {
877 let mut target_type = return_type_key_of.get_target_type().clone();
878 expand_union(codebase, &mut target_type, options);
879
880 let Some(new_return_types) = TKeyOf::get_key_of_targets(&target_type.types, codebase, false) else {
881 return vec![TAtomic::Derived(TDerived::KeyOf(return_type_key_of.clone()))];
882 };
883
884 new_return_types.types.into_owned()
885}
886
887#[cold]
888fn expand_value_of(
889 return_type_value_of: &TValueOf,
890 codebase: &CodebaseMetadata,
891 options: &TypeExpansionOptions,
892) -> Vec<TAtomic> {
893 let mut target_type = return_type_value_of.get_target_type().clone();
894 expand_union(codebase, &mut target_type, options);
895
896 let Some(new_return_types) = TValueOf::get_value_of_targets(&target_type.types, codebase, false) else {
897 return vec![TAtomic::Derived(TDerived::ValueOf(return_type_value_of.clone()))];
898 };
899
900 new_return_types.types.into_owned()
901}
902
903#[cold]
904fn expand_index_access(
905 return_type_index_access: &TIndexAccess,
906 codebase: &CodebaseMetadata,
907 options: &TypeExpansionOptions,
908) -> Vec<TAtomic> {
909 let mut target_type = return_type_index_access.get_target_type().clone();
910 expand_union(codebase, &mut target_type, options);
911
912 let mut index_type = return_type_index_access.get_index_type().clone();
913 expand_union(codebase, &mut index_type, options);
914
915 let Some(new_return_types) = TIndexAccess::get_indexed_access_result(&target_type.types, &index_type.types, false)
916 else {
917 return vec![TAtomic::Derived(TDerived::IndexAccess(return_type_index_access.clone()))];
918 };
919
920 new_return_types.types.into_owned()
921}
922
923#[cold]
924fn expand_new(new_type: &TNew, codebase: &CodebaseMetadata, options: &TypeExpansionOptions) -> Vec<TAtomic> {
925 let mut target_type = new_type.get_target_type().clone();
926 expand_union(codebase, &mut target_type, options);
927
928 let Some(new_return_types) = TNew::get_new_targets(&target_type.types, codebase) else {
929 return vec![TAtomic::Derived(TDerived::New(new_type.clone()))];
930 };
931
932 new_return_types.types.into_owned()
933}
934
935#[cold]
936fn expand_template_type(
937 template_type: &TTemplateType,
938 codebase: &CodebaseMetadata,
939 options: &TypeExpansionOptions,
940) -> Vec<TAtomic> {
941 let mut expanded = template_type.clone();
942 expand_union(codebase, expanded.get_object_mut(), options);
943 expand_union(codebase, expanded.get_class_name_mut(), options);
944 expand_union(codebase, expanded.get_template_name_mut(), options);
945
946 let Some(resolved) = expanded.resolve(codebase) else {
947 return vec![TAtomic::Mixed(TMixed::new())];
948 };
949
950 resolved.types.into_owned()
951}
952
953#[cold]
954fn expand_int_mask(int_mask: &TIntMask, codebase: &CodebaseMetadata, options: &TypeExpansionOptions) -> Vec<TAtomic> {
955 let mut literal_values = Vec::new();
956
957 for value in int_mask.get_values() {
958 let mut expanded = value.clone();
959 expand_union(codebase, &mut expanded, options);
960
961 if let Some(int_val) = expanded.get_single_literal_int_value() {
962 literal_values.push(int_val);
963 }
964 }
965
966 if literal_values.is_empty() {
967 return vec![TAtomic::Scalar(TScalar::int())];
968 }
969
970 let combinations = TIntMask::calculate_mask_combinations(&literal_values);
971 combinations.into_iter().map(|v| TAtomic::Scalar(TScalar::literal_int(v))).collect()
972}
973
974#[cold]
975fn expand_int_mask_of(
976 int_mask_of: &TIntMaskOf,
977 codebase: &CodebaseMetadata,
978 options: &TypeExpansionOptions,
979) -> Vec<TAtomic> {
980 let mut target = int_mask_of.get_target_type().clone();
981 expand_union(codebase, &mut target, options);
982
983 let mut literal_values = Vec::new();
984 for atomic in target.types.iter() {
985 if let Some(int_val) = atomic.get_literal_int_value() {
986 literal_values.push(int_val);
987 }
988 }
989
990 if literal_values.is_empty() {
991 return vec![TAtomic::Scalar(TScalar::int())];
992 }
993
994 let combinations = TIntMask::calculate_mask_combinations(&literal_values);
995 combinations.into_iter().map(|v| TAtomic::Scalar(TScalar::literal_int(v))).collect()
996}
997
998#[cold]
999fn expand_properties_of(
1000 properties_of: &TPropertiesOf,
1001 codebase: &CodebaseMetadata,
1002 options: &TypeExpansionOptions,
1003) -> Vec<TAtomic> {
1004 let mut target_type = properties_of.get_target_type().clone();
1005 expand_union(codebase, &mut target_type, options);
1006
1007 let Some(mut keyed_array) =
1008 TPropertiesOf::get_properties_of_targets(&target_type.types, codebase, properties_of.visibility(), false)
1009 else {
1010 return vec![TAtomic::Derived(TDerived::PropertiesOf(properties_of.clone()))];
1011 };
1012
1013 let mut skip_keyed_array = false;
1014 let mut expanded_parts = vec![];
1015 expand_atomic(&mut keyed_array, codebase, options, &mut skip_keyed_array, &mut expanded_parts);
1016 if skip_keyed_array {
1017 expanded_parts
1018 } else {
1019 expanded_parts.push(keyed_array);
1020 expanded_parts
1021 }
1022}
1023
1024#[cold]
1025fn expand_alias(alias: &TAlias, codebase: &CodebaseMetadata, options: &TypeExpansionOptions) -> Vec<TAtomic> {
1026 let class_name = alias.get_class_name();
1027 let alias_name = alias.get_alias_name();
1028
1029 let is_cycle = EXPANDING_ALIASES.with(|set| set.borrow().contains(&(class_name, alias_name)));
1031
1032 if is_cycle {
1033 return vec![TAtomic::Alias(alias.clone())];
1034 }
1035
1036 let Some(mut expanded_union) = alias.resolve(codebase).cloned() else {
1037 return vec![TAtomic::Alias(alias.clone())];
1038 };
1039
1040 let _guard = AliasExpansionGuard::new(class_name, alias_name);
1041
1042 expand_union(codebase, &mut expanded_union, options);
1043
1044 expanded_union.types.into_owned()
1045}
1046
1047#[cfg(test)]
1048#[allow(clippy::unwrap_used, clippy::expect_used)]
1049mod tests {
1050 use super::*;
1051 use mago_allocator::LocalArena;
1052
1053 use std::borrow::Cow;
1054 use std::collections::HashSet;
1055 use std::sync::Arc;
1056
1057 use mago_database::Database;
1058 use mago_database::DatabaseReader;
1059 use mago_database::file::File;
1060
1061 use mago_names::resolver::NameResolver;
1062
1063 use mago_syntax::parser::parse_file;
1064 use mago_word::WordSet;
1065 use mago_word::word;
1066
1067 use crate::metadata::CodebaseMetadata;
1068 use crate::misc::GenericParent;
1069 use crate::populator::populate_codebase;
1070 use crate::reference::SymbolReferences;
1071 use crate::scanner::scan_program;
1072 use crate::ttype::atomic::array::TArray;
1073 use crate::ttype::atomic::array::keyed::TKeyedArray;
1074 use crate::ttype::atomic::array::list::TList;
1075 use crate::ttype::atomic::callable::TCallable;
1076 use crate::ttype::atomic::callable::TCallableSignature;
1077 use crate::ttype::atomic::callable::parameter::TCallableParameter;
1078 use crate::ttype::atomic::conditional::TConditional;
1079 use crate::ttype::atomic::derived::TDerived;
1080 use crate::ttype::atomic::derived::index_access::TIndexAccess;
1081 use crate::ttype::atomic::derived::key_of::TKeyOf;
1082 use crate::ttype::atomic::derived::value_of::TValueOf;
1083 use crate::ttype::atomic::generic::TGenericParameter;
1084 use crate::ttype::atomic::iterable::TIterable;
1085 use crate::ttype::atomic::object::r#enum::TEnum;
1086 use crate::ttype::atomic::object::named::TNamedObject;
1087 use crate::ttype::atomic::reference::TReference;
1088 use crate::ttype::atomic::reference::TReferenceMemberSelector;
1089 use crate::ttype::atomic::scalar::TScalar;
1090 use crate::ttype::atomic::scalar::class_like_string::TClassLikeString;
1091 use crate::ttype::atomic::scalar::class_like_string::TClassLikeStringKind;
1092 use crate::ttype::flags::UnionFlags;
1093 use crate::ttype::get_int;
1094 use crate::ttype::get_mixed;
1095 use crate::ttype::get_never;
1096 use crate::ttype::get_null;
1097 use crate::ttype::get_string;
1098 use crate::ttype::get_void;
1099
1100 fn create_test_codebase(code: &'static str) -> CodebaseMetadata {
1101 let file = File::ephemeral(Cow::Borrowed(b"code.php"), Cow::Borrowed(code.as_bytes()));
1102 let config =
1103 mago_database::DatabaseConfiguration::new(std::path::Path::new("/"), vec![], vec![], vec![], vec![])
1104 .into_static();
1105 let database = Database::single(file, config);
1106
1107 let mut codebase = CodebaseMetadata::new();
1108 let arena = LocalArena::new();
1109 for file in database.files() {
1110 let program = parse_file(&arena, &file);
1111 assert!(!program.has_errors(), "Parse failed: {:?}", program.errors);
1112 let resolved_names = NameResolver::new(&arena).resolve(program);
1113 let program_codebase =
1114 scan_program(&arena, &file, program, &resolved_names, mago_php_version::PHPVersion::LATEST);
1115
1116 codebase.extend(program_codebase);
1117 }
1118
1119 populate_codebase(&mut codebase, &mut SymbolReferences::new(), WordSet::default(), HashSet::default());
1120
1121 codebase
1122 }
1123
1124 fn options_with_self(self_class: &str) -> TypeExpansionOptions {
1125 TypeExpansionOptions { self_class: Some(ascii_lowercase_word(self_class.as_bytes())), ..Default::default() }
1126 }
1127
1128 fn options_with_static(static_class: &str) -> TypeExpansionOptions {
1129 TypeExpansionOptions {
1130 self_class: Some(ascii_lowercase_word(static_class.as_bytes())),
1131 static_class_type: StaticClassType::Name(ascii_lowercase_word(static_class.as_bytes())),
1132 ..Default::default()
1133 }
1134 }
1135
1136 fn options_with_static_object(object: TObject) -> TypeExpansionOptions {
1137 TypeExpansionOptions {
1138 self_class: object.get_name(),
1139 static_class_type: StaticClassType::Object(object),
1140 ..Default::default()
1141 }
1142 }
1143
1144 macro_rules! assert_expands_to {
1145 ($codebase:expr, $input:expr, $expected:expr) => {
1146 assert_expands_to!($codebase, $input, $expected, &TypeExpansionOptions::default())
1147 };
1148 ($codebase:expr, $input:expr, $expected:expr, $options:expr) => {{
1149 let mut actual = $input.clone();
1150 expand_union($codebase, &mut actual, $options);
1151 assert_eq!(
1152 actual.types.as_ref(),
1153 $expected.types.as_ref(),
1154 "Type expansion mismatch.\nInput: {:?}\nExpected: {:?}\nActual: {:?}",
1155 $input,
1156 $expected,
1157 actual
1158 );
1159 }};
1160 }
1161
1162 fn make_self_object() -> TUnion {
1163 TUnion::from_atomic(TAtomic::Object(TObject::Named(TNamedObject::new(word("self")))))
1164 }
1165
1166 fn make_static_object() -> TUnion {
1167 TUnion::from_atomic(TAtomic::Object(TObject::Named(TNamedObject::new(word("static")))))
1168 }
1169
1170 fn make_parent_object() -> TUnion {
1171 TUnion::from_atomic(TAtomic::Object(TObject::Named(TNamedObject::new(word("parent")))))
1172 }
1173
1174 fn make_named_object(name: &str) -> TUnion {
1175 TUnion::from_atomic(TAtomic::Object(TObject::Named(TNamedObject::new(ascii_lowercase_word(name.as_bytes())))))
1176 }
1177
1178 #[test]
1179 fn test_expand_null_type() {
1180 let codebase = CodebaseMetadata::new();
1181 let null_type = get_null();
1182 assert_expands_to!(&codebase, null_type, get_null());
1183 }
1184
1185 #[test]
1186 fn test_expand_void_type() {
1187 let codebase = CodebaseMetadata::new();
1188 let void_type = get_void();
1189 assert_expands_to!(&codebase, void_type, get_void());
1190 }
1191
1192 #[test]
1193 fn test_expand_never_type() {
1194 let codebase = CodebaseMetadata::new();
1195 let never_type = get_never();
1196 assert_expands_to!(&codebase, never_type, get_never());
1197 }
1198
1199 #[test]
1200 fn test_expand_int_type() {
1201 let codebase = CodebaseMetadata::new();
1202 let int_type = get_int();
1203 assert_expands_to!(&codebase, int_type, get_int());
1204 }
1205
1206 #[test]
1207 fn test_expand_mixed_type() {
1208 let codebase = CodebaseMetadata::new();
1209 let mixed_type = get_mixed();
1210 assert_expands_to!(&codebase, mixed_type, get_mixed());
1211 }
1212
1213 #[test]
1214 fn test_expand_keyed_array_with_self_key() {
1215 let code = "<?php class Foo {}";
1216 let codebase = create_test_codebase(code);
1217
1218 let mut keyed = TKeyedArray::new();
1219 keyed.parameters = Some((Arc::new(make_self_object()), Arc::new(get_int())));
1220 let input = TUnion::from_atomic(TAtomic::Array(TArray::Keyed(keyed)));
1221
1222 let options = options_with_self("Foo");
1223 let mut actual = input;
1224 expand_union(&codebase, &mut actual, &options);
1225
1226 if let TAtomic::Array(TArray::Keyed(keyed)) = &actual.types[0]
1227 && let Some((key, _)) = &keyed.parameters
1228 {
1229 assert!(key.types.iter().any(|t| {
1230 if let TAtomic::Object(TObject::Named(named)) = t {
1231 named.name == ascii_lowercase_word(b"foo")
1232 } else {
1233 false
1234 }
1235 }));
1236 }
1237 }
1238
1239 #[test]
1240 fn test_expand_keyed_array_with_self_value() {
1241 let code = "<?php class Foo {}";
1242 let codebase = create_test_codebase(code);
1243
1244 let mut keyed = TKeyedArray::new();
1245 keyed.parameters = Some((Arc::new(get_string()), Arc::new(make_self_object())));
1246 let input = TUnion::from_atomic(TAtomic::Array(TArray::Keyed(keyed)));
1247
1248 let options = options_with_self("Foo");
1249 let mut actual = input;
1250 expand_union(&codebase, &mut actual, &options);
1251
1252 if let TAtomic::Array(TArray::Keyed(keyed)) = &actual.types[0]
1253 && let Some((_, value)) = &keyed.parameters
1254 {
1255 assert!(value.types.iter().any(|t| {
1256 if let TAtomic::Object(TObject::Named(named)) = t {
1257 named.name == ascii_lowercase_word(b"foo")
1258 } else {
1259 false
1260 }
1261 }));
1262 }
1263 }
1264
1265 #[test]
1266 fn test_expand_keyed_array_known_items() {
1267 let code = "<?php class Foo {}";
1268 let codebase = create_test_codebase(code);
1269
1270 use crate::ttype::atomic::array::key::ArrayKey;
1271 use std::collections::BTreeMap;
1272
1273 let mut keyed = TKeyedArray::new();
1274 let mut known_items = BTreeMap::new();
1275 known_items.insert(ArrayKey::String(word("key")), (false, make_self_object()));
1276 keyed.known_items = Some(known_items);
1277 let input = TUnion::from_atomic(TAtomic::Array(TArray::Keyed(keyed)));
1278
1279 let options = options_with_self("Foo");
1280 let mut actual = input;
1281 expand_union(&codebase, &mut actual, &options);
1282
1283 if let TAtomic::Array(TArray::Keyed(keyed)) = &actual.types[0]
1284 && let Some(items) = &keyed.known_items
1285 {
1286 let (_, item_type) = items.get(&ArrayKey::String(word("key"))).unwrap();
1287 assert!(item_type.types.iter().any(|t| {
1288 if let TAtomic::Object(TObject::Named(named)) = t {
1289 named.name == ascii_lowercase_word(b"foo")
1290 } else {
1291 false
1292 }
1293 }));
1294 }
1295 }
1296
1297 #[test]
1298 fn test_expand_list_with_self_element() {
1299 let code = "<?php class Foo {}";
1300 let codebase = create_test_codebase(code);
1301
1302 let list = TList::new(Arc::new(make_self_object()));
1303 let input = TUnion::from_atomic(TAtomic::Array(TArray::List(list)));
1304
1305 let options = options_with_self("Foo");
1306 let mut actual = input;
1307 expand_union(&codebase, &mut actual, &options);
1308
1309 if let TAtomic::Array(TArray::List(list)) = &actual.types[0] {
1310 assert!(list.element_type.types.iter().any(|t| {
1311 if let TAtomic::Object(TObject::Named(named)) = t {
1312 named.name == ascii_lowercase_word(b"foo")
1313 } else {
1314 false
1315 }
1316 }));
1317 }
1318 }
1319
1320 #[test]
1321 fn test_expand_list_known_elements() {
1322 let code = "<?php class Foo {}";
1323 let codebase = create_test_codebase(code);
1324
1325 use std::collections::BTreeMap;
1326
1327 let mut list = TList::new(Arc::new(get_mixed()));
1328 let mut known_elements = BTreeMap::new();
1329 known_elements.insert(0, (false, make_self_object()));
1330 list.known_elements = Some(known_elements);
1331 let input = TUnion::from_atomic(TAtomic::Array(TArray::List(list)));
1332
1333 let options = options_with_self("Foo");
1334 let mut actual = input;
1335 expand_union(&codebase, &mut actual, &options);
1336
1337 if let TAtomic::Array(TArray::List(list)) = &actual.types[0]
1338 && let Some(elements) = &list.known_elements
1339 {
1340 let (_, element_type) = elements.get(&0).unwrap();
1341 assert!(element_type.types.iter().any(|t| {
1342 if let TAtomic::Object(TObject::Named(named)) = t {
1343 named.name == ascii_lowercase_word(b"foo")
1344 } else {
1345 false
1346 }
1347 }));
1348 }
1349 }
1350
1351 #[test]
1352 fn test_expand_nested_array() {
1353 let code = "<?php class Foo {}";
1354 let codebase = create_test_codebase(code);
1355
1356 let inner_list = TList::new(Arc::new(make_self_object()));
1357 let inner_array = TUnion::from_atomic(TAtomic::Array(TArray::List(inner_list)));
1358
1359 let mut outer = TKeyedArray::new();
1360 outer.parameters = Some((Arc::new(make_self_object()), Arc::new(inner_array)));
1361 let input = TUnion::from_atomic(TAtomic::Array(TArray::Keyed(outer)));
1362
1363 let options = options_with_self("Foo");
1364 let mut actual = input;
1365 expand_union(&codebase, &mut actual, &options);
1366
1367 if let TAtomic::Array(TArray::Keyed(keyed)) = &actual.types[0]
1368 && let Some((key, value)) = &keyed.parameters
1369 {
1370 assert!(key.types.iter().any(|t| {
1371 if let TAtomic::Object(TObject::Named(named)) = t {
1372 named.name == ascii_lowercase_word(b"foo")
1373 } else {
1374 false
1375 }
1376 }));
1377 if let TAtomic::Array(TArray::List(inner)) = &value.types[0] {
1378 assert!(inner.element_type.types.iter().any(|t| {
1379 if let TAtomic::Object(TObject::Named(named)) = t {
1380 named.name == ascii_lowercase_word(b"foo")
1381 } else {
1382 false
1383 }
1384 }));
1385 }
1386 }
1387 }
1388
1389 #[test]
1390 fn test_expand_empty_array() {
1391 let codebase = CodebaseMetadata::new();
1392 let keyed = TKeyedArray::new();
1393 let input = TUnion::from_atomic(TAtomic::Array(TArray::Keyed(keyed.clone())));
1394 let expected = TUnion::from_atomic(TAtomic::Array(TArray::Keyed(keyed)));
1395 assert_expands_to!(&codebase, input, expected);
1396 }
1397
1398 #[test]
1399 fn test_expand_non_empty_list() {
1400 let code = "<?php class Foo {}";
1401 let codebase = create_test_codebase(code);
1402
1403 let mut list = TList::new(Arc::new(make_self_object()));
1404 list.non_empty = true;
1405 let input = TUnion::from_atomic(TAtomic::Array(TArray::List(list)));
1406
1407 let options = options_with_self("Foo");
1408 let mut actual = input;
1409 expand_union(&codebase, &mut actual, &options);
1410
1411 if let TAtomic::Array(TArray::List(list)) = &actual.types[0] {
1412 assert!(list.non_empty);
1413 assert!(list.element_type.types.iter().any(|t| {
1414 if let TAtomic::Object(TObject::Named(named)) = t {
1415 named.name == ascii_lowercase_word(b"foo")
1416 } else {
1417 false
1418 }
1419 }));
1420 }
1421 }
1422
1423 #[test]
1424 fn test_expand_self_to_class_name() {
1425 let code = "<?php class Foo {}";
1426 let codebase = create_test_codebase(code);
1427
1428 let input = make_self_object();
1429 let options = options_with_self("Foo");
1430 let mut actual = input;
1431 expand_union(&codebase, &mut actual, &options);
1432
1433 assert!(actual.types.iter().any(|t| {
1434 if let TAtomic::Object(TObject::Named(named)) = t {
1435 named.name == ascii_lowercase_word(b"foo")
1436 } else {
1437 false
1438 }
1439 }));
1440 }
1441
1442 #[test]
1443 fn test_expand_static_to_class_name() {
1444 let code = "<?php class Foo {}";
1445 let codebase = create_test_codebase(code);
1446
1447 let input = make_static_object();
1448 let options = options_with_static("Foo");
1449 let mut actual = input;
1450 expand_union(&codebase, &mut actual, &options);
1451
1452 assert!(actual.types.iter().any(|t| {
1453 if let TAtomic::Object(TObject::Named(named)) = t {
1454 named.name == ascii_lowercase_word(b"foo")
1455 } else {
1456 false
1457 }
1458 }));
1459 }
1460
1461 #[test]
1462 fn test_expand_static_with_object_type() {
1463 let code = "<?php class Foo {}";
1464 let codebase = create_test_codebase(code);
1465
1466 let input = make_static_object();
1467 let static_obj = TObject::Named(TNamedObject::new(ascii_lowercase_word(b"foo")));
1468 let options = options_with_static_object(static_obj);
1469 let mut actual = input;
1470 expand_union(&codebase, &mut actual, &options);
1471
1472 assert!(actual.types.iter().any(|t| {
1473 if let TAtomic::Object(TObject::Named(named)) = t {
1474 named.name == ascii_lowercase_word(b"foo") && named.is_static && !named.is_this
1475 } else {
1476 false
1477 }
1478 }));
1479 }
1480
1481 #[test]
1482 fn test_expand_static_with_enum_type() {
1483 let code = "<?php enum Status { case Active; case Inactive; }";
1484 let codebase = create_test_codebase(code);
1485
1486 let input = make_static_object();
1487 let static_enum = TObject::Enum(TEnum::new(ascii_lowercase_word(b"status")));
1488 let options = options_with_static_object(static_enum);
1489 let mut actual = input;
1490 expand_union(&codebase, &mut actual, &options);
1491
1492 assert!(actual.types.iter().any(|t| matches!(t, TAtomic::Object(TObject::Enum(_)))));
1493 }
1494
1495 #[test]
1496 fn test_expand_parent_to_parent_class() {
1497 let code = "<?php
1498 class BaseClass {}
1499 class ChildClass extends BaseClass {}
1500 ";
1501 let codebase = create_test_codebase(code);
1502
1503 let input = make_parent_object();
1504 let options = options_with_self("ChildClass");
1505 let mut actual = input;
1506 expand_union(&codebase, &mut actual, &options);
1507
1508 assert!(actual.types.iter().any(|t| {
1509 if let TAtomic::Object(TObject::Named(named)) = t {
1510 named.name == ascii_lowercase_word(b"baseclass")
1511 } else {
1512 false
1513 }
1514 }));
1515 }
1516
1517 #[test]
1518 fn test_expand_parent_without_parent_class() {
1519 let code = "<?php class Foo {}";
1520 let codebase = create_test_codebase(code);
1521
1522 let input = make_parent_object();
1523 let options = options_with_self("Foo");
1524 let mut actual = input;
1525 expand_union(&codebase, &mut actual, &options);
1526
1527 assert!(actual.types.iter().any(|t| {
1528 if let TAtomic::Object(TObject::Named(named)) = t { named.name == word("parent") } else { false }
1529 }));
1530 }
1531
1532 #[test]
1533 fn test_expand_this_variable() {
1534 let code = "<?php class Foo {}";
1535 let codebase = create_test_codebase(code);
1536
1537 let input = TUnion::from_atomic(TAtomic::Object(TObject::Named(TNamedObject::new_this(word("$this")))));
1538 let options = options_with_static("Foo");
1539 let mut actual = input;
1540 expand_union(&codebase, &mut actual, &options);
1541
1542 assert!(actual.types.iter().any(|t| {
1543 if let TAtomic::Object(TObject::Named(named)) = t {
1544 named.name == ascii_lowercase_word(b"foo")
1545 } else {
1546 false
1547 }
1548 }));
1549 }
1550
1551 #[test]
1552 fn test_expand_this_with_final_function() {
1553 let code = "<?php class Foo {}";
1554 let codebase = create_test_codebase(code);
1555
1556 let input = make_static_object();
1557 let options = TypeExpansionOptions {
1558 self_class: Some(ascii_lowercase_word(b"foo")),
1559 static_class_type: StaticClassType::Name(ascii_lowercase_word(b"foo")),
1560 function_is_final: true,
1561 ..Default::default()
1562 };
1563 let mut actual = input;
1564 expand_union(&codebase, &mut actual, &options);
1565
1566 assert!(actual.types.iter().any(|t| {
1567 if let TAtomic::Object(TObject::Named(named)) = t {
1568 named.name == ascii_lowercase_word(b"foo") && !named.is_this
1569 } else {
1570 false
1571 }
1572 }));
1573 }
1574
1575 #[test]
1576 fn test_expand_object_with_type_parameters() {
1577 let code = "<?php class Container {}";
1578 let codebase = create_test_codebase(code);
1579
1580 let named =
1581 TNamedObject::new_with_type_parameters(ascii_lowercase_word(b"container"), Some(vec![make_self_object()]));
1582 let input = TUnion::from_atomic(TAtomic::Object(TObject::Named(named)));
1583
1584 let options = options_with_self("Foo");
1585 let mut actual = input;
1586 expand_union(&codebase, &mut actual, &options);
1587
1588 if let TAtomic::Object(TObject::Named(named)) = &actual.types[0]
1589 && let Some(params) = &named.type_parameters
1590 {
1591 assert!(params[0].types.iter().any(|t| {
1592 if let TAtomic::Object(TObject::Named(named)) = t {
1593 named.name == ascii_lowercase_word(b"foo")
1594 } else {
1595 false
1596 }
1597 }));
1598 }
1599 }
1600
1601 #[test]
1602 fn test_expand_object_gets_default_type_params() {
1603 let code = "<?php
1604 /** @template T */
1605 class Container {}
1606 ";
1607 let codebase = create_test_codebase(code);
1608
1609 let named = TNamedObject::new(ascii_lowercase_word(b"container"));
1610 let input = TUnion::from_atomic(TAtomic::Object(TObject::Named(named)));
1611
1612 let mut actual = input;
1613 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
1614
1615 if let TAtomic::Object(TObject::Named(named)) = &actual.types[0] {
1616 assert!(named.type_parameters.is_some());
1617 }
1618 }
1619
1620 #[test]
1621 fn test_expand_object_intersection_from_static() {
1622 let code = "<?php
1623 interface Stringable {}
1624 class Foo implements Stringable {}
1625 ";
1626 let codebase = create_test_codebase(code);
1627
1628 let input = make_static_object();
1629
1630 let mut static_named = TNamedObject::new(ascii_lowercase_word(b"foo"));
1631 static_named.intersection_types =
1632 Some(vec![TAtomic::Object(TObject::Named(TNamedObject::new(ascii_lowercase_word(b"stringable"))))]);
1633 let static_obj = TObject::Named(static_named);
1634 let options = options_with_static_object(static_obj);
1635
1636 let mut actual = input;
1637 expand_union(&codebase, &mut actual, &options);
1638
1639 if let TAtomic::Object(TObject::Named(named)) = &actual.types[0] {
1640 assert!(named.intersection_types.is_some());
1641 }
1642 }
1643
1644 #[test]
1645 fn test_expand_self_without_self_class_option() {
1646 let codebase = CodebaseMetadata::new();
1647
1648 let input = make_self_object();
1649 let mut actual = input;
1650 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
1651
1652 assert!(actual.types.iter().any(|t| {
1653 if let TAtomic::Object(TObject::Named(named)) = t { named.name == word("self") } else { false }
1654 }));
1655 }
1656
1657 #[test]
1658 fn test_expand_callable_return_type() {
1659 let code = "<?php class Foo {}";
1660 let codebase = create_test_codebase(code);
1661
1662 let sig = TCallableSignature::new(false, false).with_return_type(Some(Arc::new(make_self_object())));
1663 let input = TUnion::from_atomic(TAtomic::Callable(TCallable::Signature(sig)));
1664
1665 let options = options_with_self("Foo");
1666 let mut actual = input;
1667 expand_union(&codebase, &mut actual, &options);
1668
1669 if let TAtomic::Callable(TCallable::Signature(sig)) = &actual.types[0]
1670 && let Some(ret) = sig.get_return_type()
1671 {
1672 assert!(ret.types.iter().any(|t| {
1673 if let TAtomic::Object(TObject::Named(named)) = t {
1674 named.name == ascii_lowercase_word(b"foo")
1675 } else {
1676 false
1677 }
1678 }));
1679 }
1680 }
1681
1682 #[test]
1683 fn test_expand_callable_parameter_types() {
1684 let code = "<?php class Foo {}";
1685 let codebase = create_test_codebase(code);
1686
1687 let param = TCallableParameter::new(Some(Arc::new(make_self_object())), false, false, false);
1688 let sig = TCallableSignature::new(false, false).with_parameters(vec![param]);
1689 let input = TUnion::from_atomic(TAtomic::Callable(TCallable::Signature(sig)));
1690
1691 let options = options_with_self("Foo");
1692 let mut actual = input;
1693 expand_union(&codebase, &mut actual, &options);
1694
1695 if let TAtomic::Callable(TCallable::Signature(sig)) = &actual.types[0]
1696 && let Some(param) = sig.get_parameters().first()
1697 && let Some(param_type) = param.get_type_signature()
1698 {
1699 assert!(param_type.types.iter().any(|t| {
1700 if let TAtomic::Object(TObject::Named(named)) = t {
1701 named.name == ascii_lowercase_word(b"foo")
1702 } else {
1703 false
1704 }
1705 }));
1706 }
1707 }
1708
1709 #[test]
1710 fn test_expand_callable_alias_to_function() {
1711 let code = "<?php
1712 function myFunc(): int { return 1; }
1713 ";
1714 let codebase = create_test_codebase(code);
1715
1716 let alias = TCallable::Alias(FunctionLikeIdentifier::Function(ascii_lowercase_word(b"myfunc")));
1717 let input = TUnion::from_atomic(TAtomic::Callable(alias));
1718
1719 let mut actual = input;
1720 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
1721
1722 assert!(actual.types.iter().any(|t| matches!(t, TAtomic::Callable(TCallable::Signature(_)))));
1723 }
1724
1725 #[test]
1726 fn test_expand_callable_alias_to_method() {
1727 let code = "<?php
1728 class Foo {
1729 public function bar(): int { return 1; }
1730 }
1731 ";
1732 let codebase = create_test_codebase(code);
1733
1734 let alias = TCallable::Alias(FunctionLikeIdentifier::Method(
1735 ascii_lowercase_word(b"foo"),
1736 ascii_lowercase_word(b"bar"),
1737 ));
1738 let input = TUnion::from_atomic(TAtomic::Callable(alias));
1739
1740 let mut actual = input;
1741 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
1742
1743 assert!(actual.types.iter().any(|t| matches!(t, TAtomic::Callable(TCallable::Signature(_)))));
1744 }
1745
1746 #[test]
1747 fn test_expand_callable_alias_unknown() {
1748 let codebase = CodebaseMetadata::new();
1749
1750 let alias = TCallable::Alias(FunctionLikeIdentifier::Function(word("nonexistent")));
1751 let input = TUnion::from_atomic(TAtomic::Callable(alias));
1752
1753 let mut actual = input;
1754 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
1755
1756 assert!(actual.types.iter().any(|t| matches!(t, TAtomic::Callable(TCallable::Alias(_)))));
1757 }
1758
1759 #[test]
1760 fn test_expand_closure_signature() {
1761 let code = "<?php class Foo {}";
1762 let codebase = create_test_codebase(code);
1763
1764 let sig = TCallableSignature::new(false, true).with_return_type(Some(Arc::new(make_self_object())));
1765 let input = TUnion::from_atomic(TAtomic::Callable(TCallable::Signature(sig)));
1766
1767 let options = options_with_self("Foo");
1768 let mut actual = input;
1769 expand_union(&codebase, &mut actual, &options);
1770
1771 if let TAtomic::Callable(TCallable::Signature(sig)) = &actual.types[0]
1772 && let Some(ret) = sig.get_return_type()
1773 {
1774 assert!(ret.types.iter().any(|t| {
1775 if let TAtomic::Object(TObject::Named(named)) = t {
1776 named.name == ascii_lowercase_word(b"foo")
1777 } else {
1778 false
1779 }
1780 }));
1781 }
1782 }
1783
1784 #[test]
1785 fn test_expand_generic_parameter_constraint() {
1786 let code = "<?php class Foo {}";
1787 let codebase = create_test_codebase(code);
1788
1789 let generic = TGenericParameter::new(
1790 word("T"),
1791 Arc::new(make_self_object()),
1792 GenericParent::ClassLike(ascii_lowercase_word(b"foo")),
1793 );
1794 let input = TUnion::from_atomic(TAtomic::GenericParameter(generic));
1795
1796 let options = options_with_self("Foo");
1797 let mut actual = input;
1798 expand_union(&codebase, &mut actual, &options);
1799
1800 if let TAtomic::GenericParameter(param) = &actual.types[0] {
1801 assert!(param.constraint.types.iter().any(|t| {
1802 if let TAtomic::Object(TObject::Named(named)) = t {
1803 named.name == ascii_lowercase_word(b"foo")
1804 } else {
1805 false
1806 }
1807 }));
1808 }
1809 }
1810
1811 #[test]
1812 fn test_expand_nested_generic_constraint() {
1813 let code = "<?php class Foo {} class Bar {}";
1814 let codebase = create_test_codebase(code);
1815
1816 let container =
1817 TNamedObject::new_with_type_parameters(ascii_lowercase_word(b"container"), Some(vec![make_self_object()]));
1818 let constraint = TUnion::from_atomic(TAtomic::Object(TObject::Named(container)));
1819
1820 let generic = TGenericParameter::new(
1821 word("T"),
1822 Arc::new(constraint),
1823 GenericParent::ClassLike(ascii_lowercase_word(b"bar")),
1824 );
1825 let input = TUnion::from_atomic(TAtomic::GenericParameter(generic));
1826
1827 let options = options_with_self("Foo");
1828 let mut actual = input;
1829 expand_union(&codebase, &mut actual, &options);
1830
1831 if let TAtomic::GenericParameter(param) = &actual.types[0]
1832 && let TAtomic::Object(TObject::Named(named)) = ¶m.constraint.types[0]
1833 && let Some(params) = &named.type_parameters
1834 {
1835 assert!(params[0].types.iter().any(|t| {
1836 if let TAtomic::Object(TObject::Named(named)) = t {
1837 named.name == ascii_lowercase_word(b"foo")
1838 } else {
1839 false
1840 }
1841 }));
1842 }
1843 }
1844
1845 #[test]
1846 fn test_expand_generic_with_intersection() {
1847 let code = "<?php
1848 interface Stringable {}
1849 class Foo {}
1850 ";
1851 let codebase = create_test_codebase(code);
1852
1853 let mut generic = TGenericParameter::new(
1854 word("T"),
1855 Arc::new(make_self_object()),
1856 GenericParent::ClassLike(ascii_lowercase_word(b"foo")),
1857 );
1858 generic.intersection_types =
1859 Some(vec![TAtomic::Object(TObject::Named(TNamedObject::new(ascii_lowercase_word(b"stringable"))))]);
1860 let input = TUnion::from_atomic(TAtomic::GenericParameter(generic));
1861
1862 let options = options_with_self("Foo");
1863 let mut actual = input;
1864 expand_union(&codebase, &mut actual, &options);
1865
1866 if let TAtomic::GenericParameter(param) = &actual.types[0] {
1867 assert!(param.intersection_types.is_some());
1868 assert!(param.constraint.types.iter().any(|t| {
1869 if let TAtomic::Object(TObject::Named(named)) = t {
1870 named.name == ascii_lowercase_word(b"foo")
1871 } else {
1872 false
1873 }
1874 }));
1875 }
1876 }
1877
1878 #[test]
1879 fn test_expand_class_string_of_self() {
1880 let code = "<?php class Foo {}";
1881 let codebase = create_test_codebase(code);
1882
1883 let constraint = Arc::new(TAtomic::Object(TObject::Named(TNamedObject::new(word("self")))));
1884 let class_string = TClassLikeString::OfType { kind: TClassLikeStringKind::Class, constraint };
1885 let input = TUnion::from_atomic(TAtomic::Scalar(TScalar::ClassLikeString(class_string)));
1886
1887 let options = options_with_self("Foo");
1888 let mut actual = input;
1889 expand_union(&codebase, &mut actual, &options);
1890
1891 if let TAtomic::Scalar(TScalar::ClassLikeString(TClassLikeString::OfType { constraint, .. })) = &actual.types[0]
1892 && let TAtomic::Object(TObject::Named(named)) = constraint.as_ref()
1893 {
1894 assert_eq!(named.name, ascii_lowercase_word(b"foo"));
1895 }
1896 }
1897
1898 #[test]
1899 fn test_expand_class_string_of_static() {
1900 let code = "<?php class Foo {}";
1901 let codebase = create_test_codebase(code);
1902
1903 let constraint = Arc::new(TAtomic::Object(TObject::Named(TNamedObject::new(word("static")))));
1904 let class_string = TClassLikeString::OfType { kind: TClassLikeStringKind::Class, constraint };
1905 let input = TUnion::from_atomic(TAtomic::Scalar(TScalar::ClassLikeString(class_string)));
1906
1907 let options = options_with_static("Foo");
1908 let mut actual = input;
1909 expand_union(&codebase, &mut actual, &options);
1910
1911 if let TAtomic::Scalar(TScalar::ClassLikeString(TClassLikeString::OfType { constraint, .. })) = &actual.types[0]
1912 && let TAtomic::Object(TObject::Named(named)) = constraint.as_ref()
1913 {
1914 assert_eq!(named.name, ascii_lowercase_word(b"foo"));
1915 }
1916 }
1917
1918 #[test]
1919 fn test_expand_interface_string_of_type() {
1920 let code = "<?php interface MyInterface {}";
1921 let codebase = create_test_codebase(code);
1922
1923 let constraint = Arc::new(TAtomic::Object(TObject::Named(TNamedObject::new(word("self")))));
1924 let class_string = TClassLikeString::OfType { kind: TClassLikeStringKind::Interface, constraint };
1925 let input = TUnion::from_atomic(TAtomic::Scalar(TScalar::ClassLikeString(class_string)));
1926
1927 let options = options_with_self("MyInterface");
1928 let mut actual = input;
1929 expand_union(&codebase, &mut actual, &options);
1930
1931 if let TAtomic::Scalar(TScalar::ClassLikeString(TClassLikeString::OfType { kind, constraint })) =
1932 &actual.types[0]
1933 {
1934 assert!(matches!(kind, TClassLikeStringKind::Interface));
1935 if let TAtomic::Object(TObject::Named(named)) = constraint.as_ref() {
1936 assert_eq!(named.name, ascii_lowercase_word(b"myinterface"));
1937 }
1938 }
1939 }
1940
1941 #[test]
1942 fn test_expand_member_reference_wildcard_constants() {
1943 let code = "<?php
1944 class Foo {
1945 public const A = 1;
1946 public const B = 2;
1947 }
1948 ";
1949 let codebase = create_test_codebase(code);
1950
1951 let reference = TReference::new_member(ascii_lowercase_word(b"foo"), TReferenceMemberSelector::Wildcard);
1952 let input = TUnion::from_atomic(TAtomic::Reference(reference));
1953
1954 let mut actual = input;
1955 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
1956
1957 assert!(!actual.types.is_empty());
1958 }
1959
1960 #[test]
1961 fn test_expand_member_reference_wildcard_enum_cases() {
1962 let code = "<?php
1963 enum Status {
1964 case Active;
1965 case Inactive;
1966 }
1967 ";
1968 let codebase = create_test_codebase(code);
1969
1970 let reference = TReference::new_member(ascii_lowercase_word(b"status"), TReferenceMemberSelector::Wildcard);
1971 let input = TUnion::from_atomic(TAtomic::Reference(reference));
1972
1973 let mut actual = input;
1974 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
1975
1976 assert_eq!(actual.types.len(), 2);
1977 assert!(actual.types.iter().all(|t| matches!(t, TAtomic::Object(TObject::Enum(_)))));
1978 }
1979
1980 #[test]
1981 fn test_expand_member_reference_starts_with() {
1982 let code = "<?php
1983 class Foo {
1984 public const STATUS_ACTIVE = 1;
1985 public const STATUS_INACTIVE = 2;
1986 public const OTHER = 3;
1987 }
1988 ";
1989 let codebase = create_test_codebase(code);
1990
1991 let reference =
1992 TReference::new_member(ascii_lowercase_word(b"foo"), TReferenceMemberSelector::StartsWith(word("STATUS_")));
1993 let input = TUnion::from_atomic(TAtomic::Reference(reference));
1994
1995 let mut actual = input;
1996 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
1997
1998 assert!(!actual.types.is_empty());
1999 }
2000
2001 #[test]
2002 fn test_expand_member_reference_ends_with() {
2003 let code = "<?php
2004 class Foo {
2005 public const READ_ERROR = 1;
2006 public const WRITE_ERROR = 2;
2007 public const SUCCESS = 0;
2008 }
2009 ";
2010 let codebase = create_test_codebase(code);
2011
2012 let reference =
2013 TReference::new_member(ascii_lowercase_word(b"foo"), TReferenceMemberSelector::EndsWith(word("_ERROR")));
2014 let input = TUnion::from_atomic(TAtomic::Reference(reference));
2015
2016 let mut actual = input;
2017 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2018
2019 assert!(!actual.types.is_empty());
2020 }
2021
2022 #[test]
2023 fn test_expand_member_reference_identifier_constant() {
2024 let code = "<?php
2025 class Foo {
2026 public const BAR = 42;
2027 }
2028 ";
2029 let codebase = create_test_codebase(code);
2030
2031 let reference =
2032 TReference::new_member(ascii_lowercase_word(b"foo"), TReferenceMemberSelector::Identifier(word("BAR")));
2033 let input = TUnion::from_atomic(TAtomic::Reference(reference));
2034
2035 let mut actual = input;
2036 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2037
2038 assert_eq!(actual.types.len(), 1);
2039 }
2040
2041 #[test]
2042 fn test_expand_member_reference_identifier_enum_case() {
2043 let code = "<?php
2044 enum Status {
2045 case Active;
2046 }
2047 ";
2048 let codebase = create_test_codebase(code);
2049
2050 let reference = TReference::new_member(
2051 ascii_lowercase_word(b"status"),
2052 TReferenceMemberSelector::Identifier(word("Active")),
2053 );
2054 let input = TUnion::from_atomic(TAtomic::Reference(reference));
2055
2056 let mut actual = input;
2057 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2058
2059 assert_eq!(actual.types.len(), 1);
2060 assert!(matches!(&actual.types[0], TAtomic::Object(TObject::Enum(_))));
2061 }
2062
2063 #[test]
2064 fn test_expand_member_reference_unknown_class() {
2065 let codebase = CodebaseMetadata::new();
2066
2067 let reference = TReference::new_member(word("NonExistent"), TReferenceMemberSelector::Identifier(word("FOO")));
2068 let input = TUnion::from_atomic(TAtomic::Reference(reference));
2069
2070 let mut actual = input;
2071 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2072
2073 assert!(actual.types.iter().any(|t| matches!(t, TAtomic::Mixed(_))));
2074 }
2075
2076 #[test]
2077 fn test_expand_member_reference_unknown_member() {
2078 let code = "<?php class Foo {}";
2079 let codebase = create_test_codebase(code);
2080
2081 let reference = TReference::new_member(
2082 ascii_lowercase_word(b"foo"),
2083 TReferenceMemberSelector::Identifier(word("NONEXISTENT")),
2084 );
2085 let input = TUnion::from_atomic(TAtomic::Reference(reference));
2086
2087 let mut actual = input;
2088 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2089
2090 assert!(actual.types.iter().any(|t| matches!(t, TAtomic::Mixed(_))));
2091 }
2092
2093 #[test]
2094 fn test_expand_member_reference_constant_with_inferred_type() {
2095 let code = r#"<?php
2096 class Foo {
2097 public const VALUE = "hello";
2098 }
2099 "#;
2100 let codebase = create_test_codebase(code);
2101
2102 let reference =
2103 TReference::new_member(ascii_lowercase_word(b"foo"), TReferenceMemberSelector::Identifier(word("VALUE")));
2104 let input = TUnion::from_atomic(TAtomic::Reference(reference));
2105
2106 let mut actual = input;
2107 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2108
2109 assert_eq!(actual.types.len(), 1);
2110 }
2111
2112 #[test]
2113 fn test_expand_member_reference_constant_with_type_metadata() {
2114 let code = "<?php
2115 class Foo {
2116 /** @var int */
2117 public const VALUE = 42;
2118 }
2119 ";
2120 let codebase = create_test_codebase(code);
2121
2122 let reference =
2123 TReference::new_member(ascii_lowercase_word(b"foo"), TReferenceMemberSelector::Identifier(word("VALUE")));
2124 let input = TUnion::from_atomic(TAtomic::Reference(reference));
2125
2126 let mut actual = input;
2127 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2128
2129 assert_eq!(actual.types.len(), 1);
2130 }
2131
2132 #[test]
2133 fn test_expand_conditional_both_branches() {
2134 let code = "<?php class Foo {} class Bar {}";
2135 let codebase = create_test_codebase(code);
2136
2137 let conditional = TConditional::new(
2138 Arc::new(get_mixed()),
2139 Arc::new(get_string()),
2140 Arc::new(make_self_object()),
2141 Arc::new(make_self_object()),
2142 false,
2143 );
2144 let input = TUnion::from_atomic(TAtomic::Conditional(conditional));
2145
2146 let options = options_with_self("Foo");
2147 let mut actual = input;
2148 expand_union(&codebase, &mut actual, &options);
2149
2150 assert!(actual.types.iter().any(|t| {
2151 if let TAtomic::Object(TObject::Named(named)) = t {
2152 named.name == ascii_lowercase_word(b"foo")
2153 } else {
2154 false
2155 }
2156 }));
2157 }
2158
2159 #[test]
2160 fn test_expand_conditional_with_self_in_then() {
2161 let code = "<?php class Foo {}";
2162 let codebase = create_test_codebase(code);
2163
2164 let conditional = TConditional::new(
2165 Arc::new(get_mixed()),
2166 Arc::new(get_string()),
2167 Arc::new(make_self_object()),
2168 Arc::new(get_int()),
2169 false,
2170 );
2171 let input = TUnion::from_atomic(TAtomic::Conditional(conditional));
2172
2173 let options = options_with_self("Foo");
2174 let mut actual = input;
2175 expand_union(&codebase, &mut actual, &options);
2176
2177 assert!(!actual.types.is_empty());
2178 }
2179
2180 #[test]
2181 fn test_expand_conditional_with_self_in_otherwise() {
2182 let code = "<?php class Foo {}";
2183 let codebase = create_test_codebase(code);
2184
2185 let conditional = TConditional::new(
2186 Arc::new(get_mixed()),
2187 Arc::new(get_string()),
2188 Arc::new(get_int()),
2189 Arc::new(make_self_object()),
2190 false,
2191 );
2192 let input = TUnion::from_atomic(TAtomic::Conditional(conditional));
2193
2194 let options = options_with_self("Foo");
2195 let mut actual = input;
2196 expand_union(&codebase, &mut actual, &options);
2197
2198 assert!(!actual.types.is_empty());
2199 }
2200
2201 #[test]
2202 fn test_expand_simple_alias() {
2203 let code = "<?php
2204 class Foo {
2205 /** @phpstan-type MyInt = int */
2206 }
2207 ";
2208 let codebase = create_test_codebase(code);
2209
2210 let alias = TAlias::new(ascii_lowercase_word(b"foo"), word("MyInt"));
2211 let input = TUnion::from_atomic(TAtomic::Alias(alias));
2212
2213 let mut actual = input;
2214 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2215
2216 assert!(!actual.types.is_empty());
2217 }
2218
2219 #[test]
2220 fn test_expand_nested_alias() {
2221 let code = "<?php
2222 class Foo {
2223 /** @phpstan-type Inner = int */
2224 /** @phpstan-type Outer = Inner */
2225 }
2226 ";
2227 let codebase = create_test_codebase(code);
2228
2229 let alias = TAlias::new(ascii_lowercase_word(b"foo"), word("Outer"));
2230 let input = TUnion::from_atomic(TAtomic::Alias(alias));
2231
2232 let mut actual = input;
2233 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2234
2235 assert!(!actual.types.is_empty());
2236 }
2237
2238 #[test]
2239 fn test_expand_alias_cycle_detection() {
2240 let code = "<?php
2241 /** @phpstan-type SelfRef = int|array<int, SelfRef> */
2242 class Foo {}
2243 ";
2244 let codebase = create_test_codebase(code);
2245
2246 let alias = TAlias::new(ascii_lowercase_word(b"foo"), word("SelfRef"));
2247 let input = TUnion::from_atomic(TAtomic::Alias(alias));
2248
2249 let mut actual = input;
2250 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2251
2252 assert!(!actual.types.is_empty());
2253 }
2254
2255 #[test]
2256 fn test_expand_alias_unknown() {
2257 let codebase = CodebaseMetadata::new();
2258
2259 let alias = TAlias::new(word("NonExistent"), word("Unknown"));
2260 let input = TUnion::from_atomic(TAtomic::Alias(alias));
2261
2262 let mut actual = input;
2263 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2264
2265 assert!(actual.types.iter().any(|t| matches!(t, TAtomic::Alias(_))));
2266 }
2267
2268 #[test]
2269 fn test_expand_alias_with_self_inside() {
2270 let code = "<?php
2271 class Foo {
2272 /** @phpstan-type MySelf = self */
2273 }
2274 ";
2275 let codebase = create_test_codebase(code);
2276
2277 let alias = TAlias::new(ascii_lowercase_word(b"foo"), word("MySelf"));
2278 let input = TUnion::from_atomic(TAtomic::Alias(alias));
2279
2280 let options = options_with_self("Foo");
2281 let mut actual = input;
2282 expand_union(&codebase, &mut actual, &options);
2283
2284 assert!(!actual.types.is_empty());
2285 }
2286
2287 #[test]
2288 fn test_expand_key_of_array() {
2289 let codebase = CodebaseMetadata::new();
2290
2291 let mut keyed = TKeyedArray::new();
2292 keyed.parameters = Some((Arc::new(get_string()), Arc::new(get_int())));
2293 let array_type = TUnion::from_atomic(TAtomic::Array(TArray::Keyed(keyed)));
2294
2295 let key_of = TKeyOf::new(Arc::new(array_type));
2296 let input = TUnion::from_atomic(TAtomic::Derived(TDerived::KeyOf(key_of)));
2297
2298 let mut actual = input;
2299 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2300
2301 assert!(actual.types.iter().any(super::super::atomic::TAtomic::is_string));
2302 }
2303
2304 #[test]
2305 fn test_expand_key_of_with_self() {
2306 let code = "<?php class Foo {}";
2307 let codebase = create_test_codebase(code);
2308
2309 let mut keyed = TKeyedArray::new();
2310 keyed.parameters = Some((Arc::new(make_self_object()), Arc::new(get_int())));
2311 let array_type = TUnion::from_atomic(TAtomic::Array(TArray::Keyed(keyed)));
2312
2313 let key_of = TKeyOf::new(Arc::new(array_type));
2314 let input = TUnion::from_atomic(TAtomic::Derived(TDerived::KeyOf(key_of)));
2315
2316 let options = options_with_self("Foo");
2317 let mut actual = input;
2318 expand_union(&codebase, &mut actual, &options);
2319
2320 assert!(!actual.types.is_empty());
2321 }
2322
2323 #[test]
2324 fn test_expand_value_of_array() {
2325 let codebase = CodebaseMetadata::new();
2326
2327 let mut keyed = TKeyedArray::new();
2328 keyed.parameters = Some((Arc::new(get_string()), Arc::new(get_int())));
2329 let array_type = TUnion::from_atomic(TAtomic::Array(TArray::Keyed(keyed)));
2330
2331 let value_of = TValueOf::new(Arc::new(array_type));
2332 let input = TUnion::from_atomic(TAtomic::Derived(TDerived::ValueOf(value_of)));
2333
2334 let mut actual = input;
2335 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2336
2337 assert!(actual.types.iter().any(super::super::atomic::TAtomic::is_int));
2338 }
2339
2340 #[test]
2341 fn test_expand_value_of_enum() {
2342 let code = "<?php
2343 enum Status: string {
2344 case Active = 'active';
2345 case Inactive = 'inactive';
2346 }
2347 ";
2348 let codebase = create_test_codebase(code);
2349
2350 let enum_type =
2351 TUnion::from_atomic(TAtomic::Object(TObject::Enum(TEnum::new(ascii_lowercase_word(b"status")))));
2352
2353 let value_of = TValueOf::new(Arc::new(enum_type));
2354 let input = TUnion::from_atomic(TAtomic::Derived(TDerived::ValueOf(value_of)));
2355
2356 let mut actual = input;
2357 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2358
2359 assert!(!actual.types.is_empty());
2360 }
2361
2362 #[test]
2363 fn test_expand_index_access() {
2364 let codebase = CodebaseMetadata::new();
2365
2366 use crate::ttype::atomic::array::key::ArrayKey;
2367 use std::collections::BTreeMap;
2368
2369 let mut keyed = TKeyedArray::new();
2370 let mut known_items = BTreeMap::new();
2371 known_items.insert(ArrayKey::String(word("key")), (false, get_int()));
2372 keyed.known_items = Some(known_items);
2373 let array_type = TUnion::from_atomic(TAtomic::Array(TArray::Keyed(keyed)));
2374
2375 use crate::ttype::get_literal_string;
2376 let index_type = get_literal_string(word("key"));
2377
2378 let index_access = TIndexAccess::new(array_type, index_type);
2379 let input = TUnion::from_atomic(TAtomic::Derived(TDerived::IndexAccess(index_access)));
2380
2381 let mut actual = input;
2382 expand_union(&codebase, &mut actual, &TypeExpansionOptions::default());
2383
2384 assert!(!actual.types.is_empty());
2385 }
2386
2387 #[test]
2388 fn test_expand_index_access_with_self() {
2389 let code = "<?php class Foo {}";
2390 let codebase = create_test_codebase(code);
2391
2392 use crate::ttype::atomic::array::key::ArrayKey;
2393 use std::collections::BTreeMap;
2394
2395 let mut keyed = TKeyedArray::new();
2396 let mut known_items = BTreeMap::new();
2397 known_items.insert(ArrayKey::String(word("key")), (false, make_self_object()));
2398 keyed.known_items = Some(known_items);
2399 let array_type = TUnion::from_atomic(TAtomic::Array(TArray::Keyed(keyed)));
2400
2401 use crate::ttype::get_literal_string;
2402 let index_type = get_literal_string(word("key"));
2403
2404 let index_access = TIndexAccess::new(array_type, index_type);
2405 let input = TUnion::from_atomic(TAtomic::Derived(TDerived::IndexAccess(index_access)));
2406
2407 let options = options_with_self("Foo");
2408 let mut actual = input;
2409 expand_union(&codebase, &mut actual, &options);
2410
2411 assert!(!actual.types.is_empty());
2412 }
2413
2414 #[test]
2415 fn test_expand_iterable_key_type() {
2416 let code = "<?php class Foo {}";
2417 let codebase = create_test_codebase(code);
2418
2419 let iterable = TIterable::new(Arc::new(make_self_object()), Arc::new(get_int()));
2420 let input = TUnion::from_atomic(TAtomic::Iterable(iterable));
2421
2422 let options = options_with_self("Foo");
2423 let mut actual = input;
2424 expand_union(&codebase, &mut actual, &options);
2425
2426 if let TAtomic::Iterable(iter) = &actual.types[0] {
2427 assert!(iter.get_key_type().types.iter().any(|t| {
2428 if let TAtomic::Object(TObject::Named(named)) = t {
2429 named.name == ascii_lowercase_word(b"foo")
2430 } else {
2431 false
2432 }
2433 }));
2434 }
2435 }
2436
2437 #[test]
2438 fn test_expand_iterable_value_type() {
2439 let code = "<?php class Foo {}";
2440 let codebase = create_test_codebase(code);
2441
2442 let iterable = TIterable::new(Arc::new(get_int()), Arc::new(make_self_object()));
2443 let input = TUnion::from_atomic(TAtomic::Iterable(iterable));
2444
2445 let options = options_with_self("Foo");
2446 let mut actual = input;
2447 expand_union(&codebase, &mut actual, &options);
2448
2449 if let TAtomic::Iterable(iter) = &actual.types[0] {
2450 assert!(iter.get_value_type().types.iter().any(|t| {
2451 if let TAtomic::Object(TObject::Named(named)) = t {
2452 named.name == ascii_lowercase_word(b"foo")
2453 } else {
2454 false
2455 }
2456 }));
2457 }
2458 }
2459
2460 #[test]
2461 fn test_get_signature_of_function() {
2462 let code = r#"<?php
2463 function myFunc(int $a): string { return ""; }
2464 "#;
2465 let codebase = create_test_codebase(code);
2466
2467 let id = FunctionLikeIdentifier::Function(ascii_lowercase_word(b"myfunc"));
2468
2469 let sig = get_signature_of_function_like_identifier(&id, &codebase);
2470 assert!(sig.is_some());
2471
2472 let sig = sig.unwrap();
2473 assert_eq!(sig.get_parameters().len(), 1);
2474 assert!(sig.get_return_type().is_some());
2475 }
2476
2477 #[test]
2478 fn test_get_signature_of_method() {
2479 let code = "<?php
2480 class Foo {
2481 public function bar(string $s): int { return 0; }
2482 }
2483 ";
2484 let codebase = create_test_codebase(code);
2485
2486 let id = FunctionLikeIdentifier::Method(ascii_lowercase_word(b"foo"), ascii_lowercase_word(b"bar"));
2487
2488 let sig = get_signature_of_function_like_identifier(&id, &codebase);
2489 assert!(sig.is_some());
2490
2491 let sig = sig.unwrap();
2492 assert_eq!(sig.get_parameters().len(), 1);
2493 }
2494
2495 #[test]
2496 fn test_get_signature_of_closure() {
2497 let codebase = CodebaseMetadata::new();
2498
2499 let id = FunctionLikeIdentifier::Closure(word(b"{closure:test.php:1:1}"));
2500 let sig = get_signature_of_function_like_identifier(&id, &codebase);
2501
2502 assert!(sig.is_none());
2503 }
2504
2505 #[test]
2506 fn test_get_atomic_of_function() {
2507 let code = "<?php
2508 function myFunc(): void {}
2509 ";
2510 let codebase = create_test_codebase(code);
2511
2512 let id = FunctionLikeIdentifier::Function(ascii_lowercase_word(b"myfunc"));
2513
2514 let atomic = get_atomic_of_function_like_identifier(&id, &codebase);
2515 assert!(atomic.is_some());
2516 assert!(matches!(atomic.unwrap(), TAtomic::Callable(TCallable::Signature(_))));
2517 }
2518
2519 #[test]
2520 fn test_get_signature_with_parameters() {
2521 let code = "<?php
2522 function multiParam(int $a, string $b, ?float $c = null): bool { return true; }
2523 ";
2524 let codebase = create_test_codebase(code);
2525
2526 let id = FunctionLikeIdentifier::Function(ascii_lowercase_word(b"multiparam"));
2527
2528 let sig = get_signature_of_function_like_identifier(&id, &codebase);
2529 assert!(sig.is_some());
2530
2531 let sig = sig.unwrap();
2532 assert_eq!(sig.get_parameters().len(), 3);
2533
2534 let third_param = &sig.get_parameters()[2];
2535 assert!(third_param.has_default());
2536 }
2537
2538 #[test]
2539 fn test_expand_preserves_by_reference_flag() {
2540 let code = "<?php class Foo {}";
2541 let codebase = create_test_codebase(code);
2542
2543 let mut input = make_self_object();
2544 input.flags.insert(UnionFlags::BY_REFERENCE);
2545
2546 let options = options_with_self("Foo");
2547 let mut actual = input.clone();
2548 expand_union(&codebase, &mut actual, &options);
2549
2550 assert!(actual.flags.contains(UnionFlags::BY_REFERENCE));
2551 }
2552
2553 #[test]
2554 fn test_expand_preserves_possibly_undefined_flag() {
2555 let code = "<?php class Foo {}";
2556 let codebase = create_test_codebase(code);
2557
2558 let mut input = make_self_object();
2559 input.flags.insert(UnionFlags::POSSIBLY_UNDEFINED);
2560
2561 let options = options_with_self("Foo");
2562 let mut actual = input.clone();
2563 expand_union(&codebase, &mut actual, &options);
2564
2565 assert!(actual.flags.contains(UnionFlags::POSSIBLY_UNDEFINED));
2566 }
2567
2568 #[test]
2569 fn test_expand_multiple_self_in_union() {
2570 let code = "<?php class Foo {}";
2571 let codebase = create_test_codebase(code);
2572
2573 let input = TUnion::from_vec(vec![
2574 TAtomic::Object(TObject::Named(TNamedObject::new(word("self")))),
2575 TAtomic::Object(TObject::Named(TNamedObject::new(word("self")))),
2576 ]);
2577
2578 let options = options_with_self("Foo");
2579 let mut actual = input;
2580 expand_union(&codebase, &mut actual, &options);
2581
2582 assert!(actual.types.len() <= 2);
2583 }
2584
2585 #[test]
2586 fn test_expand_deeply_nested_types() {
2587 let code = "<?php class Foo {}";
2588 let codebase = create_test_codebase(code);
2589
2590 let inner = TList::new(Arc::new(make_self_object()));
2591 let middle = TList::new(Arc::new(TUnion::from_atomic(TAtomic::Array(TArray::List(inner)))));
2592 let outer = TList::new(Arc::new(TUnion::from_atomic(TAtomic::Array(TArray::List(middle)))));
2593 let input = TUnion::from_atomic(TAtomic::Array(TArray::List(outer)));
2594
2595 let options = options_with_self("Foo");
2596 let mut actual = input;
2597 expand_union(&codebase, &mut actual, &options);
2598
2599 if let TAtomic::Array(TArray::List(outer)) = &actual.types[0]
2600 && let TAtomic::Array(TArray::List(middle)) = &outer.element_type.types[0]
2601 && let TAtomic::Array(TArray::List(inner)) = &middle.element_type.types[0]
2602 {
2603 assert!(inner.element_type.types.iter().any(|t| {
2604 if let TAtomic::Object(TObject::Named(named)) = t {
2605 named.name == ascii_lowercase_word(b"foo")
2606 } else {
2607 false
2608 }
2609 }));
2610 }
2611 }
2612
2613 #[test]
2614 fn test_expand_with_all_options_disabled() {
2615 let code = "<?php class Foo {}";
2616 let codebase = create_test_codebase(code);
2617
2618 let input = make_self_object();
2619 let options = TypeExpansionOptions {
2620 self_class: None,
2621 static_class_type: StaticClassType::None,
2622 parent_class: None,
2623 evaluate_class_constants: false,
2624 evaluate_conditional_types: false,
2625 function_is_final: false,
2626 expand_generic: false,
2627 expand_templates: false,
2628 };
2629
2630 let mut actual = input;
2631 expand_union(&codebase, &mut actual, &options);
2632
2633 assert!(actual.types.iter().any(|t| {
2634 if let TAtomic::Object(TObject::Named(named)) = t { named.name == word("self") } else { false }
2635 }));
2636 }
2637
2638 #[test]
2639 fn test_expand_already_expanded_type() {
2640 let code = "<?php class Foo {}";
2641 let codebase = create_test_codebase(code);
2642
2643 let input = make_named_object("Foo");
2644 let options = options_with_self("Foo");
2645
2646 let mut actual = input;
2647 expand_union(&codebase, &mut actual, &options);
2648
2649 let mut actual2 = actual.clone();
2650 expand_union(&codebase, &mut actual2, &options);
2651
2652 assert_eq!(actual.types.as_ref(), actual2.types.as_ref());
2653 }
2654
2655 #[test]
2656 fn test_expand_complex_generic_class() {
2657 let code = "<?php
2658 /**
2659 * @template T
2660 * @template U
2661 */
2662 class Container {}
2663 ";
2664 let codebase = create_test_codebase(code);
2665
2666 let named = TNamedObject::new_with_type_parameters(
2667 ascii_lowercase_word(b"container"),
2668 Some(vec![make_self_object(), make_static_object()]),
2669 );
2670 let input = TUnion::from_atomic(TAtomic::Object(TObject::Named(named)));
2671
2672 let options = TypeExpansionOptions {
2673 self_class: Some(ascii_lowercase_word(b"foo")),
2674 static_class_type: StaticClassType::Name(ascii_lowercase_word(b"bar")),
2675 ..Default::default()
2676 };
2677
2678 let mut actual = input;
2679 expand_union(&codebase, &mut actual, &options);
2680
2681 if let TAtomic::Object(TObject::Named(named)) = &actual.types[0]
2682 && let Some(params) = &named.type_parameters
2683 {
2684 assert!(params[0].types.iter().any(|t| {
2685 if let TAtomic::Object(TObject::Named(named)) = t {
2686 named.name == ascii_lowercase_word(b"foo")
2687 } else {
2688 false
2689 }
2690 }));
2691 assert!(params[1].types.iter().any(|t| {
2692 if let TAtomic::Object(TObject::Named(named)) = t {
2693 named.name == ascii_lowercase_word(b"bar")
2694 } else {
2695 false
2696 }
2697 }));
2698 }
2699 }
2700}