Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Naming] Add StaticPropertyFetch support on RenameForeachValueVariableToMatchExprVariableRector #1232

Merged
merged 4 commits into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector\Fixture;

final class SomeStaticPropertyFetch
{
public static $variables = ['test'];

public function run()
{
$array = [];
foreach ($this::$variables as $property) {
$array[] = $property;
}
}
}

?>
-----
<?php

namespace Rector\Tests\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector\Fixture;

final class SomeStaticPropertyFetch
{
public static $variables = ['test'];

public function run()
{
$array = [];
foreach ($this::$variables as $variable) {
$array[] = $variable;
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector\Fixture;

final class SomeStaticPropertyFetch2
{
public static $variables = ['test'];

public function run()
{
$array = [];
foreach (self::$variables as $property) {
$array[] = $property;
}
}
}

?>
-----
<?php

namespace Rector\Tests\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector\Fixture;

final class SomeStaticPropertyFetch2
{
public static $variables = ['test'];

public function run()
{
$array = [];
foreach (self::$variables as $variable) {
$array[] = $variable;
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Foreach_;
use PHPStan\Type\ThisType;
use Rector\CodeQuality\NodeAnalyzer\ForeachAnalyzer;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\ExpectedNameResolver\InflectorSingularResolver;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -22,7 +26,8 @@ final class RenameForeachValueVariableToMatchExprVariableRector extends Abstract
{
public function __construct(
private InflectorSingularResolver $inflectorSingularResolver,
private ForeachAnalyzer $foreachAnalyzer
private ForeachAnalyzer $foreachAnalyzer,
private PropertyFetchAnalyzer $propertyFetchAnalyzer
) {
}

Expand Down Expand Up @@ -73,15 +78,18 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $node->expr instanceof Variable && ! $node->expr instanceof PropertyFetch) {
$isPropertyFetch = $this->propertyFetchAnalyzer->isPropertyFetch($node->expr);
if (! $node->expr instanceof Variable && ! $isPropertyFetch) {
return null;
}

if ($this->isNotThisTypePropertyFetch($node->expr)) {
/** @var Variable|PropertyFetch|StaticPropertyFetch $expr */
$expr = $node->expr;
if ($this->isNotCurrentClassLikePropertyFetch($expr, $isPropertyFetch)) {
return null;
}

$exprName = $this->getName($node->expr);
$exprName = $this->getName($expr);
if ($exprName === null) {
return null;
}
Expand Down Expand Up @@ -111,14 +119,27 @@ public function refactor(Node $node): ?Node
return $this->processRename($node, $valueVarName, $singularValueVarName);
}

private function isNotThisTypePropertyFetch(PropertyFetch|Variable $expr): bool
{
if ($expr instanceof PropertyFetch) {
$variableType = $this->getType($expr->var);
return ! $variableType instanceof ThisType;
private function isNotCurrentClassLikePropertyFetch(
PropertyFetch|StaticPropertyFetch|Variable $expr,
bool $isPropertyFetch
): bool {
if (! $isPropertyFetch) {
return false;
}

/** @var PropertyFetch|StaticPropertyFetch $expr */
$variableType = $expr instanceof PropertyFetch
? $this->nodeTypeResolver->getType($expr->var)
: $this->nodeTypeResolver->getType($expr->class);

if ($variableType instanceof FullyQualifiedObjectType) {
$currentClassLike = $this->betterNodeFinder->findParentType($expr, ClassLike::class);
if ($currentClassLike instanceof ClassLike) {
return ! $this->nodeNameResolver->isName($currentClassLike, $variableType->getClassName());
}
}

return false;
return ! $variableType instanceof ThisType;
}

private function processRename(Foreach_ $foreach, string $valueVarName, string $singularValueVarName): Foreach_
Expand Down