Skip to content

Commit f6caa1d

Browse files
authored
[DowngradePhp80] Skip native AllowDynamicProperties attribute on DowngradeAttributeToAnnotationRector (#248)
* [DowngradePhp80] Skip native AllowDynamicProperties attribute on DowngradeAttributeToAnnotationRector * fix * add fixture to reprint on same line * reprint with skipped attribute * future comment
1 parent 3b4abf1 commit f6caa1d

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;
4+
5+
use AllowDynamicProperties;
6+
7+
#[AllowDynamicProperties]class ReprintSameLine
8+
{
9+
}
10+
11+
?>
12+
-----
13+
<?php
14+
15+
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;
16+
17+
use AllowDynamicProperties;
18+
19+
#[AllowDynamicProperties]
20+
class ReprintSameLine
21+
{
22+
}
23+
24+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;
4+
5+
use AllowDynamicProperties;
6+
7+
#[AllowDynamicProperties]
8+
class User
9+
{
10+
}

rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Rector\Contract\Rector\ConfigurableRectorInterface;
2020
use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation;
2121
use Rector\NodeFactory\DoctrineAnnotationFactory;
22+
use Rector\NodeTypeResolver\Node\AttributeKey;
2223
use Rector\Rector\AbstractRector;
2324
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
2425
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -34,7 +35,7 @@ final class DowngradeAttributeToAnnotationRector extends AbstractRector implemen
3435
/**
3536
* @var string[]
3637
*/
37-
private const SKIPPED_ATTRIBUTES = ['Attribute', 'ReturnTypeWillChange'];
38+
private const SKIPPED_ATTRIBUTES = ['Attribute', 'ReturnTypeWillChange', 'AllowDynamicProperties'];
3839

3940
/**
4041
* @var DowngradeAttributeToAnnotation[]
@@ -105,9 +106,14 @@ public function refactor(Node $node): ?Node
105106
$this->isDowngraded = false;
106107

107108
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
109+
$requireReprint = false;
108110
foreach ($node->attrGroups as $attrGroup) {
109111
foreach ($attrGroup->attrs as $key => $attribute) {
110112
if ($this->shouldSkipAttribute($attribute)) {
113+
// avoid error on same line
114+
// as attribute ->getStartLine() always equal with node ->getStartLine()
115+
// can't validate it, so enforce to reprint later
116+
$requireReprint = true;
111117
continue;
112118
}
113119

@@ -145,6 +151,11 @@ public function refactor(Node $node): ?Node
145151
$this->cleanupEmptyAttrGroups($node);
146152

147153
if (! $this->isDowngraded) {
154+
if ($requireReprint) {
155+
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
156+
return $node;
157+
}
158+
148159
return null;
149160
}
150161

0 commit comments

Comments
 (0)