Skip to content

Commit

Permalink
rename vairabe
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 5, 2021
1 parent cc808b9 commit fbcde09
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Rector\Tests\NameImporting\NodeAnalyzer\UseAnalyzer;

use Iterator;
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use Rector\NameImporting\NodeAnalyzer\UseAnalyzer;
Expand All @@ -32,18 +34,19 @@ protected function setUp(): void
/**
* @dataProvider provideData()
*
* @param class-string<\PhpParser\Node> $parentNodeType
* @param class-string<Node> $parentNodeType
*/
public function test(
string $filePath,
string $expectedShortName,
int $position,
\PhpParser\Node $expectedNameNode,
Name|Identifier $expectedNameNode,
string $parentNodeType
): void {
$file = $this->testingParser->parseFilePathToFile($filePath);
$firstStmt = $file->getOldStmts()[1];

$namesAndParentsByShortName = $this->useAnalyzer->resolveUsedNameNodes($file);
$namesAndParentsByShortName = $this->useAnalyzer->resolveUsedNameNodes($firstStmt);
$this->assertArrayHasKey($expectedShortName, $namesAndParentsByShortName);

$namesAndParents = $namesAndParentsByShortName[$expectedShortName];
Expand Down
21 changes: 10 additions & 11 deletions packages/NameImporting/NodeAnalyzer/UseAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PhpParser\Node\Stmt\UseUse;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\ValueObject\Application\File;
use Rector\NameImporting\ValueObject\NameAndParent;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -30,24 +29,24 @@ public function __construct(
/**
* @return array<string, NameAndParent[]>
*/
public function resolveUsedNameNodes(File $file): array
public function resolveUsedNameNodes(\PhpParser\Node $node): array
{
$usedNamesByShortName = $this->resolveUsedNames($file);
$usedClassNamesByShortName = $this->resolveUsedClassNames($file);
$usedTraitNamesByShortName = $this->resolveTraitUseNames($file);
$usedNamesByShortName = $this->resolveUsedNames($node);
$usedClassNamesByShortName = $this->resolveUsedClassNames($node);
$usedTraitNamesByShortName = $this->resolveTraitUseNames($node);

return array_merge($usedNamesByShortName, $usedClassNamesByShortName, $usedTraitNamesByShortName);
}

/**
* @return array<string, NameAndParent[]>
*/
private function resolveUsedNames(File $file): array
private function resolveUsedNames(\PhpParser\Node $node): array
{
$namesAndParentsByShortName = [];

/** @var Name[] $names */
$names = $this->betterNodeFinder->findInstanceOf($file->getOldStmts(), Name::class);
$names = $this->betterNodeFinder->findInstanceOf($node, Name::class);

foreach ($names as $name) {
/** node name before becoming FQN - attribute from @see NameResolver */
Expand All @@ -71,12 +70,12 @@ private function resolveUsedNames(File $file): array
/**
* @return array<string, NameAndParent[]>
*/
private function resolveUsedClassNames(File $file): array
private function resolveUsedClassNames(\PhpParser\Node $node): array
{
$namesAndParentsByShortName = [];

/** @var ClassLike[] $classLikes */
$classLikes = $this->betterNodeFinder->findClassLikes($file->getOldStmts());
$classLikes = $this->betterNodeFinder->findClassLikes($node);

foreach ($classLikes as $classLike) {
$classLikeName = $classLike->name;
Expand All @@ -98,12 +97,12 @@ private function resolveUsedClassNames(File $file): array
/**
* @return array<string, NameAndParent[]>
*/
private function resolveTraitUseNames(File $file): array
private function resolveTraitUseNames(\PhpParser\Node $node): array
{
$namesAndParentsByShortName = [];

/** @var Identifier[] $identifiers */
$identifiers = $this->betterNodeFinder->findInstanceOf($file->getOldStmts(), Identifier::class);
$identifiers = $this->betterNodeFinder->findInstanceOf($node, Identifier::class);

foreach ($identifiers as $identifier) {
$parentNode = $identifier->getAttribute(AttributeKey::PARENT_NODE);
Expand Down
14 changes: 0 additions & 14 deletions packages/Testing/TestingParser/TestingParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,4 @@ public function parseFileToDecoratedNodes(string $file): array
$file = new File($smartFileInfo, $smartFileInfo->getContents());
return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $nodes);
}
<<<<<<< HEAD

public function parseFilePathToFile(string $filePath): File
{
$smartFileInfo = new SmartFileInfo($filePath);
$file = new File($smartFileInfo, $smartFileInfo->getContents());

$stmts = $this->rectorParser->parseFile($smartFileInfo);
$file->hydrateStmtsAndTokens($stmts, $stmts, []);

return $file;
}
=======
>>>>>>> b529e101b... add ShortNameResolverTest
}

This file was deleted.

10 changes: 0 additions & 10 deletions rules-tests/CodingStyle/Node/UseManipulator/Source/FirstUsage.php

This file was deleted.

79 changes: 0 additions & 79 deletions rules-tests/CodingStyle/Node/UseManipulator/UseManipulatorTest.php

This file was deleted.

8 changes: 7 additions & 1 deletion rules/CodingStyle/Naming/NameRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PhpParser\Node\UnionType;
use Rector\NameImporting\ValueObject\NameAndParent;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class NameRenamer
{
Expand Down Expand Up @@ -125,7 +126,12 @@ private function renameTraitUse(string $lastName, TraitUse $traitUse, Name | Ide
continue;
}

$traitUse->traits[$key] = new Name($lastName);
$oldTraitName = $traitUse->traits[$key];
$newTraitName = new Name($lastName);
$newTraitName->setAttributes($oldTraitName->getAttributes());
$newTraitName->setAttribute(AttributeKey::ORIGINAL_NODE, null);

$traitUse->traits[$key] = $newTraitName;
}
}

Expand Down
5 changes: 2 additions & 3 deletions rules/CodingStyle/Node/DocAliasResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\ValueObject\Application\File;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;

Expand All @@ -32,11 +31,11 @@ public function __construct(
/**
* @return string[]
*/
public function resolve(File $file): array
public function resolve(\PhpParser\Node $node): array
{
$possibleDocAliases = [];

$this->simpleCallableNodeTraverser->traverseNodesWithCallable($file->getNewStmts(), function (Node $node) use (
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($node, function (Node $node) use (
&$possibleDocAliases
): void {
$docComment = $node->getDocComment();
Expand Down
39 changes: 30 additions & 9 deletions rules/CodingStyle/Rector/Use_/RemoveUnusedAliasRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class RemoveUnusedAliasRector extends AbstractRector
/**
* @var array<string, NameAndParent[]>
*/
private array $resolvedNodeNames = [];
private array $resolvedNamesAndParentsByShortName = [];

/**
* @var array<string, string[]>
Expand All @@ -43,7 +43,7 @@ final class RemoveUnusedAliasRector extends AbstractRector

public function __construct(
private DocAliasResolver $docAliasResolver,
private UseAnalyzer $useManipulator,
private UseAnalyzer $useAnalyzer,
private UseNameAliasToNameResolver $useNameAliasToNameResolver,
private NameRenamer $nameRenamer,
private ClassNameImportSkipper $classNameImportSkipper,
Expand Down Expand Up @@ -94,15 +94,23 @@ public function refactor(Node $node): ?Node
return null;
}

$this->resolvedNodeNames = $this->useManipulator->resolveUsedNameNodes($this->file);
$this->resolvedDocPossibleAliases = $this->docAliasResolver->resolve($this->file);
$searchNode = $this->resolveSearchNode($node);
if (! $searchNode instanceof \PhpParser\Node) {
return null;
}

$this->resolvedNamesAndParentsByShortName = $this->useAnalyzer->resolveUsedNameNodes($searchNode);
$this->resolvedDocPossibleAliases = $this->docAliasResolver->resolve($searchNode);
$this->useNamesAliasToName = $this->useNameAliasToNameResolver->resolve($this->file);

// lowercase
$this->resolvedDocPossibleAliases = $this->lowercaseArray($this->resolvedDocPossibleAliases);

$this->resolvedNodeNames = array_change_key_case($this->resolvedNodeNames, CASE_LOWER);
$this->resolvedNamesAndParentsByShortName = array_change_key_case(
$this->resolvedNamesAndParentsByShortName,
CASE_LOWER
);

$this->useNamesAliasToName = array_change_key_case($this->useNamesAliasToName, CASE_LOWER);

foreach ($node->uses as $use) {
Expand All @@ -120,7 +128,7 @@ public function refactor(Node $node): ?Node
}

// only last name is used → no need for alias
if (isset($this->resolvedNodeNames[$lowercasedLastName])) {
if (isset($this->resolvedNamesAndParentsByShortName[$lowercasedLastName])) {
$use->alias = null;
continue;
}
Expand Down Expand Up @@ -158,7 +166,7 @@ private function shouldSkip(Use_ $use, string $lastName, string $aliasName): boo
$loweredAliasName = strtolower($aliasName);

// both are used → nothing to remove
if (isset($this->resolvedNodeNames[$loweredLastName], $this->resolvedNodeNames[$loweredAliasName])) {
if (isset($this->resolvedNamesAndParentsByShortName[$loweredLastName], $this->resolvedNamesAndParentsByShortName[$loweredAliasName])) {
return true;
}

Expand Down Expand Up @@ -191,11 +199,14 @@ private function refactorAliasName(Use_ $use, string $fullUseUseName, string $la
}

$loweredFullUseUseName = strtolower($fullUseUseName);
if (! isset($this->resolvedNodeNames[$loweredFullUseUseName])) {
if (! isset($this->resolvedNamesAndParentsByShortName[$loweredFullUseUseName])) {
return;
}

$this->nameRenamer->renameNameNode($this->resolvedNodeNames[$loweredFullUseUseName], $lastName);
$this->nameRenamer->renameNameNode(
$this->resolvedNamesAndParentsByShortName[$loweredFullUseUseName],
$lastName
);
$useUse->alias = null;
}

Expand All @@ -209,4 +220,14 @@ private function hasUseAlias(Use_ $use): bool

return false;
}

private function resolveSearchNode(Use_ $use): ?Node
{
$searchNode = $use->getAttribute(AttributeKey::PARENT_NODE);
if ($searchNode !== null) {
return $searchNode;
}

return $use->getAttribute(AttributeKey::NEXT_NODE);
}
}

0 comments on commit fbcde09

Please sign in to comment.