Skip to content

Commit

Permalink
[CodeQuality][DeadCode] Handle SimplifyUselessVariableRector+RemoveOv…
Browse files Browse the repository at this point in the history
…erriddenValuesRector (#2277)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed May 10, 2022
1 parent e5a45af commit 30f2f91
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function refactor(Node $node): ?Node
*/
private function resolveAssignedVariables(FunctionLike $functionLike): array
{
return $this->betterNodeFinder->find($functionLike, function (Node $node): bool {
return $this->betterNodeFinder->find($functionLike, function (Node $node) use ($functionLike): bool {
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Assign) {
return false;
Expand Down Expand Up @@ -138,6 +138,11 @@ private function resolveAssignedVariables(FunctionLike $functionLike): array
return false;
}

$parentFunctionLike = $this->betterNodeFinder->findParentType($node, FunctionLike::class);
if ($parentFunctionLike !== $functionLike) {
return false;
}

return ! $this->reservedKeywordAnalyzer->isNativeVariable($node->name);
});
}
Expand Down
49 changes: 49 additions & 0 deletions tests/Issues/UselessVariableOverride/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\UselessVariableOverride\Fixture;

class Fixture
{
public function formatPlaces($geotools, $places): array
{
$features = \array_map(function (array $placeResource) use (
$geotools
) {
$distances = \array_map(static function (CoordinateInterface $coordinate) use ($geotools) {
return $geotools->distance()->flat();
}, []);

return new Feature($distances);
}, $places);

return $features;
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\UselessVariableOverride\Fixture;

class Fixture
{
public function formatPlaces($geotools, $places): array
{
return \array_map(function (array $placeResource) use (
$geotools
) {
$distances = \array_map(static function (CoordinateInterface $coordinate) use ($geotools) {
return $geotools->distance()->flat();
}, []);

return new Feature($distances);
}, $places);
}
}

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

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\UselessVariableOverride;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class UselessVariableOverrideTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
12 changes: 12 additions & 0 deletions tests/Issues/UselessVariableOverride/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\FunctionLike\RemoveOverriddenValuesRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(SimplifyUselessVariableRector::class);
$rectorConfig->rule(RemoveOverriddenValuesRector::class);
};

0 comments on commit 30f2f91

Please sign in to comment.