Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d940f42
[Down_To_PHP71] Handle Downgrade Param Widening + Downgrade Reflectio…
samsonasik Oct 18, 2022
f046709
Fixed :tada:
samsonasik Oct 18, 2022
224f523
move update and connect parent
samsonasik Oct 18, 2022
1f796e2
try ensure collect nodes to return
samsonasik Oct 18, 2022
3ad19c6
rollback
samsonasik Oct 18, 2022
2c9760c
Fix
samsonasik Oct 18, 2022
b12de60
[ci-review] Rector Rectify
actions-user Oct 18, 2022
b7fd1cb
check not Param
samsonasik Oct 18, 2022
8df2a20
check not Param
samsonasik Oct 18, 2022
6d1332f
phpstan
samsonasik Oct 18, 2022
c67b686
rollback
samsonasik Oct 18, 2022
6ed9db1
re-run refactor() when node is same and phpdoc info changed
samsonasik Oct 19, 2022
3672e38
add missing return
samsonasik Oct 19, 2022
993bc36
try setDocComment() on phpdocInfo->markAsChanged()
samsonasik Oct 19, 2022
6e5add8
Revert try setDocComment() on phpdocInfo->markAsChanged()
samsonasik Oct 19, 2022
3602b37
Early refresh PhpDocInfo before check to ensure doc node is latest up…
samsonasik Oct 19, 2022
5fe3ca0
fix
samsonasik Oct 19, 2022
c56efe2
clean up
samsonasik Oct 19, 2022
1213387
clean up comment
samsonasik Oct 19, 2022
481636e
clean up comment
samsonasik Oct 19, 2022
0533336
clean up comment
samsonasik Oct 19, 2022
5dfd46a
cs fix
samsonasik Oct 19, 2022
0817b33
move on loop
samsonasik Oct 19, 2022
a24332d
final touch: set comments attribute value to null when no children
samsonasik Oct 19, 2022
4fc2e8f
Final touch: move logic to DocBlockUpdater and differentiate between …
samsonasik Oct 19, 2022
4d1d9ec
final touch: clean up
samsonasik Oct 19, 2022
4dcc48d
final touch: clean up
samsonasik Oct 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}

?>