2020
2121declare (strict_types=1 );
2222
23- namespace PackageFactory \ComponentEngine \TypeSystem \Inferrer ;
23+ namespace PackageFactory \ComponentEngine \TypeSystem \Narrower ;
2424
2525use PackageFactory \ComponentEngine \Definition \BinaryOperator ;
2626use PackageFactory \ComponentEngine \Parser \Ast \BinaryOperationNode ;
4040 * The structure is partially inspired by phpstan
4141 * https://github.com/phpstan/phpstan-src/blob/07bb4aa2d5e39dafa78f56c5df132c763c2d1b67/src/Analyser/TypeSpecifier.php#L111
4242 */
43- class TypeInferrer
43+ class ExpressionTypeNarrower
4444{
4545 public function __construct (
4646 private readonly ScopeInterface $ scope
4747 ) {
4848 }
4949
50- public function inferTypesInCondition (ExpressionNode $ conditionNode , TypeInferrerContext $ context ): InferredTypes
50+ public function narrowTypesOfSymbolsIn (ExpressionNode $ expressionNode , TypeNarrowerContext $ context ): NarrowedTypes
5151 {
52- if ($ conditionNode ->root instanceof IdentifierNode) {
53- $ type = $ this ->scope ->lookupTypeFor ($ conditionNode ->root ->value );
52+ if ($ expressionNode ->root instanceof IdentifierNode) {
53+ $ type = $ this ->scope ->lookupTypeFor ($ expressionNode ->root ->value );
5454 if (!$ type ) {
55- return InferredTypes ::empty ();
55+ return NarrowedTypes ::empty ();
5656 }
5757 // case `nullableString ? "nullableString is not null" : "nullableString is null"`
58- return InferredTypes:: fromType ( $ conditionNode ->root ->value , $ context ->narrowDownType ($ type ));
58+ return NarrowedTypes:: fromEntry ( $ expressionNode ->root ->value , $ context ->narrowType ($ type ));
5959 }
6060
61- if (($ binaryOperationNode = $ conditionNode ->root ) instanceof BinaryOperationNode) {
61+ if (($ binaryOperationNode = $ expressionNode ->root ) instanceof BinaryOperationNode) {
6262 // cases
6363 // `nullableString === null ? "nullableString is null" : "nullableString is not null"`
6464 // `nullableString !== null ? "nullableString is not null" : "nullableString is null"`
6565 if (count ($ binaryOperationNode ->operands ->rest ) !== 1 ) {
66- return InferredTypes ::empty ();
66+ return NarrowedTypes ::empty ();
6767 }
6868 $ first = $ binaryOperationNode ->operands ->first ;
6969 $ second = $ binaryOperationNode ->operands ->rest [0 ];
@@ -77,21 +77,21 @@ public function inferTypesInCondition(ExpressionNode $conditionNode, TypeInferre
7777 };
7878
7979 if ($ comparedIdentifierValueToNull === null ) {
80- return InferredTypes ::empty ();
80+ return NarrowedTypes ::empty ();
8181 }
8282 $ type = $ this ->scope ->lookupTypeFor ($ comparedIdentifierValueToNull );
8383 if (!$ type ) {
84- return InferredTypes ::empty ();
84+ return NarrowedTypes ::empty ();
8585 }
8686
8787 if ($ binaryOperationNode ->operator === BinaryOperator::EQUAL ) {
88- return InferredTypes:: fromType ($ comparedIdentifierValueToNull , $ context ->negate ()->narrowDownType ($ type ));
88+ return NarrowedTypes:: fromEntry ($ comparedIdentifierValueToNull , $ context ->negate ()->narrowType ($ type ));
8989 }
9090 if ($ binaryOperationNode ->operator === BinaryOperator::NOT_EQUAL ) {
91- return InferredTypes:: fromType ($ comparedIdentifierValueToNull , $ context ->narrowDownType ($ type ));
91+ return NarrowedTypes:: fromEntry ($ comparedIdentifierValueToNull , $ context ->narrowType ($ type ));
9292 }
9393 }
9494
95- return InferredTypes ::empty ();
95+ return NarrowedTypes ::empty ();
9696 }
9797}
0 commit comments