Skip to content

Commit

Permalink
[Php80] Skip change UniqueEntity constrainst annotation if Entity att…
Browse files Browse the repository at this point in the history
…ribute exists on AnnotationToAttributeRector (#747)

* [Php80] Do not change UniqueEntity constrainst annotation if Entity attribute exists

* skip

* update fixture

* update fixture different definition for uniqueentity

* Fixed 🎉

* doc regex

* rename fixture

* add more fixture for do not change unique entity when enitty attribute not exists
  • Loading branch information
samsonasik committed Aug 23, 2021
1 parent 006ed85 commit 9443e84
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\FixtureAutoImported;

use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
* @UniqueEntity(fields={"username"})
* @UniqueEntity("email")
* @Entity(repositoryClass=UserRepository::class)
*/
#[Table(name: 'users')]
class User
{
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\FixtureAutoImported;

use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
* @UniqueEntity(fields={"username"})
* @UniqueEntity("email")
*/
#[Table(name: 'users')]
#[Entity(repositoryClass: UserRepository::class)]
class User
{
}

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

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\FixtureAutoImported;

use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
* @UniqueEntity(fields={"username"})
* @UniqueEntity("email")
*/
#[Entity(repositoryClass: 'UserRepository')]
#[Table(name: 'users')]
class User
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
$services->set(AnnotationToAttributeRector::class)
->call('configure', [[
AnnotationToAttributeRector::ANNOTATION_TO_ATTRIBUTE => ValueObjectInliner::inline([
new AnnotationToAttribute('Doctrine\ORM\Mapping\Entity'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\Id'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\Column'),

Expand Down
11 changes: 10 additions & 1 deletion rules/CodingStyle/NodeAnalyzer/UseImportNameMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

final class UseImportNameMatcher
{
/**
* @var string
*
* @see https://regex101.com/r/ZxFSlc/1 for last name, eg: Entity and UniqueEntity
* @see https://regex101.com/r/OLO0Un/1 for inside namespace, eg: ORM for ORM\Id or ORM\Column
*/
private const SHORT_NAME_REGEX = '#^%s(\\\\[\w]+)?$#i';

public function __construct(
private BetterNodeFinder $betterNodeFinder
) {
Expand Down Expand Up @@ -49,8 +57,9 @@ private function isUseMatchingName(string $tag, UseUse $useUse): bool
{
$shortName = $useUse->alias !== null ? $useUse->alias->name : $useUse->name->getLast();
$shortNamePattern = preg_quote($shortName, '#');
$pattern = sprintf(self::SHORT_NAME_REGEX, $shortNamePattern);

return (bool) Strings::match($tag, '#' . $shortNamePattern . '(\\\\[\w]+)?$#i');
return (bool) Strings::match($tag, $pattern);
}

private function resolveName(string $tag, UseUse $useUse): string
Expand Down

0 comments on commit 9443e84

Please sign in to comment.