-
-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UseAnalyzer test + use returned variables (#1148)
- Loading branch information
1 parent
f9f4b93
commit 502cc04
Showing
13 changed files
with
198 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages-tests/NameImporting/NodeAnalyzer/UseAnalyzer/Fixture/some_uses.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\NameImporting\NodeAnalyzer\UseAnalyzer\Fixture; | ||
|
||
use Rector\Tests\NameImporting\NodeAnalyzer\UseAnalyzer\Source\FirstUsage; | ||
|
||
final class SomeUses | ||
{ | ||
public function run() | ||
{ | ||
return new FirstUsage(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages-tests/NameImporting/NodeAnalyzer/UseAnalyzer/Source/FirstUsage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\NameImporting\NodeAnalyzer\UseAnalyzer\Source; | ||
|
||
final class FirstUsage | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\NameImporting\NodeAnalyzer; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\ClassLike; | ||
use PhpParser\Node\Stmt\UseUse; | ||
use Rector\Core\Exception\ShouldNotHappenException; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
use Rector\NameImporting\ValueObject\NameAndParent; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
|
||
/** | ||
* @see \Rector\Tests\NameImporting\NodeAnalyzer\UseAnalyzer\UseAnalyzerTest | ||
*/ | ||
final class UseAnalyzer | ||
{ | ||
public function __construct( | ||
private BetterNodeFinder $betterNodeFinder, | ||
private NodeNameResolver $nodeNameResolver | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<string, NameAndParent[]> | ||
*/ | ||
public function resolveUsedNameNodes(Node $node): array | ||
{ | ||
$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(Node $node): array | ||
{ | ||
$namesAndParentsByShortName = []; | ||
|
||
/** @var Name[] $names */ | ||
$names = $this->betterNodeFinder->findInstanceOf($node, Name::class); | ||
|
||
foreach ($names as $name) { | ||
/** node name before becoming FQN - attribute from @see NameResolver */ | ||
$originalName = $name->getAttribute(AttributeKey::ORIGINAL_NAME); | ||
if (! $originalName instanceof Name) { | ||
continue; | ||
} | ||
|
||
$parentNode = $name->getAttribute(AttributeKey::PARENT_NODE); | ||
if (! $parentNode instanceof Node) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
$shortName = $originalName->toString(); | ||
$namesAndParentsByShortName[$shortName][] = new NameAndParent($name, $parentNode); | ||
} | ||
|
||
return $namesAndParentsByShortName; | ||
} | ||
|
||
/** | ||
* @return array<string, NameAndParent[]> | ||
*/ | ||
private function resolveUsedClassNames(Node $node): array | ||
{ | ||
$namesAndParentsByShortName = []; | ||
|
||
/** @var ClassLike[] $classLikes */ | ||
$classLikes = $this->betterNodeFinder->findClassLikes($node); | ||
|
||
foreach ($classLikes as $classLike) { | ||
$classLikeName = $classLike->name; | ||
if (! $classLikeName instanceof Identifier) { | ||
continue; | ||
} | ||
|
||
$name = $this->nodeNameResolver->getName($classLikeName); | ||
if ($name === null) { | ||
continue; | ||
} | ||
|
||
$namesAndParentsByShortName[$name][] = new NameAndParent($classLikeName, $classLike); | ||
} | ||
|
||
return $namesAndParentsByShortName; | ||
} | ||
|
||
/** | ||
* @return array<string, NameAndParent[]> | ||
*/ | ||
private function resolveTraitUseNames(Node $node): array | ||
{ | ||
$namesAndParentsByShortName = []; | ||
|
||
/** @var Identifier[] $identifiers */ | ||
$identifiers = $this->betterNodeFinder->findInstanceOf($node, Identifier::class); | ||
|
||
foreach ($identifiers as $identifier) { | ||
$parentNode = $identifier->getAttribute(AttributeKey::PARENT_NODE); | ||
if (! $parentNode instanceof UseUse) { | ||
continue; | ||
} | ||
|
||
$shortName = $identifier->name; | ||
$namesAndParentsByShortName[$shortName][] = new NameAndParent($identifier, $parentNode); | ||
} | ||
|
||
return $namesAndParentsByShortName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
rules-tests/CodingStyle/Node/UseManipulator/Fixture/some_uses.php.inc
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
rules-tests/CodingStyle/Node/UseManipulator/Source/FirstUsage.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.