Skip to content

Commit

Permalink
[Php80] Handle FQCN @\ usage on AnnotationToAttributeRector (#5240)
Browse files Browse the repository at this point in the history
* [Php80] Handle FQCN @\ usage on AnnotationToAttributeRector

* [ci-review] Rector Rectify

* update fixture

* update config

* fix fixture

* fixed

* fixture

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Nov 23, 2023
1 parent 520829e commit 51695ae
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ final class DoctrineAnnotationDecorator implements PhpDocNodeDecoratorInterface
private const ALLOWED_SHORT_ANNOTATIONS = ['Target'];

/**
* @see https://regex101.com/r/95kIw4/1
* @see https://regex101.com/r/95kIw4/2
* @var string
*/
private const LONG_ANNOTATION_REGEX = '#@\\\\(?<class_name>.*?)(?<annotation_content>\(.*?\))#';
private const LONG_ANNOTATION_REGEX = '#@\\\\(?<class_name>.*?)(?<annotation_content>\(.*?\)|,)#';

/**
* @see https://regex101.com/r/xWaLOz/1
Expand Down Expand Up @@ -387,16 +387,23 @@ private function resolveFqnAnnotationSpacelessPhpDocTagNode(
$spacelessPhpDocTagNodes = [];
foreach ($matches as $match) {
$fullyQualifiedAnnotationClass = $match['class_name'] ?? null;

if ($fullyQualifiedAnnotationClass === null) {
continue;
}

$nestedAnnotationOpen = explode('(', (string) $fullyQualifiedAnnotationClass);
$fullyQualifiedAnnotationClass = $nestedAnnotationOpen[0];

$annotationContent = $match['annotation_content'] ?? null;

$tagName = '@\\' . $fullyQualifiedAnnotationClass;

$formerStartEnd = $phpDocTextNode->getAttribute(PhpDocAttributeKey::START_AND_END);

if (isset($nestedAnnotationOpen[1])) {
$annotationContent = '("' . trim($nestedAnnotationOpen[1], '"') . '")';
}

$spacelessPhpDocTagNodes[] = $this->createDoctrineSpacelessPhpDocTagNode(
$annotationContent,
$tagName,
Expand Down
48 changes: 48 additions & 0 deletions tests/Issues/FqcnAnnotationToAttribute/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Rector\Core\Tests\Issues\FqcnAnnotationToAttribute\Fixture;

/**
* @\Doctrine\ORM\Mapping\Entity()
* @\Doctrine\ORM\Mapping\Table("user", indexes={
* @\Doctrine\ORM\Mapping\Index(name="name_index", columns={"name"}),
* @\Doctrine\ORM\Mapping\Index(name="surname_index", columns={"surname"}),
* })
* @\Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity("azureB2cUuid")
* @\Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity("uuid")
* @\Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity("email")
*/
class Entity
{
/**
* @\Symfony\Component\Validator\Constraints\NotBlank()
* @\Symfony\Component\Validator\Constraints\Email(mode="strict")
* @\Doctrine\ORM\Mapping\Column(type="string", unique=true)
* @\Symfony\Component\Serializer\Annotation\Groups({"import", "export-user", "export-claim"})
*/
protected string $email = "";
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\FqcnAnnotationToAttribute\Fixture;

#[\Doctrine\ORM\Mapping\Entity]
#[\Doctrine\ORM\Mapping\Index(name: 'name_index', columns: ['name'])]
#[\Doctrine\ORM\Mapping\Index(name: 'surname_index', columns: ['surname'])]
#[\Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity('azureB2cUuid')]
#[\Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity('uuid')]
#[\Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity('email')]
#[\Doctrine\ORM\Mapping\Table('user')]
class Entity
{
#[\Symfony\Component\Validator\Constraints\NotBlank]
#[\Symfony\Component\Validator\Constraints\Email(mode: 'strict')]
#[\Doctrine\ORM\Mapping\Column(type: 'string', unique: true)]
#[\Symfony\Component\Serializer\Annotation\Groups(['import', 'export-user', 'export-claim'])]
protected string $email = "";
}

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

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\FqcnAnnotationToAttribute;

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

final class FqcnAnnotationToAttributeTest 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';
}
}
14 changes: 14 additions & 0 deletions tests/Issues/FqcnAnnotationToAttribute/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

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

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

0 comments on commit 51695ae

Please sign in to comment.