Skip to content

Commit 1cf15fe

Browse files
authored
Revert "Revert " Re-skip Override and SensitiveParameter (#320)" (#321)" (#322)
This reverts commit 4bebf17.
1 parent 4bebf17 commit 1cf15fe

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;
4+
5+
final class OverrideAndSensitiveParameter extends Foo
6+
{
7+
#[\Override]
8+
public function run(#[\SensitiveParameter] bool $param) {
9+
if ($this->isTrue($param)) {
10+
return 5;
11+
}
12+
13+
return '10';
14+
}
15+
16+
private function isTrue($value) {
17+
return $value === true;
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;
26+
27+
final class OverrideAndSensitiveParameter extends Foo
28+
{
29+
#[\Override]
30+
public function run(
31+
#[\SensitiveParameter]
32+
bool $param) {
33+
if ($this->isTrue($param)) {
34+
return 5;
35+
}
36+
37+
return '10';
38+
}
39+
40+
private function isTrue($value) {
41+
return $value === true;
42+
}
43+
}
44+
45+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;
4+
5+
final class SkipAlreadyNewlinedOverrideAndSensitiveParameters extends Foo
6+
{
7+
#[\Override]
8+
public function run(
9+
#[\SensitiveParameter]
10+
bool $param
11+
) {
12+
if ($this->isTrue($param)) {
13+
return 5;
14+
}
15+
16+
return '10';
17+
}
18+
19+
private function isTrue($value) {
20+
return $value === true;
21+
}
22+
}

rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php

Lines changed: 16 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,7 +120,15 @@ public function refactor(Node $node): ?Node
114120
(string) $oldTokens[$attrGroup->getEndTokenPos() + 1],
115121
"\n"
116122
)) {
117-
// add new line
123+
if ($node instanceof Param && (isset($oldTokens[$attrGroup->getStartTokenPos() - 1]) && ! str_contains(
124+
(string) $oldTokens[$attrGroup->getStartTokenPos() - 1],
125+
"\n"
126+
))) {
127+
// add new line before
128+
$oldTokens[$attrGroup->getStartTokenPos() - 1]->text .= "\n";
129+
}
130+
131+
// add new line after
118132
$oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text;
119133
$this->isDowngraded = true;
120134
}

0 commit comments

Comments
 (0)