Skip to content

Commit 4b38333

Browse files
authored
[Attributes] Cover slash newline in AnnotationToAttributeRector with values (#6557)
* use stubs for Then and When * cover slash newline in AnnotationToAttributeRector
1 parent 7f9dae6 commit 4b38333

File tree

8 files changed

+104
-17
lines changed

8 files changed

+104
-17
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Behat;
4+
5+
final class WithNextLine
6+
{
7+
/**
8+
* @Then then :value should \
9+
* continue here
10+
*/
11+
public function someStep(): void
12+
{
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Behat;
21+
22+
final class WithNextLine
23+
{
24+
#[\Behat\Step\Then('then :value should continue here')]
25+
public function someStep(): void
26+
{
27+
}
28+
}
29+
30+
?>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Behat;
4+
5+
final class WithValueEdgeCases
6+
{
7+
/**
8+
* @Then then :value should be have been finished
9+
*/
10+
public function someStep(): void
11+
{
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Behat;
20+
21+
final class WithValueEdgeCases
22+
{
23+
#[\Behat\Step\Then('then :value should be have been finished')]
24+
public function someStep(): void
25+
{
26+
}
27+
}
28+
29+
?>

rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/with_value_as_argument.php.inc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;
2424

2525
final class WithValueAsArgument
2626
{
27-
#[\Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Behat\When('this is a simple annotation with a value')]
28-
#[\Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Behat\When]
29-
#[\Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Behat\When('"this value is within quotes"')]
30-
#[\Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Behat\When('this value has a \' character')]
31-
#[\Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Behat\When(key: 'value')] // this annotation has parameters so won't use this option
27+
#[\Behat\Step\When('this is a simple annotation with a value')]
28+
#[\Behat\Step\When]
29+
#[\Behat\Step\When('"this value is within quotes"')]
30+
#[\Behat\Step\When('this value has a \' character')]
31+
#[\Behat\Step\When(key: 'value')] // this annotation has parameters so won't use this option
3232
public function someStep(): void
3333
{
3434
}

rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Source/Attribute/Behat/When.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/config/configured_rule.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Rector\Php80\ValueObject\AnnotationToAttribute;
88
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Annotation\OpenApi\Annotation\NestedPastAnnotation;
99
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Annotation\OpenApi\PastAnnotation;
10-
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Behat\When;
1110
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\OpenApi\Attribute\NestedFutureAttribute;
1211
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\OpenApi\FutureAttribute;
1312
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation;
@@ -48,6 +47,9 @@
4847
'Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\UseAlias\TestOther'
4948
),
5049
new AnnotationToAttribute('Sensio\Bundle\FrameworkExtraBundle\Configuration\Security'),
51-
new AnnotationToAttribute('When', When::class, useValueAsAttributeArgument: true),
50+
51+
// special case with following comment becoming a inner value
52+
new AnnotationToAttribute('When', \Behat\Step\When::class, useValueAsAttributeArgument: true),
53+
new AnnotationToAttribute('Then', \Behat\Step\Then::class, useValueAsAttributeArgument: true),
5254
]);
5355
};

rules/Php80/Rector/Class_/AnnotationToAttributeRector.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Rector\Php80\Rector\Class_;
66

7+
use Nette\Utils\Strings;
78
use PhpParser\Node;
89
use PhpParser\Node\AttributeGroup;
910
use PhpParser\Node\Expr\ArrowFunction;
@@ -204,10 +205,20 @@ private function processGenericTags(PhpDocInfo $phpDocInfo): array
204205
continue;
205206
}
206207

208+
$docValue = null;
209+
if ($annotationToAttribute->getUseValueAsAttributeArgument()) {
210+
// special case for newline
211+
$docValue = (string) $docNode->value;
212+
if (str_contains($docValue, '\\')) {
213+
$docValue = Strings::replace($docValue, "#\\\\\n#", '');
214+
}
215+
}
216+
207217
$attributeGroups[] = $this->phpAttributeGroupFactory->createFromSimpleTag(
208218
$annotationToAttribute,
209-
$annotationToAttribute->getUseValueAsAttributeArgument() ? (string) $docNode->value : null
219+
$docValue
210220
);
221+
211222
return PhpDocNodeTraverser::NODE_REMOVE;
212223
}
213224

stubs/Behat/Step/Then.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Behat\Step;
4+
5+
if (class_exists('Behat\Step\Then')) {
6+
return;
7+
}
8+
9+
class Then
10+
{
11+
12+
}

stubs/Behat/Step/When.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Behat\Step;
4+
5+
if (class_exists('Behat\Step\When')) {
6+
return;
7+
}
8+
9+
class When
10+
{
11+
12+
}

0 commit comments

Comments
 (0)