Skip to content

Commit

Permalink
[PostRector] Skip remove unused imports on used in multiple docs in s…
Browse files Browse the repository at this point in the history
…ingle node (#5604)

* [PostRector] Skip remove unused imports on used in multiple docs in single node

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Feb 12, 2024
1 parent df8d2b7 commit 8d524d4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/PostRector/Rector/UnusedImportRemovingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Rector\PostRector\Rector;

use Nette\Utils\Strings;
use PhpParser\Comment;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
Expand Down Expand Up @@ -116,18 +118,32 @@ private function findNamesInDocBlocks(Namespace_|FileWithoutNamespace $namespace
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($namespace, function (Node $node) use (
&$names
) {
if (! $node->hasAttribute(AttributeKey::COMMENTS)) {
$comments = $node->getComments();
if ($comments === []) {
return null;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$names = [...$names, ...$phpDocInfo->getAnnotationClassNames()];
$docs = array_filter($comments, static fn(Comment $comment): bool => $comment instanceof Doc);
if ($docs === []) {
return null;
}

$totalDocs = count($docs);
foreach ($docs as $doc) {
$nodeToCheck = $totalDocs === 1 ? $node : clone $node;
if ($totalDocs > 1) {
$nodeToCheck->setDocComment($doc);
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($nodeToCheck);
$names = [...$names, ...$phpDocInfo->getAnnotationClassNames()];

$constFetchNodeNames = $phpDocInfo->getConstFetchNodeClassNames();
$names = [...$names, ...$constFetchNodeNames];
$constFetchNodeNames = $phpDocInfo->getConstFetchNodeClassNames();
$names = [...$names, ...$constFetchNodeNames];

$genericTagClassNames = $phpDocInfo->getGenericTagClassNames();
$names = [...$names, ...$genericTagClassNames];
$genericTagClassNames = $phpDocInfo->getGenericTagClassNames();
$names = [...$names, ...$genericTagClassNames];
}
});

return $names;
Expand Down
15 changes: 15 additions & 0 deletions tests/Issues/NoNamespaced/Fixture/skip_used_in_multi_docs.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use bridge\workshop\viewmodel\AppointmentFormVM;
use bridge\workshop\business\LocationVO;

/** @var AppointmentFormVM $vm */

/** @var LocationVO|null $testLocation */
$testLocation = null;
foreach ($vm->locations as $location) {
if ($location->isTestLocation()) {
$testLocation = $location;
break;
}
}

0 comments on commit 8d524d4

Please sign in to comment.