Skip to content

Commit

Permalink
Add UseAnalyzer test + use returned variables (#1148)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 5, 2021
1 parent f9f4b93 commit 502cc04
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 186 deletions.
1 change: 1 addition & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
__DIR__ . '/packages-tests/BetterPhpDocParser/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php',
__DIR__ . '/tests/PhpParser/Node/NodeFactoryTest.php',
__DIR__ . '/packages-tests/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser/StaticDoctrineAnnotationParserTest.php',
__DIR__ . '/packages-tests/NameImporting/NodeAnalyzer/UseAnalyzer/UseAnalyzerTest.php',
'*TypeResolverTest.php',
__DIR__ . '/rules-tests/CodingStyle/Node/UseManipulator/UseManipulatorTest.php',
],
Expand Down
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();
}
}
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
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Tests\CodingStyle\Node\UseManipulator;
namespace Rector\Tests\NameImporting\NodeAnalyzer\UseAnalyzer;

use Iterator;
use PhpParser\Node;
Expand All @@ -11,23 +11,23 @@
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use Rector\CodingStyle\Node\UseManipulator;
use Rector\CodingStyle\ValueObject\NameAndParent;
use Rector\NameImporting\NodeAnalyzer\UseAnalyzer;
use Rector\NameImporting\ValueObject\NameAndParent;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Testing\TestingParser\TestingParser;
use Rector\Tests\CodingStyle\Node\UseManipulator\Source\FirstUsage;
use Rector\Tests\NameImporting\NodeAnalyzer\UseAnalyzer\Source\FirstUsage;

final class UseManipulatorTest extends AbstractTestCase
final class UseAnalyzerTest extends AbstractTestCase
{
private UseManipulator $useManipulator;
private UseAnalyzer $useAnalyzer;

private TestingParser $testingParser;

protected function setUp(): void
{
$this->boot();

$this->useManipulator = $this->getService(UseManipulator::class);
$this->useAnalyzer = $this->getService(UseAnalyzer::class);
$this->testingParser = $this->getService(TestingParser::class);
}

Expand All @@ -45,10 +45,9 @@ public function test(
string $parentNodeType
): void {
$file = $this->testingParser->parseFilePathToFile($filePath);

$firstStmt = $file->getOldStmts()[1];

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

$namesAndParents = $namesAndParentsByShortName[$expectedShortName];
Expand Down
119 changes: 119 additions & 0 deletions packages/NameImporting/NodeAnalyzer/UseAnalyzer.php
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\CodingStyle\ValueObject;
namespace Rector\NameImporting\ValueObject;

use PhpParser\Node;
use PhpParser\Node\Identifier;
Expand Down
22 changes: 11 additions & 11 deletions packages/Testing/TestingParser/TestingParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ public function __construct(
) {
}

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;
}

/**
* @return Node[]
*/
Expand All @@ -40,15 +51,4 @@ public function parseFileToDecoratedNodes(string $file): array
$file = new File($smartFileInfo, $smartFileInfo->getContents());
return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $nodes);
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@

use Iterator;
use Rector\CodingStyle\ClassNameImport\ShortNameResolver;
use Rector\Core\PhpParser\Parser\RectorParser;
use Rector\Core\ValueObject\Application\File;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
use Rector\Testing\TestingParser\TestingParser;

final class ShortNameResolverTest extends AbstractTestCase
{
private ShortNameResolver $shortNameResolver;

private RectorParser $rectorParser;
private TestingParser $testingParser;

protected function setUp(): void
{
$this->boot();
$this->shortNameResolver = $this->getService(ShortNameResolver::class);
$this->rectorParser = $this->getService(RectorParser::class);
$this->testingParser = $this->getService(TestingParser::class);
}

/**
Expand All @@ -30,7 +28,7 @@ protected function setUp(): void
*/
public function test(string $filePath, array $expectedShortNames): void
{
$file = $this->createFileFromFilePath($filePath);
$file = $this->testingParser->parseFilePathToFile($filePath);
$shortNames = $this->shortNameResolver->resolveFromFile($file);

$this->assertSame($expectedShortNames, $shortNames);
Expand Down Expand Up @@ -62,15 +60,4 @@ public function provideData(): Iterator
'SecondLog' => 'Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\Source\SecondLog',
]];
}

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

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

return $file;
}
}

This file was deleted.

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

This file was deleted.

2 changes: 1 addition & 1 deletion rules/CodingStyle/Naming/NameRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\TraitUse;
use PhpParser\Node\UnionType;
use Rector\CodingStyle\ValueObject\NameAndParent;
use Rector\NameImporting\ValueObject\NameAndParent;
use Rector\NodeNameResolver\NodeNameResolver;

final class NameRenamer
Expand Down
Loading

0 comments on commit 502cc04

Please sign in to comment.