Skip to content

Commit

Permalink
Add unique PHP 8.1 constraints (#5612)
Browse files Browse the repository at this point in the history
* add unique PHP 8.1 constraints

* [ci-review] Rector Rectify

* Fix

* Fix

* ensure trim space

* Fix

---------

Co-authored-by: GitHub Action <actions@github.com>
Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
3 people committed Feb 13, 2024
1 parent 7de77e5 commit 7afec9a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
@@ -0,0 +1,26 @@
<?php

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

/**
* @\Doctrine\ORM\Mapping\Table(uniqueConstraints={
* @\Doctrine\ORM\Mapping\UniqueConstraint(name="some_key")
* })
*/
final class UniqueConstraints
{
}

?>
-----
<?php

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

#[\Doctrine\ORM\Mapping\Table]
#[\Doctrine\ORM\Mapping\UniqueConstraint(name: 'some_key')]
final class UniqueConstraints
{
}

?>
@@ -1,6 +1,8 @@
<?php

declare(strict_types=1);
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\UniqueConstraint;

use Rector\Config\RectorConfig;
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
Expand All @@ -20,5 +22,9 @@
new AnnotationToAttribute(Length::class),
new AnnotationToAttribute(NotNumber::class),
new AnnotationToAttribute(GenericAnnotation::class),

// PHP 8.1 doctrine annotations
new AnnotationToAttribute(Table::class),
new AnnotationToAttribute(UniqueConstraint::class),
]);
};
4 changes: 3 additions & 1 deletion rules/Php80/Rector/Class_/AnnotationToAttributeRector.php
Expand Up @@ -134,7 +134,7 @@ public function refactor(Node $node): ?Node

$uses = $this->useImportsResolver->resolveBareUses();

// 1. bare tags without annotation class, e.g. "@inject"
// 1. bare tags without annotation class, e.g. "@require"
$genericAttributeGroups = $this->processGenericTags($phpDocInfo);

// 2. Doctrine annotation classes
Expand Down Expand Up @@ -233,6 +233,7 @@ private function processDoctrineAnnotationClasses(PhpDocInfo $phpDocInfo, array
}

$doctrineTagValueNode = $phpDocChildNode->value;

$annotationToAttribute = $this->matchAnnotationToAttribute($doctrineTagValueNode);
if (! $annotationToAttribute instanceof AnnotationToAttribute) {
continue;
Expand All @@ -242,6 +243,7 @@ private function processDoctrineAnnotationClasses(PhpDocInfo $phpDocInfo, array
$doctrineTagValueNode,
$annotationToAttribute,
);

$doctrineTagValueNodes[] = $doctrineTagValueNode;
}

Expand Down
Expand Up @@ -172,10 +172,12 @@ private function processTextSpacelessInTextNode(
if (! str_starts_with($otherText, '@\\') && trim($otherText) !== '') {
$phpDocNode->children[$key] = new PhpDocTextNode($otherText);
array_splice($phpDocNode->children, $key + 1, 0, $spacelessPhpDocTagNodes);
} else {
unset($phpDocNode->children[$key]);
array_splice($phpDocNode->children, $key, 0, $spacelessPhpDocTagNodes);

return;
}

unset($phpDocNode->children[$key]);
array_splice($phpDocNode->children, $key, 0, $spacelessPhpDocTagNodes);
}

private function transformGenericTagValueNodesToDoctrineAnnotationTagValueNodes(
Expand Down Expand Up @@ -448,15 +450,14 @@ private function resolveFqnAnnotationSpacelessPhpDocTagNode(
$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], '"\'') . '")';
}
$annotationContent = $this->resolveAnnotationContent(
$match['annotation_content'] ?? '',
$nestedAnnotationOpen
);

$spacelessPhpDocTagNodes[] = $this->createDoctrineSpacelessPhpDocTagNode(
$annotationContent,
Expand All @@ -469,4 +470,21 @@ private function resolveFqnAnnotationSpacelessPhpDocTagNode(

return $spacelessPhpDocTagNodes;
}

/**
* @param string[]|null[] $nestedAnnotationOpen
*/
private function resolveAnnotationContent(string $annotationContent, array $nestedAnnotationOpen): string
{
if (! isset($nestedAnnotationOpen[1])) {
return $annotationContent;
}

$trimmedNestedAnnotationOpen = trim($nestedAnnotationOpen[1]);
if (str_ends_with($trimmedNestedAnnotationOpen, '{')) {
return $annotationContent;
}

return '("' . trim($trimmedNestedAnnotationOpen, '"\'') . '")';
}
}

0 comments on commit 7afec9a

Please sign in to comment.