Skip to content

Commit

Permalink
[Down_To_PHP71] Handle Downgrade Param Widening + Downgrade Reflectio…
Browse files Browse the repository at this point in the history
…n Get on DowngradeLevelSetList::DOWN_TO_PHP_71 (#3001)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Oct 20, 2022
1 parent e4ec1f9 commit 4cecebf
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
37 changes: 32 additions & 5 deletions packages/Comments/NodeDocBlock/DocBlockUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ public function __construct(
public function updateNodeWithPhpDocInfo(Node $node): void
{
// nothing to change? don't save it
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
$phpDocInfo = $this->resolveChangedPhpDocInfo($node);
if (! $phpDocInfo instanceof PhpDocInfo) {
return;
}

if (! $phpDocInfo->hasChanged()) {
return;
}

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

// make sure, that many separated comments are not removed
Expand All @@ -51,6 +47,37 @@ public function updateNodeWithPhpDocInfo(Node $node): void
$node->setDocComment(new Doc($phpDoc));
}

public function updateRefactoredNodeWithPhpDocInfo(Node $node): void
{
// nothing to change? don't save it
$phpDocInfo = $this->resolveChangedPhpDocInfo($node);
if (! $phpDocInfo instanceof PhpDocInfo) {
return;
}

$phpDocNode = $phpDocInfo->getPhpDocNode();
if ($phpDocNode->children === []) {
$node->setAttribute(AttributeKey::COMMENTS, null);
return;
}

$node->setDocComment(new Doc((string) $phpDocNode));
}

private function resolveChangedPhpDocInfo(Node $node): ?PhpDocInfo
{
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if (! $phpDocInfo instanceof PhpDocInfo) {
return null;
}

if (! $phpDocInfo->hasChanged()) {
return null;
}

return $phpDocInfo;
}

private function printPhpDocInfoToString(PhpDocInfo $phpDocInfo): string
{
if ($phpDocInfo->isNewNode()) {
Expand Down
13 changes: 12 additions & 1 deletion src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\Application\ChangedNodeScopeRefresher;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Console\Output\RectorOutputStyle;
Expand Down Expand Up @@ -116,6 +117,8 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn

private FilePathHelper $filePathHelper;

private DocBlockUpdater $docBlockUpdater;

#[Required]
public function autowire(
NodesToRemoveCollector $nodesToRemoveCollector,
Expand All @@ -138,7 +141,8 @@ public function autowire(
CreatedByRuleDecorator $createdByRuleDecorator,
ChangedNodeScopeRefresher $changedNodeScopeRefresher,
RectorOutputStyle $rectorOutputStyle,
FilePathHelper $filePathHelper
FilePathHelper $filePathHelper,
DocBlockUpdater $docBlockUpdater
): void {
$this->nodesToRemoveCollector = $nodesToRemoveCollector;
$this->nodeRemover = $nodeRemover;
Expand All @@ -161,6 +165,7 @@ public function autowire(
$this->changedNodeScopeRefresher = $changedNodeScopeRefresher;
$this->rectorOutputStyle = $rectorOutputStyle;
$this->filePathHelper = $filePathHelper;
$this->docBlockUpdater = $docBlockUpdater;
}

/**
Expand Down Expand Up @@ -356,6 +361,12 @@ private function refreshScopeNodes(array | Node $node, string $filePath, ?Mutati
$nodes = $node instanceof Node ? [$node] : $node;

foreach ($nodes as $node) {
/**
* Early refresh Doc Comment of Node before refresh Scope to ensure doc node is latest update
* to make PHPStan type can be correctly detected
*/
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

$this->changedNodeScopeRefresher->refresh($node, $mutatingScope, $filePath);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Rector\Core\Tests\Issues\IssueDowngradeParamType\FixtureDownToPhp71;

use ReflectionClass;

class DowngradeParamReflectionGetAttribute
{
public function run(ReflectionClass $reflectionClass)
{
echo $reflectionClass->getFileName();

if ($reflectionClass->getAttributes()) {
return true;
}
return false;
}
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\IssueDowngradeParamType\FixtureDownToPhp71;

use ReflectionClass;

class DowngradeParamReflectionGetAttribute
{
/**
* @param \ReflectionClass $reflectionClass
*/
public function run($reflectionClass)
{
echo $reflectionClass->getFileName();

if (method_exists($reflectionClass, 'getAttributes') ? $reflectionClass->getAttributes() : []) {
return true;
}
return false;
}
}

?>

0 comments on commit 4cecebf

Please sign in to comment.