From 916fc3c26604bba4ebfbdc604da05f9304c4c0c9 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 12 May 2026 17:40:54 +0700 Subject: [PATCH 1/6] [DowngradePhp80] Handle combine DowngradeMixedTypeDeclarationRector+DowngradeAttributeToAnnotationRector on declare(strict_types=1) inline after open php tag --- ...do_not_add_newline_before_open_tag.php.inc | 35 +++++++++++++++++++ .../config/configured_rule.php | 12 +++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/Issues/IssueDowngradeMixedType/Fixture/do_not_add_newline_before_open_tag.php.inc 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..4a495ec2 100644 --- a/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php +++ b/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php @@ -5,9 +5,21 @@ use Rector\Config\RectorConfig; use Rector\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector; use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector; +use Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector; +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, [ + // Symfony + new DowngradeAttributeToAnnotation('Symfony\Contracts\Service\Attribute\Required', 'required'), + // Nette + new DowngradeAttributeToAnnotation('Nette\DI\Attributes\Inject', 'inject'), + // Jetbrains\PhpStorm\Language under nette/utils + new DowngradeAttributeToAnnotation('Jetbrains\PhpStorm\Language', 'language'), + ]); }; From 543f94d9d805a25a885494cf3c9c5b0653dfe9e1 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 12 May 2026 17:44:21 +0700 Subject: [PATCH 2/6] Fix --- .../Class_/DowngradeAttributeToAnnotationRector.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php index 71aeb20b..18ab9677 100644 --- a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php +++ b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php @@ -118,16 +118,17 @@ 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], + $nextTokenPos = $attrGroup->getEndTokenPos() + 1; + if ($attrGroup->getEndTokenPos() >= 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; } From 43a2441762b7343e8f95cf1707eef703d9e8ad73 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 12 May 2026 17:44:29 +0700 Subject: [PATCH 3/6] Fix --- .../Class_/DowngradeAttributeToAnnotationRector.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php index 18ab9677..88948308 100644 --- a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php +++ b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php @@ -118,11 +118,13 @@ public function refactor(Node $node): ?Node foreach ($node->attrGroups as $attrGroup) { foreach ($attrGroup->attrs as $key => $attribute) { if ($this->shouldSkipAttribute($attribute)) { - $nextTokenPos = $attrGroup->getEndTokenPos() + 1; - if ($attrGroup->getEndTokenPos() >= 0 && isset($oldTokens[$nextTokenPos]) && ! str_contains( - (string) $oldTokens[$nextTokenPos], - "\n" - )) { + $endTokenPos = $attrGroup->getEndTokenPos(); + $nextTokenPos = $endTokenPos + 1; + if ( + $endTokenPos >= 0 + && isset($oldTokens[$nextTokenPos]) + && ! str_contains((string) $oldTokens[$nextTokenPos], "\n") + ) { // add new line $oldTokens[$nextTokenPos]->text = "\n" . $this->resolveIndentation( $node, From f50ce8f38d23e78de5ffa4662484000f2a31e56d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 12 May 2026 17:47:05 +0700 Subject: [PATCH 4/6] Fix --- .../IssueDowngradeMixedType/config/configured_rule.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php b/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php index 4a495ec2..523a7962 100644 --- a/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php +++ b/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php @@ -9,6 +9,11 @@ use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation; return static function (RectorConfig $rectorConfig): void { + /** @var class-string $netteInjectAttribute */ + $netteInjectAttribute = 'Nette\DI\Attributes\Inject'; + /** @var class-string $jetbrainsLanguageAttribute */ + $jetbrainsLanguageAttribute = 'Jetbrains\PhpStorm\Language'; + $rectorConfig->importNames(); $rectorConfig->rule(DowngradeTypedPropertyRector::class); $rectorConfig->rule(DowngradeMixedTypeDeclarationRector::class); @@ -18,8 +23,8 @@ // Symfony new DowngradeAttributeToAnnotation('Symfony\Contracts\Service\Attribute\Required', 'required'), // Nette - new DowngradeAttributeToAnnotation('Nette\DI\Attributes\Inject', 'inject'), + new DowngradeAttributeToAnnotation($netteInjectAttribute, 'inject'), // Jetbrains\PhpStorm\Language under nette/utils - new DowngradeAttributeToAnnotation('Jetbrains\PhpStorm\Language', 'language'), + new DowngradeAttributeToAnnotation($jetbrainsLanguageAttribute, 'language'), ]); }; From 2e9b37bcf7f487b8c449aa5b6cab3633ba3b0e56 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 12 May 2026 17:49:41 +0700 Subject: [PATCH 5/6] fix phpstan --- .../config/configured_rule.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php b/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php index 523a7962..6eb34599 100644 --- a/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php +++ b/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php @@ -2,18 +2,15 @@ declare(strict_types=1); +use Jetbrains\PhpStorm\Language; +use Nette\DI\Attributes\Inject; use Rector\Config\RectorConfig; use Rector\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector; -use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector; use Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector; +use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector; use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation; return static function (RectorConfig $rectorConfig): void { - /** @var class-string $netteInjectAttribute */ - $netteInjectAttribute = 'Nette\DI\Attributes\Inject'; - /** @var class-string $jetbrainsLanguageAttribute */ - $jetbrainsLanguageAttribute = 'Jetbrains\PhpStorm\Language'; - $rectorConfig->importNames(); $rectorConfig->rule(DowngradeTypedPropertyRector::class); $rectorConfig->rule(DowngradeMixedTypeDeclarationRector::class); @@ -23,8 +20,8 @@ // Symfony new DowngradeAttributeToAnnotation('Symfony\Contracts\Service\Attribute\Required', 'required'), // Nette - new DowngradeAttributeToAnnotation($netteInjectAttribute, 'inject'), + new DowngradeAttributeToAnnotation(Inject::class, 'inject'), // Jetbrains\PhpStorm\Language under nette/utils - new DowngradeAttributeToAnnotation($jetbrainsLanguageAttribute, 'language'), + new DowngradeAttributeToAnnotation(Language::class, 'language'), ]); }; From 475bf7a8b1c545a47c96008011a9829e385f4a0f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 12 May 2026 17:51:36 +0700 Subject: [PATCH 6/6] fix phpstan --- .../IssueDowngradeMixedType/config/configured_rule.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php b/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php index 6eb34599..8cd4b307 100644 --- a/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php +++ b/tests/Issues/IssueDowngradeMixedType/config/configured_rule.php @@ -2,8 +2,6 @@ declare(strict_types=1); -use Jetbrains\PhpStorm\Language; -use Nette\DI\Attributes\Inject; use Rector\Config\RectorConfig; use Rector\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector; use Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector; @@ -17,11 +15,6 @@ $rectorConfig ->ruleWithConfiguration(DowngradeAttributeToAnnotationRector::class, [ - // Symfony new DowngradeAttributeToAnnotation('Symfony\Contracts\Service\Attribute\Required', 'required'), - // Nette - new DowngradeAttributeToAnnotation(Inject::class, 'inject'), - // Jetbrains\PhpStorm\Language under nette/utils - new DowngradeAttributeToAnnotation(Language::class, 'language'), ]); };