Skip to content

Commit

Permalink
[Php80] Annotation attribute should keep order original annotation co…
Browse files Browse the repository at this point in the history
…mment (#104)

Co-authored-by: kaizen-ci <info@kaizen-ci.org>
  • Loading branch information
samsonasik and kaizen-ci committed May 28, 2021
1 parent 80e6c3b commit e0b4e85
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 33 deletions.
11 changes: 10 additions & 1 deletion packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ public function getVarTagValueNode(): ?VarTagValueNode
return $this->phpDocNode->getVarTagValues()[0] ?? null;
}

/**
* @return array<PhpDocTagNode>
*/
public function getAllTags(): array
{
return $this->phpDocNode->getTags();
}

/**
* @return array<PhpDocTagNode>
*/
Expand Down Expand Up @@ -475,7 +483,8 @@ public function makeMultiLined(): void
$this->isSingleLine = false;
}

public function getNode(): \PhpParser\Node {
public function getNode(): \PhpParser\Node
{
return $this->node;
}

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

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

use Doctrine\ORM\Mapping as ORM;

/**
* News
*
* @ORM\Table(name="news")
* @ORM\Entity(repositoryClass="DoctrineFixtureDemo\Repository\NewsRepository")
*/
class News
{
/**
* @var integer
*
* @ORM\Column(name="id", type="bigint", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=50, nullable=false)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="content", type="string", length=255, nullable=false)
*/
private $content;
}

?>
-----
<?php

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

use Doctrine\ORM\Mapping as ORM;

/**
* News
*/
#[\Doctrine\ORM\Mapping\Table(name: 'news')]
#[\Doctrine\ORM\Mapping\Entity(repositoryClass: 'DoctrineFixtureDemo\Repository\NewsRepository')]
class News
{
/**
* @var integer
*/
#[\Doctrine\ORM\Mapping\Column(name: 'id', type: 'bigint', nullable: false)]
#[\Doctrine\ORM\Mapping\Id]
#[\Doctrine\ORM\Mapping\GeneratedValue(strategy: 'IDENTITY')]
private $id;
/**
* @var string
*/
#[\Doctrine\ORM\Mapping\Column(name: 'title', type: 'string', length: 50, nullable: false)]
private $title;
/**
* @var string
*/
#[\Doctrine\ORM\Mapping\Column(name: 'content', type: 'string', length: 255, nullable: false)]
private $content;
}

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

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

use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\ChangeTrackingPolicy;

/**
* @Entity
* @ChangeTrackingPolicy("DEFERRED_IMPLICIT")
* @ChangeTrackingPolicy("NOTIFY")
*/
class User
{
}
?>
-----
<?php

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

use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\ChangeTrackingPolicy;

#[\Doctrine\ORM\Mapping\Entity]
#[\Doctrine\ORM\Mapping\ChangeTrackingPolicy('DEFERRED_IMPLICIT')]
#[\Doctrine\ORM\Mapping\ChangeTrackingPolicy('NOTIFY')]
class User
{
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

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

use Doctrine\ORM\Mapping as ORM;

/**
* News
*
* @ORM\Table(name="news")
* @ORM\Entity(repositoryClass="DoctrineFixtureDemo\Repository\NewsRepository")
*/
class News
{
/**
* @var integer
*
* @ORM\Column(name="id", type="bigint", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=50, nullable=false)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="content", type="string", length=255, nullable=false)
*/
private $content;
}

?>
-----
<?php

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

use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping as ORM;

/**
* News
*/
#[Table(name: 'news')]
#[Entity(repositoryClass: 'DoctrineFixtureDemo\Repository\NewsRepository')]
class News
{
/**
* @var integer
*/
#[Column(name: 'id', type: 'bigint', nullable: false)]
#[Id]
#[GeneratedValue(strategy: 'IDENTITY')]
private $id;
/**
* @var string
*/
#[Column(name: 'title', type: 'string', length: 50, nullable: false)]
private $title;
/**
* @var string
*/
#[Column(name: 'content', type: 'string', length: 255, nullable: false)]
private $content;
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@
'Symfony\Component\Validator\Constraints\Choice',
'Symfony\Component\Validator\Constraints\Choice'
),

new AnnotationToAttribute('Doctrine\ORM\Mapping\Table', 'Doctrine\ORM\Mapping\Table'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\Entity', 'Doctrine\ORM\Mapping\Entity'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\Id', 'Doctrine\ORM\Mapping\Id'),
new AnnotationToAttribute(
'Doctrine\ORM\Mapping\GeneratedValue',
'Doctrine\ORM\Mapping\GeneratedValue'
),
new AnnotationToAttribute('Doctrine\ORM\Mapping\Column', 'Doctrine\ORM\Mapping\Column'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\ChangeTrackingPolicy', 'Doctrine\ORM\Mapping\ChangeTrackingPolicy'),
]),
]]);
};
96 changes: 64 additions & 32 deletions rules/Php80/Rector/Class_/AnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
Expand Down Expand Up @@ -117,38 +119,8 @@ public function refactor(Node $node): ?Node
return null;
}

$hasNewAttrGroups = false;

foreach ($this->annotationsToAttributes as $annotationToAttribute) {
$tag = $annotationToAttribute->getTag();

// 1. simple doc tags
if ($phpDocInfo->hasByName($tag)) {
// 1. remove php-doc tag
$this->phpDocTagRemover->removeByName($phpDocInfo, $tag);

// 2. add attributes
$node->attrGroups[] = $this->phpAttributeGroupFactory->createFromSimpleTag($annotationToAttribute);

$hasNewAttrGroups = true;
}

$doctrineAnnotationTagValueNode = $phpDocInfo->getByAnnotationClass($tag);
if (! $doctrineAnnotationTagValueNode instanceof DoctrineAnnotationTagValueNode) {
continue;
}

// 1. remove php-doc tag
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $doctrineAnnotationTagValueNode);

// 2. add attributes
$node->attrGroups[] = $this->phpAttributeGroupFactory->create(
$doctrineAnnotationTagValueNode,
$annotationToAttribute
);

$hasNewAttrGroups = true;
}
$tags = $phpDocInfo->getAllTags();
$hasNewAttrGroups = $this->processApplyAttrGroups($tags, $phpDocInfo, $node);

if ($hasNewAttrGroups) {
return $node;
Expand All @@ -167,4 +139,64 @@ public function configure(array $configuration): void

$this->annotationsToAttributes = $annotationsToAttributes;
}

/**
* @param array<PhpDocTagNode> $tags
* @param Class_|Property|ClassMethod|Function_|Closure|ArrowFunction $node
*/
private function processApplyAttrGroups(array $tags, PhpDocInfo $phpDocInfo, Node $node): bool
{
$hasNewAttrGroups = false;
foreach ($tags as $tag) {
foreach ($this->annotationsToAttributes as $annotationToAttribute) {
$annotationToAttributeTag = $annotationToAttribute->getTag();
if ($phpDocInfo->hasByName($annotationToAttributeTag)) {
// 1. remove php-doc tag
$this->phpDocTagRemover->removeByName($phpDocInfo, $annotationToAttributeTag);

// 2. add attributes
$node->attrGroups[] = $this->phpAttributeGroupFactory->createFromSimpleTag(
$annotationToAttribute
);

$hasNewAttrGroups = true;

continue 2;
}

if ($this->shouldSkip($tag->value, $phpDocInfo, $annotationToAttributeTag)) {
continue;
}

// 1. remove php-doc tag
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $tag->value);

// 2. add attributes
/** @var DoctrineAnnotationTagValueNode $tagValue */
$tagValue = $tag->value;
$node->attrGroups[] = $this->phpAttributeGroupFactory->create(
$tagValue,
$annotationToAttribute
);

$hasNewAttrGroups = true;
continue 2;
}
}

return $hasNewAttrGroups;
}

private function shouldSkip(
PhpDocTagValueNode $phpDocTagValueNode,
PhpDocInfo $phpDocInfo,
string $annotationToAttributeTag
): bool {
$doctrineAnnotationTagValueNode = $phpDocInfo->getByAnnotationClass($annotationToAttributeTag);
if ($phpDocTagValueNode !== $doctrineAnnotationTagValueNode) {
return true;
}

return ! $phpDocTagValueNode instanceof DoctrineAnnotationTagValueNode;
}
}

0 comments on commit e0b4e85

Please sign in to comment.