From 6095e3216f057fbf8dded18cf982bfab034f8086 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 22 Feb 2024 02:52:54 +0700 Subject: [PATCH] [TypeDeclaration] Allow abs() to return int when pass int arg on ReturnTypeFromStrictTypedCallRector (#5653) * [TypeDeclaration] Allow abs() to return int when pass int arg * [TypeDeclaration] Allow abs() to return int when pass int arg --- .../FixturePhp80/abs_return_int.php.inc | 25 +++++++++++++++++++ .../TypeAnalyzer/ReturnStrictTypeAnalyzer.php | 14 +++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/FixturePhp80/abs_return_int.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/FixturePhp80/abs_return_int.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/FixturePhp80/abs_return_int.php.inc new file mode 100644 index 00000000000..75d1c27b550 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/FixturePhp80/abs_return_int.php.inc @@ -0,0 +1,25 @@ + +----- + diff --git a/rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php b/rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php index 86c90d728ec..df526efa7c6 100644 --- a/rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php +++ b/rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php @@ -4,7 +4,6 @@ namespace Rector\TypeDeclaration\TypeAnalyzer; -use PHPStan\Reflection\ParametersAcceptorSelector; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; @@ -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; @@ -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();