Skip to content

Commit

Permalink
[TypeDeclaration] Fix abs() returns on ReturnTypeFromStrictTypedCallR…
Browse files Browse the repository at this point in the history
…ector (#5652)

* Create demo_file.php.inc with return type for abs function

Function [abs](https://www.php.net/manual/en/function.abs.php) returns int or float (depends on input parameter). For PHP versions without support union return types this rule should only add return type into docBlock.

* return

* return

* fix

* fix

* [ci-review] Rector Rectify

---------

Co-authored-by: Pavel Kácha <kacha@shoptet.cz>
Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
3 people committed Feb 21, 2024
1 parent 290926c commit 99a79f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
@@ -0,0 +1,25 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Fixture;

final class AbsReturn
{
function aa($param)
{
return abs($param);
}
}
?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Fixture;

final class AbsReturn
{
function aa($param): int|float
{
return abs($param);
}
}
?>
Expand Up @@ -4,6 +4,7 @@

namespace Rector\TypeDeclaration\TypeAnalyzer;

use PHPStan\Reflection\ParametersAcceptorSelector;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
Expand Down Expand Up @@ -98,12 +99,12 @@ public function resolveMethodCallReturnNode(MethodCall | StaticCall | FuncCall $
return null;
}

$parametersAcceptor = $methodReflection->getVariants()[0];
if ($parametersAcceptor instanceof FunctionVariantWithPhpDocs) {
$parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::combineAcceptors($methodReflection->getVariants());
if ($parametersAcceptorWithPhpDocs instanceof FunctionVariantWithPhpDocs) {
// native return type is needed, as docblock can be false
$returnType = $parametersAcceptor->getNativeReturnType();
$returnType = $parametersAcceptorWithPhpDocs->getNativeReturnType();
} else {
$returnType = $parametersAcceptor->getReturnType();
$returnType = $parametersAcceptorWithPhpDocs->getReturnType();
}

if ($returnType instanceof MixedType) {
Expand Down

0 comments on commit 99a79f8

Please sign in to comment.