Skip to content

Commit 7331bc5

Browse files
committedFeb 21, 2023
Fix calling dynamic return type extensions on nullable types
1 parent 26c1788 commit 7331bc5

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed
 

‎src/Analyser/MutatingScope.php

+16-5
Original file line numberDiff line numberDiff line change
@@ -4774,8 +4774,7 @@ private function exactInstantiation(New_ $node, string $className): ?Type
47744774
);
47754775
}
47764776

4777-
/** @api */
4778-
public function getMethodReflection(Type $typeWithMethod, string $methodName): ?ExtendedMethodReflection
4777+
private function filterTypeWithMethod(Type $typeWithMethod, string $methodName): ?Type
47794778
{
47804779
if ($typeWithMethod instanceof UnionType) {
47814780
$newTypes = [];
@@ -4796,19 +4795,31 @@ public function getMethodReflection(Type $typeWithMethod, string $methodName): ?
47964795
return null;
47974796
}
47984797

4799-
return $typeWithMethod->getMethod($methodName, $this);
4798+
return $typeWithMethod;
4799+
}
4800+
4801+
/** @api */
4802+
public function getMethodReflection(Type $typeWithMethod, string $methodName): ?ExtendedMethodReflection
4803+
{
4804+
$type = $this->filterTypeWithMethod($typeWithMethod, $methodName);
4805+
if ($type === null) {
4806+
return null;
4807+
}
4808+
4809+
return $type->getMethod($methodName, $this);
48004810
}
48014811

48024812
/**
48034813
* @param MethodCall|Node\Expr\StaticCall $methodCall
48044814
*/
48054815
private function methodCallReturnType(Type $typeWithMethod, string $methodName, Expr $methodCall): ?Type
48064816
{
4807-
$methodReflection = $this->getMethodReflection($typeWithMethod, $methodName);
4808-
if ($methodReflection === null) {
4817+
$typeWithMethod = $this->filterTypeWithMethod($typeWithMethod, $methodName);
4818+
if ($typeWithMethod === null) {
48094819
return null;
48104820
}
48114821

4822+
$methodReflection = $typeWithMethod->getMethod($methodName, $this);
48124823
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
48134824
$this,
48144825
$methodCall->getArgs(),

‎tests/PHPStan/Analyser/data/date-format.php

+4
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ function (\DateTimeImmutable $dt, string $s): void {
4343
assertType('numeric-string', $dt->format('Y'));
4444
assertType('numeric-string', $dt->format('Ghi'));
4545
};
46+
47+
function (?\DateTimeImmutable $d): void {
48+
assertType('DateTimeImmutable|null', $d->modify('+1 day'));
49+
};

0 commit comments

Comments
 (0)
Please sign in to comment.