Skip to content

Commit

Permalink
Attempt to fix incorrect doctrine table attribute values on php 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
acrobat committed Jul 21, 2022
1 parent ff524c5 commit 89af657
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
13 changes: 5 additions & 8 deletions packages/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php
Expand Up @@ -28,21 +28,18 @@ final class PhpAttributeGroupFactory
/**
* @var array<string, string[]>>
*/
private array $unwrappedAnnotations = [];
private array $unwrappedAnnotations = [
'Doctrine\ORM\Mapping\Table' => ['indexes', 'uniqueConstraints'],
'Doctrine\ORM\Mapping\Entity' => ['uniqueConstraints'],
];

public function __construct(
private readonly AnnotationToAttributeMapper $annotationToAttributeMapper,
private readonly AttributeNameFactory $attributeNameFactory,
private readonly NamedArgsFactory $namedArgsFactory,
private readonly ExprParameterReflectionTypeCorrector $exprParameterReflectionTypeCorrector,
private readonly AttributeArrayNameInliner $attributeArrayNameInliner,
PhpVersionProvider $phpVersionProvider
private readonly AttributeArrayNameInliner $attributeArrayNameInliner
) {
// nested indexes supported only since PHP 8.1
if (! $phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NEW_INITIALIZERS)) {
$this->unwrappedAnnotations['Doctrine\ORM\Mapping\Table'] = ['indexes', 'uniqueConstraints'];
$this->unwrappedAnnotations['Doctrine\ORM\Mapping\Entity'][] = 'uniqueConstraints';
}
}

public function createFromSimpleTag(AnnotationToAttribute $annotationToAttribute): AttributeGroup
Expand Down
10 changes: 0 additions & 10 deletions packages/PhpAttribute/UnwrapableAnnotationAnalyzer.php
Expand Up @@ -29,11 +29,6 @@ final class UnwrapableAnnotationAnalyzer
*/
private array $annotationsToAttributes = [];

public function __construct(
private readonly PhpVersionProvider $phpVersionProvider
) {
}

/**
* @param AnnotationToAttribute[] $annotationsToAttributes
*/
Expand All @@ -47,11 +42,6 @@ public function configure(array $annotationsToAttributes): void
*/
public function areUnwrappable(array $doctrineAnnotationTagValueNodes): bool
{
// the new in initilazers is handled directly
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NEW_INITIALIZERS)) {
return false;
}

foreach ($doctrineAnnotationTagValueNodes as $doctrineAnnotationTagValueNode) {
$annotationClassName = $doctrineAnnotationTagValueNode->identifierTypeNode->getAttribute(
PhpDocAttributeKey::RESOLVED_CLASS
Expand Down
Expand Up @@ -23,7 +23,9 @@ namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\F

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'])])]
#[ORM\Table]
#[ORM\Index(name: 'search_idx', columns: ['name', 'c'])]
#[ORM\UniqueConstraint(name: 'name_idx', columns: ['name'])]
class TableAndNestedIndex
{
}
Expand Down
8 changes: 8 additions & 0 deletions rules/Php80/Rector/Class_/AnnotationToAttributeRector.php
Expand Up @@ -250,6 +250,14 @@ private function processDoctrineAnnotationClasses(PhpDocInfo $phpDocInfo, array
$shouldInlinedNested = true;
}

// Inline nested annotations when they can/need to be unwrapped (doctrine @Table(index: [@Index(...)])
if ($nestedDoctrineAnnotationTagValueNodes !== []
&& $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NEW_INITIALIZERS)
&& $this->unwrapableAnnotationAnalyzer->areUnwrappable($nestedDoctrineAnnotationTagValueNodes)
) {
$shouldInlinedNested = true;
}

if (! $this->removableAnnotationAnalyzer->isRemovable($doctrineTagValueNode)) {
$doctrineTagAndAnnotationToAttributes[] = new DoctrineTagAndAnnotationToAttribute(
$doctrineTagValueNode,
Expand Down

0 comments on commit 89af657

Please sign in to comment.