diff --git a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php index 71aeb20b..88948308 100644 --- a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php +++ b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php @@ -118,16 +118,19 @@ public function refactor(Node $node): ?Node foreach ($node->attrGroups as $attrGroup) { foreach ($attrGroup->attrs as $key => $attribute) { if ($this->shouldSkipAttribute($attribute)) { - if (isset($oldTokens[$attrGroup->getEndTokenPos() + 1]) && ! str_contains( - (string) $oldTokens[$attrGroup->getEndTokenPos() + 1], - "\n" - )) { + $endTokenPos = $attrGroup->getEndTokenPos(); + $nextTokenPos = $endTokenPos + 1; + if ( + $endTokenPos >= 0 + && isset($oldTokens[$nextTokenPos]) + && ! str_contains((string) $oldTokens[$nextTokenPos], "\n") + ) { // add new line - $oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $this->resolveIndentation( + $oldTokens[$nextTokenPos]->text = "\n" . $this->resolveIndentation( $node, $attrGroup, $attribute - ) . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text; + ) . $oldTokens[$nextTokenPos]->text; $this->isDowngraded = true; } diff --git a/tests/Issues/IssueDowngradeMixedType/Fixture/do_not_add_newline_before_open_tag.php.inc b/tests/Issues/IssueDowngradeMixedType/Fixture/do_not_add_newline_before_open_tag.php.inc new file mode 100644 index 00000000..8b9726a3 --- /dev/null +++ b/tests/Issues/IssueDowngradeMixedType/Fixture/do_not_add_newline_before_open_tag.php.inc @@ -0,0 +1,35 @@ + +----- + diff --git a/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php b/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php index 6a4acde8..8cd4b307 100644 --- a/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php +++ b/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php @@ -4,10 +4,17 @@ use Rector\Config\RectorConfig; use Rector\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector; +use Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector; use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector; +use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation; return static function (RectorConfig $rectorConfig): void { $rectorConfig->importNames(); $rectorConfig->rule(DowngradeTypedPropertyRector::class); $rectorConfig->rule(DowngradeMixedTypeDeclarationRector::class); + + $rectorConfig + ->ruleWithConfiguration(DowngradeAttributeToAnnotationRector::class, [ + new DowngradeAttributeToAnnotation('Symfony\Contracts\Service\Attribute\Required', 'required'), + ]); };