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 df1bdaf
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 94 deletions.
63 changes: 0 additions & 63 deletions packages/PhpAttribute/RemovableAnnotationAnalyzer.php

This file was deleted.

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
15 changes: 4 additions & 11 deletions rules/Php80/Rector/Class_/AnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Rector\Php80\ValueObject\DoctrineTagAndAnnotationToAttribute;
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
use Rector\PhpAttribute\RemovableAnnotationAnalyzer;
use Rector\PhpAttribute\UnwrapableAnnotationAnalyzer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\Astral\PhpDocParser\PhpDocNodeTraverser;
Expand All @@ -56,7 +55,6 @@ public function __construct(
private readonly PhpDocTagRemover $phpDocTagRemover,
private readonly PhpDocNodeFinder $phpDocNodeFinder,
private readonly UnwrapableAnnotationAnalyzer $unwrapableAnnotationAnalyzer,
private readonly RemovableAnnotationAnalyzer $removableAnnotationAnalyzer,
private readonly AttributeGroupNamedArgumentManipulator $attributeGroupNamedArgumentManipulator,
private readonly PhpVersionProvider $phpVersionProvider,
private readonly UseImportsResolver $useImportsResolver,
Expand Down Expand Up @@ -152,7 +150,6 @@ public function configure(array $configuration): void
$this->annotationsToAttributes = $configuration;

$this->unwrapableAnnotationAnalyzer->configure($configuration);
$this->removableAnnotationAnalyzer->configure($configuration);
}

public function provideMinPhpVersion(): int
Expand Down Expand Up @@ -258,14 +255,10 @@ private function processDoctrineAnnotationClasses(PhpDocInfo $phpDocInfo, array
$shouldInlinedNested = true;
}

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

if ($shouldInlinedNested) {
// inline nested
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 df1bdaf

Please sign in to comment.