-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHP 8.0] Add NestedAnnotationToAttributeRector (#2781)
* [PHP 8.0] Add NestedAnnotationToAttributeRector * remove unused class-string * [PHP 8.0] Add NestedAnnotationToAttribute * move JoinColumns to NestedAnnotationToAttributeRector * [ci-review] Rector Rectify Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
f41acb3
commit b8549f5
Showing
23 changed files
with
620 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
packages/PhpAttribute/NodeFactory/PhpNestedAttributeGroupFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PhpAttribute\NodeFactory; | ||
|
||
use Nette\Utils\Strings; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Attribute; | ||
use PhpParser\Node\AttributeGroup; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Stmt\Use_; | ||
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode; | ||
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode; | ||
use Rector\Php80\ValueObject\NestedAnnotationToAttribute; | ||
use Rector\PhpAttribute\AnnotationToAttributeMapper; | ||
use Rector\PhpAttribute\AttributeArrayNameInliner; | ||
use Rector\PhpAttribute\NodeAnalyzer\ExprParameterReflectionTypeCorrector; | ||
|
||
final class PhpNestedAttributeGroupFactory | ||
{ | ||
/** | ||
* @var string | ||
* @see https://regex101.com/r/g3d9jy/1 | ||
*/ | ||
private const SHORT_ORM_ALIAS_REGEX = '#^@ORM#'; | ||
|
||
public function __construct( | ||
private readonly AnnotationToAttributeMapper $annotationToAttributeMapper, | ||
private readonly AttributeNameFactory $attributeNameFactory, | ||
private readonly NamedArgsFactory $namedArgsFactory, | ||
private readonly ExprParameterReflectionTypeCorrector $exprParameterReflectionTypeCorrector, | ||
private readonly AttributeArrayNameInliner $attributeArrayNameInliner | ||
) { | ||
} | ||
|
||
/** | ||
* @param Use_[] $uses | ||
*/ | ||
public function create( | ||
DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode, | ||
NestedAnnotationToAttribute $nestedAnnotationToAttribute, | ||
array $uses | ||
): AttributeGroup { | ||
$values = $doctrineAnnotationTagValueNode->getValuesWithExplicitSilentAndWithoutQuotes(); | ||
|
||
$args = $this->createArgsFromItems($values, $nestedAnnotationToAttribute); | ||
$args = $this->attributeArrayNameInliner->inlineArrayToArgs($args); | ||
|
||
$attributeName = $this->attributeNameFactory->create( | ||
$nestedAnnotationToAttribute, | ||
$doctrineAnnotationTagValueNode, | ||
$uses | ||
); | ||
|
||
$attribute = new Attribute($attributeName, $args); | ||
return new AttributeGroup([$attribute]); | ||
} | ||
|
||
/** | ||
* @return AttributeGroup[] | ||
*/ | ||
public function createNested( | ||
DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode, | ||
NestedAnnotationToAttribute $nestedAnnotationToAttribute, | ||
): array { | ||
$attributeGroups = []; | ||
|
||
$values = $doctrineAnnotationTagValueNode->getValuesWithExplicitSilentAndWithoutQuotes(); | ||
|
||
foreach ($nestedAnnotationToAttribute->getAnnotationPropertiesToAttributeClasses() as $itemName => $nestedAttributeClass) { | ||
$nestedValues = $values[$itemName] ?? null; | ||
if ($nestedValues === null) { | ||
continue; | ||
} | ||
|
||
if ($nestedValues instanceof CurlyListNode) { | ||
foreach ($nestedValues->getValues() as $nestedDoctrineAnnotationTagValueNode) { | ||
/** @var DoctrineAnnotationTagValueNode $nestedDoctrineAnnotationTagValueNode */ | ||
$args = $this->createArgsFromItems( | ||
$nestedDoctrineAnnotationTagValueNode->getValuesWithExplicitSilentAndWithoutQuotes(), | ||
$nestedAnnotationToAttribute | ||
); | ||
|
||
$args = $this->attributeArrayNameInliner->inlineArrayToArgs($args); | ||
|
||
$originalIdentifier = $nestedDoctrineAnnotationTagValueNode->identifierTypeNode->name; | ||
|
||
$attributeName = $this->resolveAliasedAttributeName($originalIdentifier, $nestedAttributeClass); | ||
|
||
$attribute = new Attribute($attributeName, $args); | ||
$attributeGroups[] = new AttributeGroup([$attribute]); | ||
} | ||
} | ||
} | ||
|
||
return $attributeGroups; | ||
} | ||
|
||
/** | ||
* @param mixed[] $items | ||
* @return Arg[] | ||
*/ | ||
private function createArgsFromItems(array $items, NestedAnnotationToAttribute $nestedAnnotationToAttribute): array | ||
{ | ||
// remove nested items | ||
foreach (array_keys($nestedAnnotationToAttribute->getAnnotationPropertiesToAttributeClasses()) as $itemName) { | ||
unset($items[$itemName]); | ||
} | ||
|
||
/** @var Expr[]|Expr\Array_ $items */ | ||
$items = $this->annotationToAttributeMapper->map($items); | ||
|
||
$items = $this->exprParameterReflectionTypeCorrector->correctItemsByAttributeClass( | ||
$items, | ||
$nestedAnnotationToAttribute->getTag() | ||
); | ||
|
||
return $this->namedArgsFactory->createFromValues($items); | ||
} | ||
|
||
/** | ||
* @todo improve this hardcoded approach later | ||
*/ | ||
private function resolveAliasedAttributeName( | ||
string $originalIdentifier, | ||
string $nestedAttributeClass | ||
): FullyQualified|Name { | ||
$matches = Strings::match($originalIdentifier, self::SHORT_ORM_ALIAS_REGEX); | ||
|
||
if ($matches !== null) { | ||
// or alias | ||
$shortDoctrineAttributeName = Strings::after($nestedAttributeClass, '\\', -1); | ||
return new Name('ORM\\' . $shortDoctrineAttributeName); | ||
} | ||
|
||
return new FullyQualified($nestedAttributeClass); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...Property/NestedAnnotationToAttributeRector/Fixture/multiple_inversed_join_columns.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Property\NestedAnnotationToAttributeRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
final class MultipleInversedJoinColumns | ||
{ | ||
/** | ||
* @ORM\JoinTable(name="join_table_name", | ||
* inverseJoinColumns={ | ||
* @ORM\JoinColumn(name="target_id"), | ||
* @ORM\JoinColumn(name="another_id") | ||
* } | ||
* ) | ||
*/ | ||
private $collection; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Property\NestedAnnotationToAttributeRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
final class MultipleInversedJoinColumns | ||
{ | ||
#[ORM\JoinTable(name: 'join_table_name')] | ||
#[ORM\InverseJoinColumn(name: 'target_id')] | ||
#[ORM\InverseJoinColumn(name: 'another_id')] | ||
private $collection; | ||
} | ||
|
||
?> |
34 changes: 34 additions & 0 deletions
34
...-tests/Php80/Rector/Property/NestedAnnotationToAttributeRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Property\NestedAnnotationToAttributeRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
final class SomeEntity | ||
{ | ||
/** | ||
* @ORM\JoinTable(name="join_table_name", | ||
* joinColumns={@ORM\JoinColumn(name="origin_id")}, | ||
* inverseJoinColumns={@ORM\JoinColumn(name="target_id")} | ||
* ) | ||
*/ | ||
private $collection; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Property\NestedAnnotationToAttributeRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
final class SomeEntity | ||
{ | ||
#[ORM\JoinTable(name: 'join_table_name')] | ||
#[ORM\JoinColumn(name: 'origin_id')] | ||
#[ORM\InverseJoinColumn(name: 'target_id')] | ||
private $collection; | ||
} | ||
|
||
?> |
Oops, something went wrong.