Skip to content

Commit

Permalink
[Comments] Move left over clean empty doc handling to DocBlockUpdater (
Browse files Browse the repository at this point in the history
…#6584)

* [Comments] Move left over empty doc handling to DocBlockUpdater

* [Comments] Move left over empty doc handling to DocBlockUpdater
  • Loading branch information
samsonasik authored Dec 14, 2024
1 parent 6192fe3 commit 484098b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
23 changes: 0 additions & 23 deletions rules/Php80/Rector/Class_/AnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Php80\Rector\Class_;

use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\ArrowFunction;
Expand All @@ -30,7 +29,6 @@
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Exception\Configuration\InvalidConfigurationException;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Php80\NodeFactory\AttrGroupsFactory;
use Rector\Php80\NodeManipulator\AttributeGroupNamedArgumentManipulator;
Expand Down Expand Up @@ -154,9 +152,6 @@ public function refactor(Node $node): ?Node
// 3. Reprint docblock
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

// 4. Left over comment removal
$this->cleanLeftOverComment($node);

$this->attributeGroupNamedArgumentManipulator->decorate($attributeGroups);
$node->attrGroups = array_merge($node->attrGroups, $attributeGroups);

Expand All @@ -177,24 +172,6 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::ATTRIBUTES;
}

private function cleanLeftOverComment(Node $node): void
{
$comments = $node->getComments();

if ($comments === []) {
return;
}

foreach ($comments as $key => $comment) {
if ($comment instanceof Doc && $comment->getText() === '') {
unset($comments[$key]);
continue;
}
}

$node->setAttribute(AttributeKey::COMMENTS, array_values($comments));
}

/**
* @return AttributeGroup[]
*/
Expand Down
13 changes: 12 additions & 1 deletion src/Comments/NodeDocBlock/DocBlockUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,24 @@ public function updateRefactoredNodeWithPhpDocInfo(Node $node): void
}

$printedPhpDoc = $this->printPhpDocInfoToString($phpDocInfo);

$node->setDocComment(new Doc($printedPhpDoc));

if ($printedPhpDoc === '') {
$this->clearEmptyDoc($node);
}
}

private function setCommentsAttribute(Node $node): void
{
$comments = array_filter($node->getComments(), static fn (Comment $comment): bool => ! $comment instanceof Doc);
$node->setAttribute(AttributeKey::COMMENTS, $comments);
$node->setAttribute(AttributeKey::COMMENTS, array_values($comments));
}

private function clearEmptyDoc(Node $node): void
{
$comments = array_filter($node->getComments(), static fn (Comment $comment): bool => ! $comment instanceof Doc || $comment->getText() !== '');
$node->setAttribute(AttributeKey::COMMENTS, array_values($comments));
}

private function printPhpDocInfoToString(PhpDocInfo $phpDocInfo): string
Expand Down

0 comments on commit 484098b

Please sign in to comment.