Skip to content

Commit

Permalink
[PHP 8.0] Add class support to NestedAnnotationToAttributeRector (#2782)
Browse files Browse the repository at this point in the history
* [PHP 8.0] Add class support to AnnotationToAttributeRector

* cleanup

* remove unwrappable check, make it explicit on configuration
  • Loading branch information
TomasVotruba committed Aug 18, 2022
1 parent b8549f5 commit 69b625c
Show file tree
Hide file tree
Showing 22 changed files with 74 additions and 535 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
use Rector\PhpAttribute\AnnotationToAttributeMapper;
use Rector\PhpAttribute\AttributeArrayNameInliner;
use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
use Rector\PhpAttribute\Exception\InvalidNestedAttributeException;
use Rector\PhpAttribute\UnwrapableAnnotationAnalyzer;
use Symfony\Contracts\Service\Attribute\Required;

/**
Expand All @@ -27,7 +25,6 @@ final class DoctrineAnnotationAnnotationToAttributeMapper implements AnnotationT

public function __construct(
private readonly PhpVersionProvider $phpVersionProvider,
private readonly UnwrapableAnnotationAnalyzer $unwrapableAnnotationAnalyzer,
private readonly AttributeArrayNameInliner $attributeArrayNameInliner,
) {
}
Expand All @@ -47,19 +44,14 @@ public function isCandidate(mixed $value): bool
return false;
}

return ! $this->unwrapableAnnotationAnalyzer->areUnwrappable([$value]);
return $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NEW_INITIALIZERS);
}

/**
* @param DoctrineAnnotationTagValueNode $value
*/
public function map($value): New_
{
// if PHP 8.0- throw exception
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NEW_INITIALIZERS)) {
throw new InvalidNestedAttributeException();
}

$annotationShortName = $this->resolveAnnotationName($value);

$values = $value->getValues();
Expand Down

This file was deleted.

46 changes: 0 additions & 46 deletions packages/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Use_;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\Php80\ValueObject\AnnotationToAttribute;
Expand All @@ -23,14 +21,6 @@
*/
final class PhpAttributeGroupFactory
{
/**
* @var array<string, string[]>>
*/
private const UNWRAPPED_ANNOTATIONS = [
'Doctrine\ORM\Mapping\Table' => ['indexes', 'uniqueConstraints'],
'Doctrine\ORM\Mapping\Entity' => ['uniqueConstraints'],
];

public function __construct(
private readonly AnnotationToAttributeMapper $annotationToAttributeMapper,
private readonly AttributeNameFactory $attributeNameFactory,
Expand Down Expand Up @@ -98,42 +88,6 @@ public function createArgsFromItems(array $items, string $attributeClass): array

$items = $this->exprParameterReflectionTypeCorrector->correctItemsByAttributeClass($items, $attributeClass);

$items = $this->removeUnwrappedItems($attributeClass, $items);

return $this->namedArgsFactory->createFromValues($items);
}

/**
* @todo deprecated
*
* @param mixed[] $items
* @return mixed[]
*/
private function removeUnwrappedItems(string $attributeClass, array $items): array
{
// unshift annotations that can be extracted
$unwrappeColumns = self::UNWRAPPED_ANNOTATIONS[$attributeClass] ?? [];
if ($unwrappeColumns === []) {
return $items;
}

foreach ($items as $key => $item) {
if (! $item instanceof ArrayItem) {
continue;
}

if (! $item->key instanceof String_) {
continue;
}

$stringItemKey = $item->key;
if (! in_array($stringItemKey->value, $unwrappeColumns, true)) {
continue;
}

unset($items[$key]);
}

return $items;
}
}
76 changes: 0 additions & 76 deletions packages/PhpAttribute/UnwrapableAnnotationAnalyzer.php

This file was deleted.

This file was deleted.

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

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

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Table(name="user_account_role",
* uniqueConstraints={
* @ORM\UniqueConstraint(name="user_account_role_unique", columns={"user_account_id", "list_user_role_id"})
* },
* indexes={
* @ORM\Index(name="some_name")
* }
* )
*/
final class SkipTableAndUniqueConstraints
{
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ final class MultipleNestedAttribute
/**
* @Assert\All({
* @Assert\NotNull(),
* @Assert\NotNumber(),
* @Assert\NotNumber(secondValue=1000),
* @Assert\NotNumber(hey=10, hi="hello"),
* })
*/
public $value;
Expand All @@ -25,7 +26,7 @@ use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\SourcePhp81 as

final class MultipleNestedAttribute
{
#[Assert\All([new Assert\NotNull(), new Assert\NotNumber()])]
#[Assert\All([new Assert\NotNull(), new Assert\NotNumber(secondValue: 1000), new Assert\NotNumber(hey: 10, hi: 'hello')])]
public $value;
}

Expand Down
Loading

0 comments on commit 69b625c

Please sign in to comment.