Skip to content

Commit

Permalink
feat: add the option to use the annotation value as an argument to th…
Browse files Browse the repository at this point in the history
…e attribute (#6468)
  • Loading branch information
carlos-granados authored Nov 24, 2024
1 parent fb169fa commit 1ec42bc
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

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

final class WithValueAsArgument
{
/**
* @When this is a simple annotation with a value
* @When
* @When "this value is within quotes"
* @When this value has a ' character
* @When(key="value") this annotation has parameters so won't use this option
*/
public function someStep(): void
{
}
}

?>
-----
<?php

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

final class WithValueAsArgument
{
#[\Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Behat\When('this is a simple annotation with a value')]
#[\Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Behat\When]
#[\Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Behat\When('"this value is within quotes"')]
#[\Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Behat\When('this value has a \' character')]
#[\Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Behat\When(key: 'value')] // this annotation has parameters so won't use this option
public function someStep(): void
{
}
}

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

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Behat;

final class When
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Annotation\OpenApi\Annotation\NestedPastAnnotation;
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Annotation\OpenApi\PastAnnotation;
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Behat\When;
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\OpenApi\Attribute\NestedFutureAttribute;
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\OpenApi\FutureAttribute;
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation;
Expand Down Expand Up @@ -47,5 +48,6 @@
'Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\UseAlias\TestOther'
),
new AnnotationToAttribute('Sensio\Bundle\FrameworkExtraBundle\Configuration\Security'),
new AnnotationToAttribute('When', When::class, useValueAsAttributeArgument: true),
]);
};
5 changes: 4 additions & 1 deletion rules/Php80/Rector/Class_/AnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ private function processGenericTags(PhpDocInfo $phpDocInfo): array
continue;
}

$attributeGroups[] = $this->phpAttributeGroupFactory->createFromSimpleTag($annotationToAttribute);
$attributeGroups[] = $this->phpAttributeGroupFactory->createFromSimpleTag(
$annotationToAttribute,
$annotationToAttribute->getUseValueAsAttributeArgument() ? (string) $docNode->value : null
);
return PhpDocNodeTraverser::NODE_REMOVE;
}

Expand Down
8 changes: 7 additions & 1 deletion rules/Php80/ValueObject/AnnotationToAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -48,4 +49,9 @@ public function getClassReferenceFields(): array
{
return $this->classReferenceFields;
}

public function getUseValueAsAttributeArgument(): bool
{
return $this->useValueAsAttributeArgument;
}
}
15 changes: 11 additions & 4 deletions src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down

0 comments on commit 1ec42bc

Please sign in to comment.