[CodingStyle] Handle VarConstantCommentRector + NewlineAfterStatementRector (#872)

* [CodingStyle] Handle VarConstantCommentRector + NewlineAfterStatementRector

* [ci-review] Rector Rectify

* Fixed 🎉

* check with next PhpDocInfo changed

* phpstan

* final touch

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-09-12 21:33:06 +07:00 committed by GitHub
parent e2fb0079bc
commit fccd592223
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 116 additions and 3 deletions

View File

@ -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'));

View File

@ -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_;

View File

@ -0,0 +1,52 @@
<?php
namespace Rector\Core\Tests\Issues\IssueVarConstantNewline\Fixture;
class Fixture
{
public const A = 'a';
public const B = 'b';
/**
* @var string
*/
public const C = 'c';
public const D = 'd';
public const E = 'e';
}
?>
-----
<?php
namespace Rector\Core\Tests\Issues\IssueVarConstantNewline\Fixture;
class Fixture
{
/**
* @var string
*/
public const A = 'a';
/**
* @var string
*/
public const B = 'b';
/**
* @var string
*/
public const C = 'c';
/**
* @var string
*/
public const D = 'd';
/**
* @var string
*/
public const E = 'e';
}
?>

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\IssueVarConstantNewline;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class IssueVarConstantNewlineTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
use Rector\CodingStyle\Rector\ClassConst\VarConstantCommentRector;
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(VarConstantCommentRector::class);
$services->set(NewlineAfterStatementRector::class);
};