Skip to content

Commit

Permalink
move JoinColumns to NestedAnnotationToAttributeRector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 18, 2022
1 parent 8b366be commit 7875554
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 45 deletions.
14 changes: 7 additions & 7 deletions packages/PhpAttribute/RemovableAnnotationAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

final class RemovableAnnotationAnalyzer
{
/**
* Annotation classes that only holds nested annotation, but have no alternative in attributes.
* Can be removed.
*
* @var string[]
*/
private const REMOVABLE_ANNOTATION_CLASSES = ['Doctrine\ORM\Mapping\JoinColumns'];
// /**
// * Annotation classes that only holds nested annotation, but have no alternative in attributes.
// * Can be removed.
// *
// * @var string[]
// */
// private const REMOVABLE_ANNOTATION_CLASSES = ['Doctrine\ORM\Mapping\JoinColumns'];

/**
* @var AnnotationToAttribute[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

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

use Doctrine\ORM\Mapping as ORM;
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation;

final class TrailingComma
{
/**
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true, )
**/
* @GenericAnnotation(key="value", )
*/
protected $someColumn;
}

Expand All @@ -18,11 +18,11 @@ final class TrailingComma

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

use Doctrine\ORM\Mapping as ORM;
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation;

final class TrailingComma
{
#[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'id', nullable: true)]
#[GenericAnnotation(key: 'value')]
protected $someColumn;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
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'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\DiscriminatorMap'),

// validation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

use Doctrine\ORM\Mapping as ORM;

Expand All @@ -19,7 +19,7 @@ final class DoctrineNestedJoinColumns
-----
<?php

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

use Doctrine\ORM\Mapping as ORM;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@
'joinColumns' => 'Doctrine\ORM\Mapping\JoinColumn',
'inverseJoinColumns' => 'Doctrine\ORM\Mapping\InverseJoinColumn',
]),

new NestedAnnotationToAttribute('Doctrine\ORM\Mapping\JoinColumns', [
'Doctrine\ORM\Mapping\JoinColumn',
], true),
]);
};
17 changes: 11 additions & 6 deletions rules/Php80/NodeFactory/NestedAttrGroupsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ public function create(array $nestedDoctrineTagAndAnnotationToAttributes, array
foreach ($nestedDoctrineTagAndAnnotationToAttributes as $nestedDoctrineTagAndAnnotationToAttribute) {
$doctrineAnnotationTagValueNode = $nestedDoctrineTagAndAnnotationToAttribute->getDoctrineAnnotationTagValueNode();

// add attributes
$attributeGroups[] = $this->phpNestedAttributeGroupFactory->create(
$doctrineAnnotationTagValueNode,
$nestedDoctrineTagAndAnnotationToAttribute->getNestedAnnotationToAttribute(),
$uses
);
$nestedAnnotationToAttribute = $nestedDoctrineTagAndAnnotationToAttribute->getNestedAnnotationToAttribute();

// do not create alternative for the annotation, only unwrap
if (! $nestedAnnotationToAttribute->shouldRemoveOriginal()) {
// add attributes
$attributeGroups[] = $this->phpNestedAttributeGroupFactory->create(
$doctrineAnnotationTagValueNode,
$nestedDoctrineTagAndAnnotationToAttribute->getNestedAnnotationToAttribute(),
$uses
);
}

$nestedAttributeGroups = $this->phpNestedAttributeGroupFactory->createNested(
$doctrineAnnotationTagValueNode,
Expand Down
36 changes: 18 additions & 18 deletions rules/Php80/Rector/Class_/AnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,24 +258,24 @@ private function processDoctrineAnnotationClasses(PhpDocInfo $phpDocInfo, array
$shouldInlinedNested = true;
}

if (! $this->removableAnnotationAnalyzer->isRemovable($doctrineTagValueNode)) {
$doctrineTagAndAnnotationToAttributes[] = new DoctrineTagAndAnnotationToAttribute(
$doctrineTagValueNode,
$annotationToAttribute,
);
} else {
$shouldInlinedNested = true;
}

if ($shouldInlinedNested) {
// inline nested
foreach ($nestedDoctrineAnnotationTagValueNodes as $nestedDoctrineAnnotationTagValueNode) {
$doctrineTagAndAnnotationToAttributes[] = new DoctrineTagAndAnnotationToAttribute(
$nestedDoctrineAnnotationTagValueNode,
$annotationToAttribute,
);
}
}
// if (! $this->removableAnnotationAnalyzer->isRemovable($doctrineTagValueNode)) {
$doctrineTagAndAnnotationToAttributes[] = new DoctrineTagAndAnnotationToAttribute(
$doctrineTagValueNode,
$annotationToAttribute,
);
// } else {
// $shouldInlinedNested = true;
// }

// if ($shouldInlinedNested) {
// // inline nested
// foreach ($nestedDoctrineAnnotationTagValueNodes as $nestedDoctrineAnnotationTagValueNode) {
// $doctrineTagAndAnnotationToAttributes[] = new DoctrineTagAndAnnotationToAttribute(
// $nestedDoctrineAnnotationTagValueNode,
// $annotationToAttribute,
// );
// }
// }

$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $doctrineTagValueNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ private function transformDoctrineAnnotationClassesToAttributeGroups(PhpDocInfo
continue;
}

$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $doctrineTagValueNode);

$nestedDoctrineTagAndAnnotationToAttributes[] = new NestedDoctrineTagAndAnnotationToAttribute(
$doctrineTagValueNode,
$nestedAnnotationToAttribute,
);

$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $doctrineTagValueNode);
}

return $this->nestedAttrGroupsFactory->create($nestedDoctrineTagAndAnnotationToAttributes, $uses);
Expand Down
12 changes: 9 additions & 3 deletions rules/Php80/ValueObject/NestedAnnotationToAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
final class NestedAnnotationToAttribute implements AnnotationToAttributeInterface
{
/**
* @param array<string, string> $annotationPropertiesToAttributeClasses
* @param array<string, string>|string[] $annotationPropertiesToAttributeClasses
*/
public function __construct(
private readonly string $tag,
private readonly array $annotationPropertiesToAttributeClasses
private readonly array $annotationPropertiesToAttributeClasses,
private bool $removeOriginal = false
) {
RectorAssert::className($tag);
}
Expand All @@ -25,7 +26,7 @@ public function getTag(): string
}

/**
* @return array<string, string>
* @return array<string, string>|string[]
*/
public function getAnnotationPropertiesToAttributeClasses(): array
{
Expand All @@ -36,4 +37,9 @@ public function getAttributeClass(): string
{
return $this->tag;
}

public function shouldRemoveOriginal(): bool
{
return $this->removeOriginal;
}
}

0 comments on commit 7875554

Please sign in to comment.