@@ -53,22 +53,19 @@ public function inferTypesInCondition(ExpressionNode $conditionNode, TypeInferre
5353 {
5454 if ($ conditionNode ->root instanceof IdentifierNode) {
5555 $ 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 ();
56+ if (!$ type ) {
57+ return InferredTypes::empty ();
5958 }
60-
61- return new InferredTypes (
62- ...[$ conditionNode ->root ->value => $ context ->isTrue () ? $ type ->withoutNull () : NullType::get ()]
63- );
59+ // case `nullableString ? "nullableString is not null" : "nullableString is null"`
60+ return InferredTypes::fromType ($ conditionNode ->root ->value , $ context ->narrowDownType ($ type ));
6461 }
6562
6663 if (($ binaryOperationNode = $ conditionNode ->root ) instanceof BinaryOperationNode) {
6764 // cases
6865 // `nullableString === null ? "nullableString is null" : "nullableString is not null"`
6966 // `nullableString !== null ? "nullableString is not null" : "nullableString is null"`
7067 if (count ($ binaryOperationNode ->operands ->rest ) !== 1 ) {
71- return new InferredTypes ();
68+ return InferredTypes:: empty ();
7269 }
7370 $ first = $ binaryOperationNode ->operands ->first ;
7471 $ second = $ binaryOperationNode ->operands ->rest [0 ];
@@ -82,26 +79,21 @@ public function inferTypesInCondition(ExpressionNode $conditionNode, TypeInferre
8279 };
8380
8481 if ($ comparedIdentifierValueToNull === null ) {
85- return new InferredTypes ();
82+ return InferredTypes:: empty ();
8683 }
87-
8884 $ type = $ this ->scope ->lookupTypeFor ($ comparedIdentifierValueToNull );
89- if (!$ type instanceof UnionType || ! $ type -> containsNull () ) {
90- return new InferredTypes ();
85+ if (!$ type ) {
86+ return InferredTypes:: empty ();
9187 }
9288
9389 if ($ binaryOperationNode ->operator === BinaryOperator::EQUAL ) {
94- return new InferredTypes (
95- ...[$ comparedIdentifierValueToNull => $ context ->isTrue () ? NullType::get () : $ type ->withoutNull ()]
96- );
90+ return InferredTypes::fromType ($ comparedIdentifierValueToNull , $ context ->negate ()->narrowDownType ($ type ));
9791 }
9892 if ($ binaryOperationNode ->operator === BinaryOperator::NOT_EQUAL ) {
99- return new InferredTypes (
100- ...[$ comparedIdentifierValueToNull => $ context ->isTrue () ? $ type ->withoutNull () : NullType::get ()]
101- );
93+ return InferredTypes::fromType ($ comparedIdentifierValueToNull , $ context ->narrowDownType ($ type ));
10294 }
10395 }
10496
105- return new InferredTypes ();
97+ return InferredTypes:: empty ();
10698 }
10799}
0 commit comments