Skip to content

Commit

Permalink
[Php80] Handle value as argument with keep description as comment on …
Browse files Browse the repository at this point in the history
…AttributeValueResolver (#6589)

* [Php80] Handle behat @when with values as argument and description as comment

* debugging

* handle comment as value

* [Php80] Handle value as argument with keep description as comment on AttributeValueResolver

* new lines

* new lines

* deetail
  • Loading branch information
samsonasik authored Dec 14, 2024
1 parent 171afe1 commit 832e458
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

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

final class WithValueAsArgumentAndDescription
{
/**
* original comment
* some other new lines
*
* @When(key="value") some description
*
* other comment
* other new line comment
*/
public function someStep(): void
{
}
}

?>
-----
<?php

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

final class WithValueAsArgumentAndDescription
{
/**
* original comment
* some other new lines
*
* some description
*
* other comment
* other new line comment
*/
#[\Behat\Step\When('(key="value")')]
public function someStep(): void
{
}
}

?>
20 changes: 17 additions & 3 deletions rules/Php80/Rector/Class_/AttributeValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nette\Utils\Strings;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Rector\Php80\ValueObject\AttributeValueAndDocComment;
use Rector\Util\NewLineSplitter;
Expand All @@ -27,10 +28,23 @@ public function resolve(
return null;
}

$docValue = (string) $phpDocTagNode->value;

if ($phpDocTagNode->value instanceof DoctrineAnnotationTagValueNode) {
$docValue = (string) $phpDocTagNode->value->getOriginalContent();
} else {
$docValue = (string) $phpDocTagNode->value;
$originalContent = (string) $phpDocTagNode->value->getOriginalContent();

if ($docValue === '') {
$attributeComment = (string) $phpDocTagNode->value->getAttribute(AttributeKey::ATTRIBUTE_COMMENT);

if ($originalContent === $attributeComment) {
$docValue = $originalContent;
}
} else {
$attributeComment = ltrim($originalContent, $docValue);
if ($attributeComment !== '') {
$docValue .= "\n" . $attributeComment;
}
}
}

$docComment = '';
Expand Down

0 comments on commit 832e458

Please sign in to comment.