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

[CodingStyle] Remove usage of Reflection::expandClassName() from nette/utils 4.0 as cause bug on downgrade #5740

Merged
merged 4 commits into from
Mar 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 3 additions & 38 deletions rules/CodingStyle/ClassNameImport/ShortNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\CodingStyle\ClassNameImport;

use Nette\Utils\Reflection;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
Expand All @@ -13,8 +12,6 @@
use PhpParser\Node\Stmt\Namespace_;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\CodingStyle\NodeAnalyzer\UseImportNameMatcher;
Expand All @@ -25,7 +22,6 @@
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\ValueObject\Application\File;
use ReflectionClass;

/**
* @see \Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\ShortNameResolverTest
Expand All @@ -40,7 +36,6 @@ final class ShortNameResolver
public function __construct(
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private readonly NodeNameResolver $nodeNameResolver,
private readonly ReflectionProvider $reflectionProvider,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly UseImportNameMatcher $useImportNameMatcher,
private readonly PhpDocInfoFactory $phpDocInfoFactory
Expand Down Expand Up @@ -148,8 +143,6 @@ private function resolveForStmts(array $stmts): array
*/
private function resolveFromStmtsDocBlocks(array $stmts): array
{
$classReflection = $this->resolveClassReflection($stmts);

$shortNames = [];
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use (
&$shortNames
Expand Down Expand Up @@ -185,50 +178,22 @@ static function ($node) use (&$shortNames): null {
return null;
});

return $this->fqnizeShortNames($shortNames, $classReflection, $stmts);
}

/**
* @param Node[] $stmts
*/
private function resolveClassReflection(array $stmts): ?ClassReflection
{
$firstClassLike = $this->betterNodeFinder->findFirstInstanceOf($stmts, ClassLike::class);
if (! $firstClassLike instanceof ClassLike) {
return null;
}

$className = (string) $this->nodeNameResolver->getName($firstClassLike);
if (! $this->reflectionProvider->hasClass($className)) {
return null;
}

return $this->reflectionProvider->getClass($className);
return $this->fqnizeShortNames($shortNames, $stmts);
}

/**
* @param string[] $shortNames
* @param Stmt[] $stmts
* @return array<string, string>
*/
private function fqnizeShortNames(array $shortNames, ?ClassReflection $classReflection, array $stmts): array
private function fqnizeShortNames(array $shortNames, array $stmts): array
{
$shortNamesToFullyQualifiedNames = [];

$nativeReflectionClass = $classReflection instanceof ClassReflection && ! $classReflection->isAnonymous()
? $classReflection->getNativeReflection()
: null;

foreach ($shortNames as $shortName) {
$stmtsMatchedName = $this->useImportNameMatcher->matchNameWithStmts($shortName, $stmts);

if ($nativeReflectionClass instanceof ReflectionClass) {
$fullyQualifiedName = Reflection::expandClassName($shortName, $nativeReflectionClass);
} elseif (is_string($stmtsMatchedName)) {
$fullyQualifiedName = $stmtsMatchedName;
} else {
$fullyQualifiedName = $shortName;
}
$fullyQualifiedName = is_string($stmtsMatchedName) ? $stmtsMatchedName : $shortName;

$shortNamesToFullyQualifiedNames[$shortName] = $fullyQualifiedName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Rector\Tests\Issues\RenameAnnotationToAttributeAutoImport\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

#[Route(path: '/pro/{id}/networks/{networkId}/sectors', name: 'api_network_sectors', requirements: ['id' => '\d+', 'networkId' => '\d+'])]
#[\Symfony\Component\Security\Http\Attribute\IsGranted('TEST')]
class WithExistingAttribute extends AbstractController
{
}

?>
-----
<?php

namespace Rector\Tests\Issues\RenameAnnotationToAttributeAutoImport\Fixture;

use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

#[Route(path: '/pro/{id}/networks/{networkId}/sectors', name: 'api_network_sectors', requirements: ['id' => '\d+', 'networkId' => '\d+'])]
#[IsGranted('TEST')]
class WithExistingAttribute extends AbstractController
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ class WithExistingAttribute extends AbstractController

namespace Rector\Tests\Issues\RenameAnnotationToAttributeAutoImport\Fixture;

use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

#[Route(path: '/pro/{id}/networks/{networkId}/sectors', name: 'api_network_sectors', requirements: ['id' => '\d+', 'networkId' => '\d+'])]
#[IsGranted('TEST')]
#[\Symfony\Component\Security\Http\Attribute\IsGranted('TEST')]
Copy link
Member Author

@samsonasik samsonasik Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomasVotruba this is only drawback for this, as this consist of rename + make attribute, and run twice will auto-import it, see fixture https://github.com/rectorphp/rector-src/pull/5740/files#diff-fe6ad097b1f7d2f4670de3d6f764aabffb83f16862d8d7893e464012e0be1702

which I think it is ok :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me :)

class WithExistingAttribute extends AbstractController
{
}
Expand Down