Skip to content

Commit 244093e

Browse files
committed
Infer types of MethodCall with dynamic name
1 parent 51a867f commit 244093e

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

src/Analyser/MutatingScope.php

+30-19
Original file line numberDiff line numberDiff line change
@@ -2029,36 +2029,47 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
20292029
);
20302030
}
20312031

2032-
if ($node instanceof MethodCall && $node->name instanceof Node\Identifier) {
2033-
if ($this->nativeTypesPromoted) {
2032+
if ($node instanceof MethodCall) {
2033+
if ($node->name instanceof Node\Identifier) {
2034+
if ($this->nativeTypesPromoted) {
2035+
$typeCallback = function () use ($node): Type {
2036+
$methodReflection = $this->getMethodReflection(
2037+
$this->getNativeType($node->var),
2038+
$node->name->name,
2039+
);
2040+
if ($methodReflection === null) {
2041+
return new ErrorType();
2042+
}
2043+
2044+
return ParametersAcceptorSelector::combineAcceptors($methodReflection->getVariants())->getNativeReturnType();
2045+
};
2046+
2047+
return $this->getNullsafeShortCircuitingType($node->var, $typeCallback());
2048+
}
2049+
20342050
$typeCallback = function () use ($node): Type {
2035-
$methodReflection = $this->getMethodReflection(
2036-
$this->getNativeType($node->var),
2051+
$returnType = $this->methodCallReturnType(
2052+
$this->getType($node->var),
20372053
$node->name->name,
2054+
$node,
20382055
);
2039-
if ($methodReflection === null) {
2056+
if ($returnType === null) {
20402057
return new ErrorType();
20412058
}
2042-
2043-
return ParametersAcceptorSelector::combineAcceptors($methodReflection->getVariants())->getNativeReturnType();
2059+
return $returnType;
20442060
};
20452061

20462062
return $this->getNullsafeShortCircuitingType($node->var, $typeCallback());
20472063
}
20482064

2049-
$typeCallback = function () use ($node): Type {
2050-
$returnType = $this->methodCallReturnType(
2051-
$this->getType($node->var),
2052-
$node->name->name,
2053-
$node,
2065+
$nameType = $this->getType($node->name);
2066+
if (count($nameType->getConstantStrings()) > 0) {
2067+
return TypeCombinator::union(
2068+
...array_map(fn ($constantString) => $this
2069+
->filterByTruthyValue(new BinaryOp\Identical($node->name, new String_($constantString->getValue())))
2070+
->getType(new MethodCall($node->var, new Identifier($constantString->getValue()), $node->args)), $nameType->getConstantStrings()),
20542071
);
2055-
if ($returnType === null) {
2056-
return new ErrorType();
2057-
}
2058-
return $returnType;
2059-
};
2060-
2061-
return $this->getNullsafeShortCircuitingType($node->var, $typeCallback());
2072+
}
20622073
}
20632074

20642075
if ($node instanceof Expr\NullsafeMethodCall) {

0 commit comments

Comments
 (0)