Skip to content

Commit

Permalink
[Php80] Handle Call to a member function getOriginalValues() on strin…
Browse files Browse the repository at this point in the history
…g on AnnotationToAttributeRector (#1099)

* [Php80] Handle sCall to a member function getOriginalValues() on string on AnnotationToAttributeRector

* debug

* [ci-review] Rector Rectify

* update

* Fixed 🎉

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* phpstan

* phpstan

* cs fix

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Oct 29, 2021
1 parent 0db02e2 commit 87f6d1b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Table(name=App\Entity\Aktenzeichen::TABLE_NAME, uniqueConstraints={@ORM\UniqueConstraint(name="aktenzeichen_idx", columns={"aktenzeichen"})})
* @ORM\Entity
*/
class StringAnnotationValue
{
}

?>
-----
<?php

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

use Doctrine\ORM\Mapping as ORM;

#[\Doctrine\ORM\Mapping\Table(name: App\Entity\Aktenzeichen::TABLE_NAME)]
#[\Doctrine\ORM\Mapping\UniqueConstraint(name: 'aktenzeichen_idx', columns: ['aktenzeichen'])]
#[\Doctrine\ORM\Mapping\Entity]
class StringAnnotationValue
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
new AnnotationToAttribute('Doctrine\ORM\Mapping\Index'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\JoinColumn'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\UniqueConstraint'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\Table'),
]),
]]);
};
51 changes: 50 additions & 1 deletion rules/Php80/Rector/Class_/AnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\PhpDocParser\Ast\Node as DocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\BetterPhpDocParser\AnnotationAnalyzer\DoctrineAnnotationTagValueNodeAnalyzer;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
Expand Down Expand Up @@ -246,7 +247,9 @@ private function processDoctrineAnnotationClasses(PhpDocInfo $phpDocInfo): array
$docNode,
$this->annotationsToAttributes
)) {
$newDoctrineTagValueNode = new DoctrineAnnotationTagValueNode($docNode->identifierTypeNode);
$stringValues = $this->getStringFromNestedDoctrineTagAnnotationToAttribute($docNode);
$newDoctrineTagValueNode = $this->resolveNewDoctrineTagValueNode($docNode, $stringValues);

$doctrineTagAndAnnotationToAttributes[] = new DoctrineTagAndAnnotationToAttribute(
$newDoctrineTagValueNode,
$annotationToAttribute
Expand Down Expand Up @@ -276,6 +279,42 @@ private function processDoctrineAnnotationClasses(PhpDocInfo $phpDocInfo): array
return $this->attrGroupsFactory->create($doctrineTagAndAnnotationToAttributes);
}

/**
* @param string[] $stringValues
*/
private function resolveNewDoctrineTagValueNode(
DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode,
array $stringValues
): DoctrineAnnotationTagValueNode {
if ($stringValues === []) {
return new DoctrineAnnotationTagValueNode($doctrineAnnotationTagValueNode->identifierTypeNode);
}

return new DoctrineAnnotationTagValueNode(
$doctrineAnnotationTagValueNode->identifierTypeNode,
current($stringValues),
$stringValues
);
}

/**
* @return string[]
*/
private function getStringFromNestedDoctrineTagAnnotationToAttribute(
DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode
): array {
$values = $doctrineAnnotationTagValueNode->getValues();
foreach ($values as $key => $value) {
if (is_string($value)) {
return [
$key => $value,
];
}
}

return [];
}

/**
* @param DoctrineTagAndAnnotationToAttribute[] $doctrineTagAndAnnotationToAttributes
* @return DoctrineTagAndAnnotationToAttribute[] $doctrineTagAndAnnotationToAttributes
Expand All @@ -286,6 +325,16 @@ private function addNestedDoctrineTagAndAnnotationToAttribute(
): array {
$values = $doctrineAnnotationTagValueNode->getValues();
foreach ($values as $value) {
if (is_string($value)) {
$originalValue = new DoctrineAnnotationTagValueNode(new IdentifierTypeNode($value));
$doctrineTagAndAnnotationToAttributes = $this->collectDoctrineTagAndAnnotationToAttributes(
$originalValue,
$doctrineTagAndAnnotationToAttributes
);

continue;
}

$originalValues = $value->getOriginalValues();
foreach ($originalValues as $originalValue) {
$doctrineTagAndAnnotationToAttributes = $this->collectDoctrineTagAndAnnotationToAttributes(
Expand Down

0 comments on commit 87f6d1b

Please sign in to comment.