Skip to content

Commit 286be03

Browse files
committed
Fix print
1 parent 67da1fc commit 286be03

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

rules/DowngradePhp81/Rector/Property/DowngradeReadonlyPropertyRector.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Param;
9+
use PhpParser\Node\Stmt\ClassMethod;
910
use PhpParser\Node\Stmt\Property;
1011
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
1112
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
1213
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1314
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
15+
use Rector\NodeTypeResolver\Node\AttributeKey;
1416
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
1517
use Rector\Rector\AbstractRector;
18+
use Rector\ValueObject\MethodName;
1619
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1720
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1821

@@ -40,7 +43,7 @@ public function __construct(
4043
*/
4144
public function getNodeTypes(): array
4245
{
43-
return [Property::class, Param::class];
46+
return [Property::class, ClassMethod::class];
4447
}
4548

4649
public function getRuleDefinition(): RuleDefinition
@@ -81,30 +84,60 @@ public function __construct()
8184
}
8285

8386
/**
84-
* @param Property|Param $node
87+
* @param Property|ClassMethod $node
8588
*/
8689
public function refactor(Node $node): ?Node
8790
{
88-
if (! $this->visibilityManipulator->isReadonly($node)) {
91+
if ($node instanceof Property) {
92+
if (! $this->visibilityManipulator->isReadonly($node)) {
93+
return null;
94+
}
95+
96+
$this->addPhpDocTag($node);
97+
$this->visibilityManipulator->removeReadonly($node);
98+
return $node;
99+
}
100+
101+
if (! $this->isName($node, MethodName::CONSTRUCT)) {
89102
return null;
90103
}
91104

92-
$this->addPhpDocTag($node);
105+
$hasChangedDoc = false;
106+
$hasChanged = false;
107+
foreach ($node->params as $param) {
108+
if (! $this->visibilityManipulator->isReadonly($param)) {
109+
continue;
110+
}
111+
112+
if ($this->addPhpDocTag($param)) {
113+
$hasChangedDoc = true;
114+
}
115+
116+
$this->visibilityManipulator->removeReadonly($param);
117+
$hasChanged = true;
118+
}
93119

94-
$this->visibilityManipulator->removeReadonly($node);
120+
if (! $hasChanged) {
121+
return null;
122+
}
123+
124+
if ($hasChangedDoc) {
125+
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
126+
}
95127

96128
return $node;
97129
}
98130

99-
private function addPhpDocTag(Property|Param $node): void
131+
private function addPhpDocTag(Property|Param $node): bool
100132
{
101133
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
102134

103135
if ($phpDocInfo->hasByName(self::TAGNAME)) {
104-
return;
136+
return false;
105137
}
106138

107139
$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@' . self::TAGNAME, new GenericTagValueNode('')));
108140
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
141+
return true;
109142
}
110143
}

tests/Issues/DowngradeReadonlyClassPropertyToPhp80/Fixture/fixture.php.inc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ namespace Rector\Tests\Issues\DowngradeReadonlyClassPropertyToPhp80\Fixture;
1717

1818
final class Fixture
1919
{
20-
public function __construct(public array $property)
20+
public function __construct(
21+
/**
22+
* @readonly
23+
*/
24+
public array $property
25+
)
2126
{
2227
}
2328
}

0 commit comments

Comments
 (0)