From 13b8110f08bfa38a7ad2911e02f9f9ff6f352ead Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Thu, 12 Sep 2024 10:07:23 +0200 Subject: [PATCH] Improve check for changed phpdoc and add new tests --- .../Fixture/preserve_comments.php.inc | 16 ++++++++++++++++ .../Fixture/skip_phpstan_and_psalm_tags.php.inc | 8 ++++++++ .../Expression/InlineVarDocTagToAssertRector.php | 2 -- src/Rector/AbstractRector.php | 8 ++++++-- 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/Expression/InlineVarDocTagToAssertRector/Fixture/skip_phpstan_and_psalm_tags.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/Expression/InlineVarDocTagToAssertRector/Fixture/preserve_comments.php.inc b/rules-tests/TypeDeclaration/Rector/Expression/InlineVarDocTagToAssertRector/Fixture/preserve_comments.php.inc index c32ec25508..f762f495d0 100644 --- a/rules-tests/TypeDeclaration/Rector/Expression/InlineVarDocTagToAssertRector/Fixture/preserve_comments.php.inc +++ b/rules-tests/TypeDeclaration/Rector/Expression/InlineVarDocTagToAssertRector/Fixture/preserve_comments.php.inc @@ -35,6 +35,14 @@ $bar = getInt(); /** @var string $foo this is the string */ $foo = getString(); +/* normal comment */ +/** @var string $foo */ +$foo = getString(); + +/** @var string $foo */ +// normal comment +$foo = getString(); + ?> ----- diff --git a/rules-tests/TypeDeclaration/Rector/Expression/InlineVarDocTagToAssertRector/Fixture/skip_phpstan_and_psalm_tags.php.inc b/rules-tests/TypeDeclaration/Rector/Expression/InlineVarDocTagToAssertRector/Fixture/skip_phpstan_and_psalm_tags.php.inc new file mode 100644 index 0000000000..671792b77f --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Expression/InlineVarDocTagToAssertRector/Fixture/skip_phpstan_and_psalm_tags.php.inc @@ -0,0 +1,8 @@ +phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $tagValueNode); $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); - $node->setAttribute(AttributeKey::DO_NOT_CHANGE, true); $arg = new Arg($typeExpression); $funcCall = new FuncCall(new Name('assert'), [$arg]); diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 1f76add243..8128f7841b 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -21,6 +21,7 @@ use PHPStan\Type\Type; use Rector\Application\ChangedNodeScopeRefresher; use Rector\Application\Provider\CurrentFileProvider; +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\ChangesReporting\ValueObject\RectorWithLineChange; use Rector\Contract\Rector\RectorInterface; use Rector\Exception\ShouldNotHappenException; @@ -263,11 +264,14 @@ protected function mirrorComments(Node $newNode, Node $oldNode): void return; } - if ($newNode->getAttribute(AttributeKey::DO_NOT_CHANGE) === true) { + $oldPhpDocInfo = $oldNode->getAttribute(AttributeKey::PHP_DOC_INFO); + $newPhpDocInfo = $newNode->getAttribute(AttributeKey::PHP_DOC_INFO); + + if ($newPhpDocInfo instanceof PhpDocInfo && $oldPhpDocInfo !== $newPhpDocInfo) { return; } - $newNode->setAttribute(AttributeKey::PHP_DOC_INFO, $oldNode->getAttribute(AttributeKey::PHP_DOC_INFO)); + $newNode->setAttribute(AttributeKey::PHP_DOC_INFO, $oldPhpDocInfo); if (! $newNode instanceof Nop) { $newNode->setAttribute(AttributeKey::COMMENTS, $oldNode->getAttribute(AttributeKey::COMMENTS)); }