Skip to content

Commit

Permalink
Updated Rector to commit 2ff0f08bd7f7f6c848821bcabf0872f81a9c440b
Browse files Browse the repository at this point in the history
rectorphp/rector-src@2ff0f08 [PHP 8.0] Add always class to AnnotationToAttribute to include string to ::class reference conversion (#5619)
  • Loading branch information
TomasVotruba committed Feb 14, 2024
1 parent a022b93 commit 055cd31
Show file tree
Hide file tree
Showing 24 changed files with 147 additions and 180 deletions.
20 changes: 19 additions & 1 deletion rules/Php80/ValueObject/AnnotationToAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Rector\Php80\Contract\ValueObject\AnnotationToAttributeInterface;
use Rector\Validation\RectorAssert;
use RectorPrefix202402\Webmozart\Assert\Assert;
final class AnnotationToAttribute implements AnnotationToAttributeInterface
{
/**
Expand All @@ -17,14 +18,24 @@ final class AnnotationToAttribute implements AnnotationToAttributeInterface
* @var string|null
*/
private $attributeClass;
public function __construct(string $tag, ?string $attributeClass = null)
/**
* @var string[]
* @readonly
*/
private $classReferenceFields = [];
/**
* @param string[] $classReferenceFields
*/
public function __construct(string $tag, ?string $attributeClass = null, array $classReferenceFields = [])
{
$this->tag = $tag;
$this->attributeClass = $attributeClass;
$this->classReferenceFields = $classReferenceFields;
RectorAssert::className($tag);
if (\is_string($attributeClass)) {
RectorAssert::className($attributeClass);
}
Assert::allString($classReferenceFields);
}
public function getTag() : string
{
Expand All @@ -37,4 +48,11 @@ public function getAttributeClass() : string
}
return $this->attributeClass;
}
/**
* @return string[]
*/
public function getClassReferenceFields() : array
{
return $this->classReferenceFields;
}
}
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '435d125053eed8764652ccf8565bde1660ee08ce';
public const PACKAGE_VERSION = '2ff0f08bd7f7f6c848821bcabf0872f81a9c440b';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-02-14 15:37:49';
public const RELEASE_DATE = '2024-02-14 19:26:45';
/**
* @var int
*/
Expand Down
2 changes: 1 addition & 1 deletion src/PhpAttribute/AnnotationToAttributeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(array $annotationToAttributeMappers)
Assert::notEmpty($annotationToAttributeMappers);
}
/**
* @return Expr|DocTagNodeState::REMOVE_ARRAY
* @return mixed|DocTagNodeState::REMOVE_ARRAY
* @param mixed $value
*/
public function map($value)
Expand Down
3 changes: 2 additions & 1 deletion src/PhpAttribute/AttributeArrayNameInliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ final class AttributeArrayNameInliner
{
/**
* @param Array_|Arg[] $array
* @param string[] $classReferenceFields
* @return Arg[]
*/
public function inlineArrayToArgs($array) : array
public function inlineArrayToArgs($array, array $classReferenceFields = []) : array
{
if (\is_array($array)) {
return $this->inlineArray($array);
Expand Down
103 changes: 0 additions & 103 deletions src/PhpAttribute/NodeAnalyzer/ExprParameterReflectionTypeCorrector.php

This file was deleted.

52 changes: 39 additions & 13 deletions src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Use_;
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Rector\PhpAttribute\AnnotationToAttributeMapper;
use Rector\PhpAttribute\AttributeArrayNameInliner;
use Rector\PhpAttribute\NodeAnalyzer\ExprParameterReflectionTypeCorrector;
/**
* @see \Rector\Tests\PhpAttribute\Printer\PhpAttributeGroupFactoryTest
*/
Expand All @@ -36,22 +39,16 @@ final class PhpAttributeGroupFactory
* @var \Rector\PhpAttribute\NodeFactory\NamedArgsFactory
*/
private $namedArgsFactory;
/**
* @readonly
* @var \Rector\PhpAttribute\NodeAnalyzer\ExprParameterReflectionTypeCorrector
*/
private $exprParameterReflectionTypeCorrector;
/**
* @readonly
* @var \Rector\PhpAttribute\AttributeArrayNameInliner
*/
private $attributeArrayNameInliner;
public function __construct(AnnotationToAttributeMapper $annotationToAttributeMapper, \Rector\PhpAttribute\NodeFactory\AttributeNameFactory $attributeNameFactory, \Rector\PhpAttribute\NodeFactory\NamedArgsFactory $namedArgsFactory, ExprParameterReflectionTypeCorrector $exprParameterReflectionTypeCorrector, AttributeArrayNameInliner $attributeArrayNameInliner)
public function __construct(AnnotationToAttributeMapper $annotationToAttributeMapper, \Rector\PhpAttribute\NodeFactory\AttributeNameFactory $attributeNameFactory, \Rector\PhpAttribute\NodeFactory\NamedArgsFactory $namedArgsFactory, AttributeArrayNameInliner $attributeArrayNameInliner)
{
$this->annotationToAttributeMapper = $annotationToAttributeMapper;
$this->attributeNameFactory = $attributeNameFactory;
$this->namedArgsFactory = $namedArgsFactory;
$this->exprParameterReflectionTypeCorrector = $exprParameterReflectionTypeCorrector;
$this->attributeArrayNameInliner = $attributeArrayNameInliner;
}
public function createFromSimpleTag(AnnotationToAttribute $annotationToAttribute) : AttributeGroup
Expand Down Expand Up @@ -81,7 +78,7 @@ public function createFromClassWithItems(string $attributeClass, array $items) :
public function create(DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode, AnnotationToAttribute $annotationToAttribute, array $uses) : AttributeGroup
{
$values = $doctrineAnnotationTagValueNode->getValuesWithSilentKey();
$args = $this->createArgsFromItems($values, $annotationToAttribute->getAttributeClass());
$args = $this->createArgsFromItems($values, $annotationToAttribute->getAttributeClass(), $annotationToAttribute->getClassReferenceFields());
$args = $this->attributeArrayNameInliner->inlineArrayToArgs($args);
$attributeName = $this->attributeNameFactory->create($annotationToAttribute, $doctrineAnnotationTagValueNode, $uses);
// keep FQN in the attribute, so it can be easily detected later
Expand All @@ -93,14 +90,43 @@ public function create(DoctrineAnnotationTagValueNode $doctrineAnnotationTagValu
* @api tests
*
* @param ArrayItemNode[]|mixed[] $items
* @param string[] $classReferencedFields
* @return Arg[]
*/
public function createArgsFromItems(array $items, string $attributeClass) : array
public function createArgsFromItems(array $items, string $attributeClass, array $classReferencedFields = []) : array
{
/** @var Expr[]|Expr\Array_ $mappedItems */
$mappedItems = $this->annotationToAttributeMapper->map($items);
$mappedItems = $this->exprParameterReflectionTypeCorrector->correctItemsByAttributeClass($mappedItems, $attributeClass);
$this->mapClassReferences($mappedItems, $classReferencedFields);
$values = $mappedItems instanceof Array_ ? $mappedItems->items : $mappedItems;
// the key here should contain the named argument
return $this->namedArgsFactory->createFromValues($mappedItems);
return $this->namedArgsFactory->createFromValues($values);
}
/**
* @param string[] $classReferencedFields
* @param \PhpParser\Node\Expr|string $expr
*/
private function mapClassReferences($expr, array $classReferencedFields) : void
{
if (!$expr instanceof Array_) {
return;
}
foreach ($expr->items as $arrayItem) {
if (!$arrayItem instanceof ArrayItem) {
continue;
}
if (!$arrayItem->key instanceof String_) {
continue;
}
if (!\in_array($arrayItem->key->value, $classReferencedFields)) {
continue;
}
if ($arrayItem->value instanceof ClassConstFetch) {
continue;
}
if (!$arrayItem->value instanceof String_) {
continue;
}
$arrayItem->value = new ClassConstFetch(new FullyQualified($arrayItem->value->value), 'class');
}
}
}
28 changes: 10 additions & 18 deletions src/PhpAttribute/NodeFactory/PhpNestedAttributeGroupFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Nop;
Expand All @@ -26,7 +26,6 @@
use Rector\Php80\ValueObject\NestedAnnotationToAttribute;
use Rector\PhpAttribute\AnnotationToAttributeMapper;
use Rector\PhpAttribute\AttributeArrayNameInliner;
use Rector\PhpAttribute\NodeAnalyzer\ExprParameterReflectionTypeCorrector;
use RectorPrefix202402\Webmozart\Assert\Assert;
final class PhpNestedAttributeGroupFactory
{
Expand All @@ -45,11 +44,6 @@ final class PhpNestedAttributeGroupFactory
* @var \Rector\PhpAttribute\NodeFactory\NamedArgsFactory
*/
private $namedArgsFactory;
/**
* @readonly
* @var \Rector\PhpAttribute\NodeAnalyzer\ExprParameterReflectionTypeCorrector
*/
private $exprParameterReflectionTypeCorrector;
/**
* @readonly
* @var \Rector\PhpAttribute\AttributeArrayNameInliner
Expand All @@ -65,12 +59,11 @@ final class PhpNestedAttributeGroupFactory
* @var \Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser
*/
private $staticDoctrineAnnotationParser;
public function __construct(AnnotationToAttributeMapper $annotationToAttributeMapper, \Rector\PhpAttribute\NodeFactory\AttributeNameFactory $attributeNameFactory, \Rector\PhpAttribute\NodeFactory\NamedArgsFactory $namedArgsFactory, ExprParameterReflectionTypeCorrector $exprParameterReflectionTypeCorrector, AttributeArrayNameInliner $attributeArrayNameInliner, TokenIteratorFactory $tokenIteratorFactory, StaticDoctrineAnnotationParser $staticDoctrineAnnotationParser)
public function __construct(AnnotationToAttributeMapper $annotationToAttributeMapper, \Rector\PhpAttribute\NodeFactory\AttributeNameFactory $attributeNameFactory, \Rector\PhpAttribute\NodeFactory\NamedArgsFactory $namedArgsFactory, AttributeArrayNameInliner $attributeArrayNameInliner, TokenIteratorFactory $tokenIteratorFactory, StaticDoctrineAnnotationParser $staticDoctrineAnnotationParser)
{
$this->annotationToAttributeMapper = $annotationToAttributeMapper;
$this->attributeNameFactory = $attributeNameFactory;
$this->namedArgsFactory = $namedArgsFactory;
$this->exprParameterReflectionTypeCorrector = $exprParameterReflectionTypeCorrector;
$this->attributeArrayNameInliner = $attributeArrayNameInliner;
$this->tokenIteratorFactory = $tokenIteratorFactory;
$this->staticDoctrineAnnotationParser = $staticDoctrineAnnotationParser;
Expand All @@ -82,7 +75,7 @@ public function create(DoctrineAnnotationTagValueNode $doctrineAnnotationTagValu
{
$values = $doctrineAnnotationTagValueNode->getValues();
$values = $this->removeItems($values, $nestedAnnotationToAttribute);
$args = $this->createArgsFromItems($values, $nestedAnnotationToAttribute);
$args = $this->createArgsFromItems($values);
$args = $this->attributeArrayNameInliner->inlineArrayToArgs($args);
$attributeName = $this->attributeNameFactory->create($nestedAnnotationToAttribute, $doctrineAnnotationTagValueNode, $uses);
$attribute = new Attribute($attributeName, $args);
Expand All @@ -107,7 +100,7 @@ public function createNested(DoctrineAnnotationTagValueNode $doctrineAnnotationT
if (!$nestedArrayItemNode->value instanceof DoctrineAnnotationTagValueNode) {
continue;
}
$attributeArgs = $this->createAttributeArgs($nestedArrayItemNode->value, $nestedAnnotationToAttribute);
$attributeArgs = $this->createAttributeArgs($nestedArrayItemNode->value);
$originalIdentifier = $doctrineAnnotationTagValueNode->identifierTypeNode->name;
$attributeName = $this->resolveAliasedAttributeName($originalIdentifier, $nestedAnnotationPropertyToAttributeClass);
$attribute = new Attribute($attributeName, $attributeArgs);
Expand All @@ -119,21 +112,20 @@ public function createNested(DoctrineAnnotationTagValueNode $doctrineAnnotationT
/**
* @return Arg[]
*/
private function createAttributeArgs(DoctrineAnnotationTagValueNode $nestedDoctrineAnnotationTagValueNode, NestedAnnotationToAttribute $nestedAnnotationToAttribute) : array
private function createAttributeArgs(DoctrineAnnotationTagValueNode $nestedDoctrineAnnotationTagValueNode) : array
{
$args = $this->createArgsFromItems($nestedDoctrineAnnotationTagValueNode->getValues(), $nestedAnnotationToAttribute);
$args = $this->createArgsFromItems($nestedDoctrineAnnotationTagValueNode->getValues());
return $this->attributeArrayNameInliner->inlineArrayToArgs($args);
}
/**
* @param ArrayItemNode[] $arrayItemNodes
* @return Arg[]
*/
private function createArgsFromItems(array $arrayItemNodes, NestedAnnotationToAttribute $nestedAnnotationToAttribute) : array
private function createArgsFromItems(array $arrayItemNodes) : array
{
/** @var Expr[]|Expr\Array_ $arrayItemNodes */
$arrayItemNodes = $this->annotationToAttributeMapper->map($arrayItemNodes);
$arrayItemNodes = $this->exprParameterReflectionTypeCorrector->correctItemsByAttributeClass($arrayItemNodes, $nestedAnnotationToAttribute->getTag());
return $this->namedArgsFactory->createFromValues($arrayItemNodes);
$values = $arrayItemNodes instanceof Array_ ? $arrayItemNodes->items : $arrayItemNodes;
return $this->namedArgsFactory->createFromValues($values);
}
/**
* @todo improve this hardcoded approach later
Expand Down Expand Up @@ -202,7 +194,7 @@ private function createFromExplicitProperties(NestedAnnotationToAttribute $neste
$values = $this->staticDoctrineAnnotationParser->resolveAnnotationMethodCall($nestedTokenIterator, new Nop());
$nestedDoctrineAnnotationTagValueNode = new DoctrineAnnotationTagValueNode($identifierTypeNode, $match['annotation_content'] ?? '', $values);
}
$attributeArgs = $this->createAttributeArgs($nestedDoctrineAnnotationTagValueNode, $nestedAnnotationToAttribute);
$attributeArgs = $this->createAttributeArgs($nestedDoctrineAnnotationTagValueNode);
$originalIdentifier = $nestedDoctrineAnnotationTagValueNode->identifierTypeNode->name;
$attributeName = $this->resolveAliasedAttributeName($originalIdentifier, $annotationPropertyToAttributeClass);
if ($annotationPropertyToAttributeClass->doesNeedNewImport() && \count($attributeName->getParts()) === 1) {
Expand Down
Loading

0 comments on commit 055cd31

Please sign in to comment.