Skip to content

Commit

Permalink
[CodeQuality] Add indirect return scalar on ReturnTypeFromStrictScala…
Browse files Browse the repository at this point in the history
…rReturnExprRector (#3478)
  • Loading branch information
samsonasik committed Mar 13, 2023
1 parent 6cc0a40 commit 1138101
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture;

final class ReturnVariableString
{
public function run($value)
{
$result = $value . '/' ;
return $result;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture;

final class ReturnVariableString
{
public function run($value): string
{
$result = $value . '/' ;
return $result;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
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;
use PhpParser\Node\Scalar\LNumber;
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;
Expand All @@ -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
) {
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1138101

Please sign in to comment.