Skip to content

Commit

Permalink
Fix calling dynamic return type extensions on nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 21, 2023
1 parent 26c1788 commit 7331bc5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4774,8 +4774,7 @@ private function exactInstantiation(New_ $node, string $className): ?Type
);
}

/** @api */
public function getMethodReflection(Type $typeWithMethod, string $methodName): ?ExtendedMethodReflection
private function filterTypeWithMethod(Type $typeWithMethod, string $methodName): ?Type
{
if ($typeWithMethod instanceof UnionType) {
$newTypes = [];
Expand All @@ -4796,19 +4795,31 @@ public function getMethodReflection(Type $typeWithMethod, string $methodName): ?
return null;
}

return $typeWithMethod->getMethod($methodName, $this);
return $typeWithMethod;
}

/** @api */
public function getMethodReflection(Type $typeWithMethod, string $methodName): ?ExtendedMethodReflection
{
$type = $this->filterTypeWithMethod($typeWithMethod, $methodName);
if ($type === null) {
return null;
}

return $type->getMethod($methodName, $this);
}

/**
* @param MethodCall|Node\Expr\StaticCall $methodCall
*/
private function methodCallReturnType(Type $typeWithMethod, string $methodName, Expr $methodCall): ?Type
{
$methodReflection = $this->getMethodReflection($typeWithMethod, $methodName);
if ($methodReflection === null) {
$typeWithMethod = $this->filterTypeWithMethod($typeWithMethod, $methodName);
if ($typeWithMethod === null) {
return null;
}

$methodReflection = $typeWithMethod->getMethod($methodName, $this);
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$this,
$methodCall->getArgs(),
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Analyser/data/date-format.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ function (\DateTimeImmutable $dt, string $s): void {
assertType('numeric-string', $dt->format('Y'));
assertType('numeric-string', $dt->format('Ghi'));
};

function (?\DateTimeImmutable $d): void {
assertType('DateTimeImmutable|null', $d->modify('+1 day'));
};

0 comments on commit 7331bc5

Please sign in to comment.