Skip to content

Commit

Permalink
[CodingStyle] Remove usage of Reflection::expandClassName() from nett…
Browse files Browse the repository at this point in the history
…e/utils 4.0 as cause bug on downgrade (#5740)

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

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* fixture update

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Mar 19, 2024
1 parent a78fb1b commit 1bf3947
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
41 changes: 3 additions & 38 deletions rules/CodingStyle/ClassNameImport/ShortNameResolver.php
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
@@ -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
{
}

?>
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')]
class WithExistingAttribute extends AbstractController
{
}
Expand Down

0 comments on commit 1bf3947

Please sign in to comment.