Skip to content

Commit

Permalink
[Attributes] Cover slash newline in AnnotationToAttributeRector with …
Browse files Browse the repository at this point in the history
…values (#6557)

* use stubs for Then and When

* cover slash newline in AnnotationToAttributeRector
  • Loading branch information
TomasVotruba authored Dec 11, 2024
1 parent 7f9dae6 commit 4b38333
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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

final class WithNextLine
{
/**
* @Then then :value should \
* continue here
*/
public function someStep(): void
{
}
}

?>
-----
<?php

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

final class WithNextLine
{
#[\Behat\Step\Then('then :value should continue here')]
public function someStep(): void
{
}
}

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

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

final class WithValueEdgeCases
{
/**
* @Then then :value should be have been finished
*/
public function someStep(): void
{
}
}

?>
-----
<?php

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

final class WithValueEdgeCases
{
#[\Behat\Step\Then('then :value should be have been finished')]
public function someStep(): void
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ 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
#[\Behat\Step\When('this is a simple annotation with a value')]
#[\Behat\Step\When]
#[\Behat\Step\When('"this value is within quotes"')]
#[\Behat\Step\When('this value has a \' character')]
#[\Behat\Step\When(key: 'value')] // this annotation has parameters so won't use this option
public function someStep(): void
{
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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 @@ -48,6 +47,9 @@
'Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\UseAlias\TestOther'
),
new AnnotationToAttribute('Sensio\Bundle\FrameworkExtraBundle\Configuration\Security'),
new AnnotationToAttribute('When', When::class, useValueAsAttributeArgument: true),

// special case with following comment becoming a inner value
new AnnotationToAttribute('When', \Behat\Step\When::class, useValueAsAttributeArgument: true),
new AnnotationToAttribute('Then', \Behat\Step\Then::class, useValueAsAttributeArgument: true),
]);
};
13 changes: 12 additions & 1 deletion rules/Php80/Rector/Class_/AnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Php80\Rector\Class_;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\ArrowFunction;
Expand Down Expand Up @@ -204,10 +205,20 @@ private function processGenericTags(PhpDocInfo $phpDocInfo): array
continue;
}

$docValue = null;
if ($annotationToAttribute->getUseValueAsAttributeArgument()) {
// special case for newline
$docValue = (string) $docNode->value;
if (str_contains($docValue, '\\')) {
$docValue = Strings::replace($docValue, "#\\\\\n#", '');
}
}

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

return PhpDocNodeTraverser::NODE_REMOVE;
}

Expand Down
12 changes: 12 additions & 0 deletions stubs/Behat/Step/Then.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Behat\Step;

if (class_exists('Behat\Step\Then')) {
return;
}

class Then
{

}
12 changes: 12 additions & 0 deletions stubs/Behat/Step/When.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Behat\Step;

if (class_exists('Behat\Step\When')) {
return;
}

class When
{

}

0 comments on commit 4b38333

Please sign in to comment.