28
28
use PackageFactory \ComponentEngine \Parser \Ast \IdentifierNode ;
29
29
use PackageFactory \ComponentEngine \Parser \Ast \NullLiteralNode ;
30
30
use PackageFactory \ComponentEngine \TypeSystem \ScopeInterface ;
31
- use PackageFactory \ComponentEngine \TypeSystem \Type \NullType \NullType ;
32
- use PackageFactory \ComponentEngine \TypeSystem \Type \UnionType \UnionType ;
33
31
34
32
/**
35
33
* This class handles the analysis of identifier types that are used in a condition
@@ -53,22 +51,19 @@ public function inferTypesInCondition(ExpressionNode $conditionNode, TypeInferre
53
51
{
54
52
if ($ conditionNode ->root instanceof IdentifierNode) {
55
53
$ 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 ();
59
56
}
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 ));
64
59
}
65
60
66
61
if (($ binaryOperationNode = $ conditionNode ->root ) instanceof BinaryOperationNode) {
67
62
// cases
68
63
// `nullableString === null ? "nullableString is null" : "nullableString is not null"`
69
64
// `nullableString !== null ? "nullableString is not null" : "nullableString is null"`
70
65
if (count ($ binaryOperationNode ->operands ->rest ) !== 1 ) {
71
- return new InferredTypes ();
66
+ return InferredTypes:: empty ();
72
67
}
73
68
$ first = $ binaryOperationNode ->operands ->first ;
74
69
$ second = $ binaryOperationNode ->operands ->rest [0 ];
@@ -82,26 +77,21 @@ public function inferTypesInCondition(ExpressionNode $conditionNode, TypeInferre
82
77
};
83
78
84
79
if ($ comparedIdentifierValueToNull === null ) {
85
- return new InferredTypes ();
80
+ return InferredTypes:: empty ();
86
81
}
87
-
88
82
$ type = $ this ->scope ->lookupTypeFor ($ comparedIdentifierValueToNull );
89
- if (!$ type instanceof UnionType || ! $ type -> containsNull () ) {
90
- return new InferredTypes ();
83
+ if (!$ type ) {
84
+ return InferredTypes:: empty ();
91
85
}
92
86
93
87
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 ));
97
89
}
98
90
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 ));
102
92
}
103
93
}
104
94
105
- return new InferredTypes ();
95
+ return InferredTypes:: empty ();
106
96
}
107
97
}
0 commit comments