Skip to content

Commit

Permalink
Improve check for changed phpdoc and add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Sep 12, 2024
1 parent a34fc3c commit 13b8110
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

?>
-----
<?php
Expand Down Expand Up @@ -74,4 +82,12 @@ $bar = getInt();
/** @var string $foo this is the string */
$foo = getString();

/* normal comment */
$foo = getString();
assert(is_string($foo));

// normal comment
$foo = getString();
assert(is_string($foo));

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/** @phpstan-var string $foo */
$foo = getString();

/** @psalm-var string $foo */
$foo = getString();

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\TypeDeclaration\PhpDocParser\TypeExpressionFromVarTagResolver;
use Rector\ValueObject\PhpVersionFeature;
Expand Down Expand Up @@ -109,7 +108,6 @@ public function refactor(Node $node): ?array
if ($typeExpression !== false) {
$this->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]);
Expand Down
8 changes: 6 additions & 2 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 13b8110

Please sign in to comment.