Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Attributes] Cover slash newline in AnnotationToAttributeRector with values #6557

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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#", '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"\r?\n" should be used to support windows newline

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

$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
{

}
Loading