diff --git a/packages-tests/BetterPhpDocParser/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php b/packages-tests/BetterPhpDocParser/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php index bc9ae250d1b..437ae978c30 100644 --- a/packages-tests/BetterPhpDocParser/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php +++ b/packages-tests/BetterPhpDocParser/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php @@ -63,7 +63,7 @@ final class PhpDocInfoTest extends AbstractTestCase $this->assertStringEqualsFile(__DIR__ . '/Source/expected-replaced-tag.txt', $printedPhpDocInfo); } - public function testDoNotAddSpaseWhenAddEmptyString() + public function testDoNotAddSpaseWhenAddEmptyString(): void { $this->phpDocInfo->addPhpDocTagNode(new PhpDocTextNode('')); $this->phpDocInfo->addPhpDocTagNode(new PhpDocTextNode('Some text')); diff --git a/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php b/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php index 4d3b6438ef4..bac0859503d 100644 --- a/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php +++ b/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Rector\CodingStyle\Rector\Stmt; +use PhpParser\Comment\Doc; use PhpParser\Node; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Catch_; @@ -134,11 +135,13 @@ CODE_SAMPLE if ($rangeLine > 1) { $comments = $nextNode->getAttribute(AttributeKey::COMMENTS); - if ($comments === null) { + + if ($this->hasNoComment($comments)) { return null; } - if (! isset($comments[0])) { + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($nextNode); + if ($phpDocInfo->hasChanged()) { return null; } @@ -156,6 +159,18 @@ CODE_SAMPLE return $node; } + /** + * @param null|Doc[] $comments + */ + private function hasNoComment(?array $comments): bool + { + if ($comments === null) { + return true; + } + + return ! isset($comments[0]); + } + private function shouldSkip(Node $nextNode): bool { return $nextNode instanceof Else_ || $nextNode instanceof ElseIf_ || $nextNode instanceof Catch_ || $nextNode instanceof Finally_; diff --git a/tests/Issues/IssueVarConstantNewline/Fixture/fixture.php.inc b/tests/Issues/IssueVarConstantNewline/Fixture/fixture.php.inc new file mode 100644 index 00000000000..cc077a05c44 --- /dev/null +++ b/tests/Issues/IssueVarConstantNewline/Fixture/fixture.php.inc @@ -0,0 +1,52 @@ + +----- + diff --git a/tests/Issues/IssueVarConstantNewline/IssueVarConstantNewlineTest.php b/tests/Issues/IssueVarConstantNewline/IssueVarConstantNewlineTest.php new file mode 100644 index 00000000000..2d049eaf876 --- /dev/null +++ b/tests/Issues/IssueVarConstantNewline/IssueVarConstantNewlineTest.php @@ -0,0 +1,33 @@ +doTestFileInfo($fileInfo); + } + + /** + * @return Iterator + */ + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Issues/IssueVarConstantNewline/config/configured_rule.php b/tests/Issues/IssueVarConstantNewline/config/configured_rule.php new file mode 100644 index 00000000000..8e4e521abf2 --- /dev/null +++ b/tests/Issues/IssueVarConstantNewline/config/configured_rule.php @@ -0,0 +1,13 @@ +services(); + $services->set(VarConstantCommentRector::class); + $services->set(NewlineAfterStatementRector::class); +};