Skip to content

Commit

Permalink
Fix annotation to attribute rector with doctrine table and nested uni…
Browse files Browse the repository at this point in the history
…queConstraints option (#1850)
  • Loading branch information
acrobat committed Feb 21, 2022
1 parent 886aad9 commit 900b6fe
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 5 deletions.
8 changes: 3 additions & 5 deletions packages/PhpAttribute/Printer/PhpAttributeGroupFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ final class PhpAttributeGroupFactory
/**
* @var array<string, string[]>>
*/
private array $unwrappedAnnotations = [
'Doctrine\ORM\Mapping\Table' => ['uniqueConstraints'],
'Doctrine\ORM\Mapping\Entity' => ['uniqueConstraints'],
];
private array $unwrappedAnnotations = [];

public function __construct(
private readonly AnnotationToAttributeMapper $annotationToAttributeMapper,
Expand All @@ -44,7 +41,8 @@ public function __construct(
) {
// nested indexes supported only since PHP 8.1
if (! $phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NEW_INITIALIZERS)) {
$this->unwrappedAnnotations['Doctrine\ORM\Mapping\Table'][] = 'indexes';
$this->unwrappedAnnotations['Doctrine\ORM\Mapping\Table'] = ['indexes', 'uniqueConstraints'];
$this->unwrappedAnnotations['Doctrine\ORM\Mapping\Entity'][] = 'uniqueConstraints';
}
}

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

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Doctrine;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Table(indexes={@ORM\Index(name="search_idx", columns={"name", "c"})}, uniqueConstraints={@ORM\UniqueConstraint(name="name_idx", columns={"name"})}]
*/
class TableAndNestedIndex
{
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Doctrine;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Table]
#[ORM\Index(name: 'search_idx', columns: ['name', 'c'])]
#[ORM\UniqueConstraint(name: 'name_idx', columns: ['name'])]
class TableAndNestedIndex
{
}

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

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\FixturePhp81;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Table(indexes={@ORM\Index(name="search_idx", columns={"name", "c"})}, uniqueConstraints={@ORM\UniqueConstraint(name="name_idx", columns={"name"})}]
*/
class TableAndNestedIndex
{
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\FixturePhp81;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Table(indexes: [new ORM\Index(name: 'search_idx', columns: ['name', 'c'])], uniqueConstraints: [new ORM\UniqueConstraint(name: 'name_idx', columns: ['name'])])]
class TableAndNestedIndex
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
new AnnotationToAttribute('Doctrine\ORM\Mapping\Entity'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\ManyToMany'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\Table'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\Index'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\UniqueConstraint'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\JoinColumns'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\JoinColumn'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
new AnnotationToAttribute(NotNull::class),
new AnnotationToAttribute(NotNumber::class),
new AnnotationToAttribute(GenericAnnotation::class),
new AnnotationToAttribute('Doctrine\ORM\Mapping\Table'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\Index'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\UniqueConstraint'),
]);
};
16 changes: 16 additions & 0 deletions stubs/Doctrine/ORM/Mapping/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Mapping;

if (class_exists('Doctrine\ORM\Mapping\Index')) {
return;
}

final class Index
{
public function __construct($name, $columns = [])
{
}
}

0 comments on commit 900b6fe

Please sign in to comment.