From 1ec42bcc33f0607533e08d75c47b589d89e7649e Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Sun, 24 Nov 2024 18:23:37 +0100 Subject: [PATCH] feat: add the option to use the annotation value as an argument to the attribute (#6468) --- .../Fixture/with_value_as_argument.php.inc | 37 +++++++++++++++++++ .../Source/Attribute/Behat/When.php | 9 +++++ .../config/configured_rule.php | 2 + .../Class_/AnnotationToAttributeRector.php | 5 ++- .../ValueObject/AnnotationToAttribute.php | 8 +++- .../NodeFactory/PhpAttributeGroupFactory.php | 15 ++++++-- 6 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/with_value_as_argument.php.inc create mode 100644 rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Source/Attribute/Behat/When.php diff --git a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/with_value_as_argument.php.inc b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/with_value_as_argument.php.inc new file mode 100644 index 00000000000..16c10051635 --- /dev/null +++ b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/with_value_as_argument.php.inc @@ -0,0 +1,37 @@ + +----- + diff --git a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Source/Attribute/Behat/When.php b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Source/Attribute/Behat/When.php new file mode 100644 index 00000000000..512f0aab72e --- /dev/null +++ b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Source/Attribute/Behat/When.php @@ -0,0 +1,9 @@ +phpAttributeGroupFactory->createFromSimpleTag($annotationToAttribute); + $attributeGroups[] = $this->phpAttributeGroupFactory->createFromSimpleTag( + $annotationToAttribute, + $annotationToAttribute->getUseValueAsAttributeArgument() ? (string) $docNode->value : null + ); return PhpDocNodeTraverser::NODE_REMOVE; } diff --git a/rules/Php80/ValueObject/AnnotationToAttribute.php b/rules/Php80/ValueObject/AnnotationToAttribute.php index 4ad5b9a87b0..785fcfe80d8 100644 --- a/rules/Php80/ValueObject/AnnotationToAttribute.php +++ b/rules/Php80/ValueObject/AnnotationToAttribute.php @@ -16,7 +16,8 @@ public function __construct( private string $tag, private ?string $attributeClass = null, - private array $classReferenceFields = [] + private array $classReferenceFields = [], + private bool $useValueAsAttributeArgument = false, ) { RectorAssert::className($tag); @@ -48,4 +49,9 @@ public function getClassReferenceFields(): array { return $this->classReferenceFields; } + + public function getUseValueAsAttributeArgument(): bool + { + return $this->useValueAsAttributeArgument; + } } diff --git a/src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php b/src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php index ac74985038f..291c6207e7d 100644 --- a/src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php +++ b/src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php @@ -36,18 +36,25 @@ public function __construct( ) { } - public function createFromSimpleTag(AnnotationToAttribute $annotationToAttribute): AttributeGroup - { - return $this->createFromClass($annotationToAttribute->getAttributeClass()); + public function createFromSimpleTag( + AnnotationToAttribute $annotationToAttribute, + ?string $value = null + ): AttributeGroup { + return $this->createFromClass($annotationToAttribute->getAttributeClass(), $value); } /** * @param AttributeName::*|string $attributeClass */ - public function createFromClass(string $attributeClass): AttributeGroup + public function createFromClass(string $attributeClass, ?string $value = null): AttributeGroup { $fullyQualified = new FullyQualified($attributeClass); $attribute = new Attribute($fullyQualified); + if ($value !== null && $value !== '') { + $arg = new Arg(new String_($value)); + $attribute->args = [$arg]; + } + return new AttributeGroup([$attribute]); }