2828use PackageFactory \ComponentEngine \Parser \Ast \IdentifierNode ;
2929use PackageFactory \ComponentEngine \Parser \Ast \NullLiteralNode ;
3030use PackageFactory \ComponentEngine \TypeSystem \ScopeInterface ;
31- use PackageFactory \ComponentEngine \TypeSystem \Type \NullType \NullType ;
32- use PackageFactory \ComponentEngine \TypeSystem \Type \UnionType \UnionType ;
3331
3432/**
3533 * This class handles the analysis of identifier types that are used in a condition
@@ -53,22 +51,19 @@ public function inferTypesInCondition(ExpressionNode $conditionNode, TypeInferre
5351 {
5452 if ($ conditionNode ->root instanceof IdentifierNode) {
5553 $ type = $ this ->scope ->lookupTypeFor ($ conditionNode ->root ->value );
56- // case `nullableString ? "nullableString is not null" : "nullableString is null"`
57- if (!$ type instanceof UnionType || !$ type ->containsNull ()) {
58- return new InferredTypes ();
54+ if (!$ type ) {
55+ return InferredTypes::empty ();
5956 }
60-
61- return new InferredTypes (
62- ...[$ conditionNode ->root ->value => $ context ->isTrue () ? $ type ->withoutNull () : NullType::get ()]
63- );
57+ // case `nullableString ? "nullableString is not null" : "nullableString is null"`
58+ return InferredTypes::fromType ($ conditionNode ->root ->value , $ context ->narrowDownType ($ type ));
6459 }
6560
6661 if (($ binaryOperationNode = $ conditionNode ->root ) instanceof BinaryOperationNode) {
6762 // cases
6863 // `nullableString === null ? "nullableString is null" : "nullableString is not null"`
6964 // `nullableString !== null ? "nullableString is not null" : "nullableString is null"`
7065 if (count ($ binaryOperationNode ->operands ->rest ) !== 1 ) {
71- return new InferredTypes ();
66+ return InferredTypes:: empty ();
7267 }
7368 $ first = $ binaryOperationNode ->operands ->first ;
7469 $ second = $ binaryOperationNode ->operands ->rest [0 ];
@@ -82,26 +77,21 @@ public function inferTypesInCondition(ExpressionNode $conditionNode, TypeInferre
8277 };
8378
8479 if ($ comparedIdentifierValueToNull === null ) {
85- return new InferredTypes ();
80+ return InferredTypes:: empty ();
8681 }
87-
8882 $ type = $ this ->scope ->lookupTypeFor ($ comparedIdentifierValueToNull );
89- if (!$ type instanceof UnionType || ! $ type -> containsNull () ) {
90- return new InferredTypes ();
83+ if (!$ type ) {
84+ return InferredTypes:: empty ();
9185 }
9286
9387 if ($ binaryOperationNode ->operator === BinaryOperator::EQUAL ) {
94- return new InferredTypes (
95- ...[$ comparedIdentifierValueToNull => $ context ->isTrue () ? NullType::get () : $ type ->withoutNull ()]
96- );
88+ return InferredTypes::fromType ($ comparedIdentifierValueToNull , $ context ->negate ()->narrowDownType ($ type ));
9789 }
9890 if ($ binaryOperationNode ->operator === BinaryOperator::NOT_EQUAL ) {
99- return new InferredTypes (
100- ...[$ comparedIdentifierValueToNull => $ context ->isTrue () ? $ type ->withoutNull () : NullType::get ()]
101- );
91+ return InferredTypes::fromType ($ comparedIdentifierValueToNull , $ context ->narrowDownType ($ type ));
10292 }
10393 }
10494
105- return new InferredTypes ();
95+ return InferredTypes:: empty ();
10696 }
10797}
0 commit comments