Skip to content

Commit

Permalink
[TypeDeclaration] Allow abs() to return int when pass int arg on Ret…
Browse files Browse the repository at this point in the history
…urnTypeFromStrictTypedCallRector (#5653)

* [TypeDeclaration] Allow abs() to return int when pass int arg

* [TypeDeclaration] Allow abs() to return int when pass int arg
  • Loading branch information
samsonasik committed Feb 21, 2024
1 parent 99a79f8 commit 6095e32
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
@@ -0,0 +1,25 @@
<?php

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

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

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

final class AbsReturnInt
{
function aa(int $param): int
{
return abs($param);
}
}
?>
14 changes: 12 additions & 2 deletions rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php
Expand Up @@ -4,7 +4,6 @@

namespace Rector\TypeDeclaration\TypeAnalyzer;

use PHPStan\Reflection\ParametersAcceptorSelector;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
Expand All @@ -27,6 +26,8 @@
use PHPStan\Type\StaticType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Reflection\ReflectionResolver;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand Down Expand Up @@ -99,7 +100,16 @@ public function resolveMethodCallReturnNode(MethodCall | StaticCall | FuncCall $
return null;
}

$parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::combineAcceptors($methodReflection->getVariants());
$scope = $call->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return null;
}

$parametersAcceptorWithPhpDocs = ParametersAcceptorSelectorVariantsWrapper::select(
$methodReflection,
$call,
$scope
);
if ($parametersAcceptorWithPhpDocs instanceof FunctionVariantWithPhpDocs) {
// native return type is needed, as docblock can be false
$returnType = $parametersAcceptorWithPhpDocs->getNativeReturnType();
Expand Down

0 comments on commit 6095e32

Please sign in to comment.