Skip to content

Commit

Permalink
[Php80] Handle NestedAnnotationToAttributeRector with empty new line (#…
Browse files Browse the repository at this point in the history
…5448)

* [Php80] Handle NestedAnnotationToAttributeRector with empty new line

* [ci-review] Rector Rectify

* fix

* fix

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jan 9, 2024
1 parent 32d5fce commit 5a156be
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Rector\BetterPhpDocParser\PhpDocParser;

use Nette\Utils\Strings;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
Expand All @@ -28,6 +30,12 @@
*/
final class BetterPhpDocParser extends PhpDocParser
{
/**
* @var string
* @see https://regex101.com/r/JOKSmr/1
*/
private const MULTI_NEW_LINES_REGEX = '#(\n\r|\n){2,}#';

/**
* @param PhpDocNodeDecoratorInterface[] $phpDocNodeDecorators
*/
Expand Down Expand Up @@ -118,6 +126,10 @@ public function parseTagValue(TokenIterator $tokenIterator, string $tag): PhpDoc
$startAndEnd = new StartAndEnd($startPosition, $endPosition);
$phpDocTagValueNode->setAttribute(PhpDocAttributeKey::START_AND_END, $startAndEnd);

if ($phpDocTagValueNode instanceof GenericTagValueNode) {
$phpDocTagValueNode->value = Strings::replace($phpDocTagValueNode->value, self::MULTI_NEW_LINES_REGEX, "\n");
}

return $phpDocTagValueNode;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\EmptyLineNestedAnnotationToAttribute;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class EmptyLineNestedAnnotationToAttributeTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Rector\Tests\Issues\EmptyLineNestedAnnotationToAttribute\Fixture;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="App\Repository\SegmentRepository")
* @ORM\Table(
* name="segments",
*
* indexes={
* @ORM\Index(name="sequence", columns={"sequence"}),
* @ORM\Index(name="status_channel_started_at", columns={"status", "channel", "started_at"}),
* @ORM\Index(name="representation_started_at", columns={"representation", "started_at"}),
* @ORM\Index(name="channel_started_at_status_representation_sequence", columns={"channel", "started_at", "status", "representation", "sequence"})
* },
* uniqueConstraints={
* @ORM\UniqueConstraint(name="unique_channel_representation_started_at", columns={"channel", "representation", "started_at"})
* }
* )
*/
class Segment
{
}

?>
-----
<?php

namespace Rector\Tests\Issues\EmptyLineNestedAnnotationToAttribute\Fixture;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Table(name: 'segments')]
#[ORM\Index(name: 'sequence', columns: ['sequence'])]
#[ORM\Index(name: 'status_channel_started_at', columns: ['status', 'channel', 'started_at'])]
#[ORM\Index(name: 'representation_started_at', columns: ['representation', 'started_at'])]
#[ORM\Index(name: 'channel_started_at_status_representation_sequence', columns: ['channel', 'started_at', 'status', 'representation', 'sequence'])]
#[ORM\UniqueConstraint(name: 'unique_channel_representation_started_at', columns: ['channel', 'representation', 'started_at'])]
#[ORM\Entity(repositoryClass: 'App\Repository\SegmentRepository')]
class Segment
{
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Set\DoctrineSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
]);
};

0 comments on commit 5a156be

Please sign in to comment.