Skip to content

Commit 364fbdb

Browse files
authored
[DowngradePhp80] Handle combine DowngradeMixedTypeDeclarationRector+DowngradeAttributeToAnnotationRector on declare(strict_types=1) inline after open php tag (#373)
* [DowngradePhp80] Handle combine DowngradeMixedTypeDeclarationRector+DowngradeAttributeToAnnotationRector on declare(strict_types=1) inline after open php tag * Fix * Fix * Fix * fix phpstan * fix phpstan
1 parent e49e4c8 commit 364fbdb

3 files changed

Lines changed: 51 additions & 6 deletions

File tree

rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,19 @@ public function refactor(Node $node): ?Node
118118
foreach ($node->attrGroups as $attrGroup) {
119119
foreach ($attrGroup->attrs as $key => $attribute) {
120120
if ($this->shouldSkipAttribute($attribute)) {
121-
if (isset($oldTokens[$attrGroup->getEndTokenPos() + 1]) && ! str_contains(
122-
(string) $oldTokens[$attrGroup->getEndTokenPos() + 1],
123-
"\n"
124-
)) {
121+
$endTokenPos = $attrGroup->getEndTokenPos();
122+
$nextTokenPos = $endTokenPos + 1;
123+
if (
124+
$endTokenPos >= 0
125+
&& isset($oldTokens[$nextTokenPos])
126+
&& ! str_contains((string) $oldTokens[$nextTokenPos], "\n")
127+
) {
125128
// add new line
126-
$oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $this->resolveIndentation(
129+
$oldTokens[$nextTokenPos]->text = "\n" . $this->resolveIndentation(
127130
$node,
128131
$attrGroup,
129132
$attribute
130-
) . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text;
133+
) . $oldTokens[$nextTokenPos]->text;
131134
$this->isDowngraded = true;
132135
}
133136

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Rector\Tests\Issues\IssueDowngradeMixedType\Fixture;
4+
5+
class SkipSameLineDeclareStrictType extends \stdClass implements \ArrayAccess, \Countable, \IteratorAggregate
6+
{
7+
public function offsetGet($key): mixed
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php declare(strict_types=1);
15+
16+
namespace Rector\Tests\Issues\IssueDowngradeMixedType\Fixture;
17+
18+
use stdClass;
19+
use ArrayAccess;
20+
use Countable;
21+
use IteratorAggregate;
22+
use ReturnTypeWillChange;
23+
24+
class SkipSameLineDeclareStrictType extends stdClass implements ArrayAccess, Countable, IteratorAggregate
25+
{
26+
/**
27+
* @return mixed
28+
*/
29+
#[ReturnTypeWillChange]
30+
public function offsetGet($key)
31+
{
32+
}
33+
}
34+
35+
?>

tests/Issues/IssueDowngradeMixedType/config/configured_rule.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@
44

55
use Rector\Config\RectorConfig;
66
use Rector\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector;
7+
use Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector;
78
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector;
9+
use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation;
810

911
return static function (RectorConfig $rectorConfig): void {
1012
$rectorConfig->importNames();
1113
$rectorConfig->rule(DowngradeTypedPropertyRector::class);
1214
$rectorConfig->rule(DowngradeMixedTypeDeclarationRector::class);
15+
16+
$rectorConfig
17+
->ruleWithConfiguration(DowngradeAttributeToAnnotationRector::class, [
18+
new DowngradeAttributeToAnnotation('Symfony\Contracts\Service\Attribute\Required', 'required'),
19+
]);
1320
};

0 commit comments

Comments
 (0)