Skip to content

Commit 17296b3

Browse files
authored
[DowngradePhp80] Skip Override attribute on DowngradeAttributeToAnnotationRector (#327)
1 parent 35a459d commit 17296b3

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;
4+
5+
#[\Attribute]final class AttributeSameLine
6+
{
7+
#[\ReturnTypeWillChange]
8+
public function getValue()
9+
{
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;
18+
19+
#[\Attribute]
20+
final class AttributeSameLine
21+
{
22+
#[\ReturnTypeWillChange]
23+
public function getValue()
24+
{
25+
}
26+
}
27+
28+
?>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;
4+
5+
final class WithOverride
6+
{
7+
#[\Override]public function action()
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;
17+
18+
final class WithOverride
19+
{
20+
#[\Override]
21+
public function action()
22+
{
23+
}
24+
}
25+
26+
?>

rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ final class DowngradeAttributeToAnnotationRector extends AbstractRector implemen
3434
/**
3535
* @var string[]
3636
*/
37-
private const SKIPPED_ATTRIBUTES = ['Attribute', 'ReturnTypeWillChange', 'AllowDynamicProperties'];
37+
private const SKIPPED_ATTRIBUTES = [
38+
'Attribute',
39+
'ReturnTypeWillChange',
40+
'AllowDynamicProperties',
41+
'Override',
42+
'SensitiveParameter',
43+
];
3844

3945
/**
4046
* @var DowngradeAttributeToAnnotation[]
@@ -114,8 +120,15 @@ public function refactor(Node $node): ?Node
114120
(string) $oldTokens[$attrGroup->getEndTokenPos() + 1],
115121
"\n"
116122
)) {
123+
if ($node->getStartTokenPos() === strlen($attribute->name->toString()) - 2) {
124+
$indentation = '';
125+
} else {
126+
$indent = $attrGroup->getEndTokenPos() - $node->getStartTokenPos() + 2;
127+
$indentation = $indent > 0 ? str_repeat(' ', $indent) : '';
128+
}
129+
117130
// add new line
118-
$oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text;
131+
$oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $indentation . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text;
119132
$this->isDowngraded = true;
120133
}
121134

0 commit comments

Comments
 (0)