From 113810116956135df1b1862c9b87ea1bd211c94f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 13 Mar 2023 21:48:25 +0700 Subject: [PATCH] [CodeQuality] Add indirect return scalar on ReturnTypeFromStrictScalarReturnExprRector (#3478) --- .../Fixture/return_variable_string.php.inc | 29 ++++++++++++++ .../AlwaysStrictScalarExprAnalyzer.php | 39 ++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector/Fixture/return_variable_string.php.inc diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector/Fixture/return_variable_string.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector/Fixture/return_variable_string.php.inc new file mode 100644 index 00000000000..5fe665b40f3 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector/Fixture/return_variable_string.php.inc @@ -0,0 +1,29 @@ + +----- + diff --git a/rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictScalarExprAnalyzer.php b/rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictScalarExprAnalyzer.php index a832699f4a8..8f9b0d760db 100644 --- a/rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictScalarExprAnalyzer.php +++ b/rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictScalarExprAnalyzer.php @@ -5,9 +5,13 @@ namespace Rector\TypeDeclaration\TypeAnalyzer; use PhpParser\Node\Expr; +use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\BinaryOp\Concat; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\PropertyFetch; +use PhpParser\Node\Expr\StaticPropertyFetch; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; use PhpParser\Node\Scalar; use PhpParser\Node\Scalar\DNumber; @@ -15,6 +19,8 @@ use PhpParser\Node\Scalar\MagicConst; use PhpParser\Node\Scalar\MagicConst\Line; use PhpParser\Node\Scalar\String_; +use PhpParser\Node\Stmt\Expression; +use PhpParser\Node\Stmt\Return_; use PHPStan\Analyser\Scope; use PHPStan\Reflection\Native\NativeFunctionReflection; use PHPStan\Reflection\ReflectionProvider; @@ -25,13 +31,15 @@ use PHPStan\Type\NullType; use PHPStan\Type\StringType; use PHPStan\Type\Type; +use Rector\Core\PhpParser\Comparing\NodeComparator; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper; final class AlwaysStrictScalarExprAnalyzer { public function __construct( - private readonly ReflectionProvider $reflectionProvider + private readonly ReflectionProvider $reflectionProvider, + private readonly NodeComparator $nodeComparator ) { } @@ -72,7 +80,34 @@ public function matchStrictScalarExpr(Expr $expr): ?Type return $returnType; } - return null; + return $this->resolveIndirectReturnType($expr); + } + + private function resolveIndirectReturnType(Expr $expr): ?Type + { + if (! $expr instanceof Variable && ! $expr instanceof PropertyFetch && ! $expr instanceof StaticPropertyFetch) { + return null; + } + + $parentNode = $expr->getAttribute(AttributeKey::PARENT_NODE); + if (! $parentNode instanceof Return_) { + return null; + } + + $node = $parentNode->getAttribute(AttributeKey::PREVIOUS_NODE); + if (! $node instanceof Expression) { + return null; + } + + if (! $node->expr instanceof Assign) { + return null; + } + + if (! $this->nodeComparator->areNodesEqual($node->expr->var, $expr)) { + return null; + } + + return $this->matchStrictScalarExpr($node->expr->expr); } private function isScalarType(Type $type): bool