Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PostRector] Skip remove use statement on used in annotation on remov…
…eUnusedImports() (#5657) * [PostRector] Skip remove use statement on used in annotation on removeUnusedImports() * Fixed 🎉 * [ci-review] Rector Rectify * phpstan fix --------- Co-authored-by: GitHub Action <actions@github.com>
- Loading branch information
1 parent
fb312e0
commit b9ea5fd
Showing
5 changed files
with
117 additions
and
0 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
67 changes: 67 additions & 0 deletions
67
src/BetterPhpDocParser/PhpDocParser/ArrayItemClassNameDecorator.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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\BetterPhpDocParser\PhpDocParser; | ||
|
||
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode; | ||
use PhpParser\Node as PhpNode; | ||
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode; | ||
use PHPStan\PhpDocParser\Ast\Node; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; | ||
use Rector\BetterPhpDocParser\Contract\PhpDocParser\PhpDocNodeDecoratorInterface; | ||
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey; | ||
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser; | ||
use Rector\StaticTypeMapper\Naming\NameScopeFactory; | ||
|
||
/** | ||
* Decorate node with fully qualified class name for annotation: | ||
* e.g. @ORM\Column(type=Types::STRING, length=100, nullable=false) | ||
*/ | ||
final readonly class ArrayItemClassNameDecorator implements PhpDocNodeDecoratorInterface | ||
{ | ||
public function __construct( | ||
private NameScopeFactory $nameScopeFactory, | ||
private PhpDocNodeTraverser $phpDocNodeTraverser | ||
) { | ||
} | ||
|
||
public function decorate(PhpDocNode $phpDocNode, PhpNode $phpNode): void | ||
{ | ||
// iterating all phpdocs has big overhead. peek into the phpdoc to exit early | ||
if (! str_contains($phpDocNode->__toString(), '::')) { | ||
return; | ||
} | ||
|
||
$this->phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', function (Node $node) use ( | ||
$phpNode | ||
): Node|null { | ||
if (! $node instanceof ArrayItemNode) { | ||
return null; | ||
} | ||
|
||
if (! is_string($node->value)) { | ||
return null; | ||
} | ||
|
||
$splitScopeResolution = explode('::', $node->value); | ||
if (count($splitScopeResolution) !== 2) { | ||
return null; | ||
} | ||
|
||
$firstName = $splitScopeResolution[0]; | ||
$constFetchNode = new ConstFetchNode($firstName, $firstName); | ||
|
||
$className = $this->resolveFullyQualifiedClass($constFetchNode, $phpNode); | ||
$node->setAttribute(PhpDocAttributeKey::RESOLVED_CLASS, $className); | ||
|
||
return $node; | ||
}); | ||
} | ||
|
||
private function resolveFullyQualifiedClass(ConstFetchNode $constFetchNode, PhpNode $phpNode): string | ||
{ | ||
$nameScope = $this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($phpNode); | ||
return $nameScope->resolveStringName($constFetchNode->className); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
tests/Issues/NoNamespaced/Fixture/skip_used_in_annotation.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,16 @@ | ||
<?php | ||
|
||
use App\Repository\DemoRepository; | ||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
/** | ||
* @ORM\Entity(repositoryClass=DemoRepository::class) | ||
*/ | ||
final class SkipUsedInAnnotation | ||
{ | ||
/** | ||
* @ORM\Column(type=Types::STRING, length=100, nullable=false) | ||
*/ | ||
protected $status; | ||
} |