Skip to content
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,32 @@
<?php

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

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

?>
-----
<?php

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

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

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

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

final class BackslashWitNewlineWithoutAnotherTextWithOtherComment
{
// some comment
/**
* @Then then :value should \
*
*
*
*/
// some comment 2
public function someStep(): void
{
}
}

?>
-----
<?php

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

final class BackslashWitNewlineWithoutAnotherTextWithOtherComment
{
// some comment
// some comment 2
#[\Behat\Step\Then('then :value should \\')]
public function someStep(): void
{
}
}

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

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

final class BackslashWitNewlineWithoutAnotherTextWithOtherComment2
{
// some comment
/**
* @Then then :value should \
*
*
*
*/
public function someStep(): void
{
}
}

?>
-----
<?php

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

final class BackslashWitNewlineWithoutAnotherTextWithOtherComment2
{
// some comment
#[\Behat\Step\Then('then :value should \\')]
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 BackslashWithoutNewline
{
/**
* @Then then :value should \
*/
public function someStep(): void
{
}
}

?>
-----
<?php

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

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

?>
22 changes: 22 additions & 0 deletions rules/Php80/Rector/Class_/AnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Exception\Configuration\InvalidConfigurationException;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Php80\NodeFactory\AttrGroupsFactory;
use Rector\Php80\NodeManipulator\AttributeGroupNamedArgumentManipulator;
Expand Down Expand Up @@ -152,12 +153,33 @@ public function refactor(Node $node): ?Node
// 3. Reprint docblock
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

// 4. Left over comment removal
$this->cleanLeftOverComment($node);

$this->attributeGroupNamedArgumentManipulator->decorate($attributeGroups);
$node->attrGroups = array_merge($node->attrGroups, $attributeGroups);

return $node;
}

private function cleanLeftOverComment(Node $node): void
{
$comments = $node->getComments();

if ($comments === []) {
return;
}

foreach ($comments as $key => $comment) {
if ($comment->getText() === '') {
unset($comments[$key]);
continue;
}
}

$node->setAttribute(AttributeKey::COMMENTS, array_values($comments));
}

/**
* @param mixed[] $configuration
*/
Expand Down
7 changes: 4 additions & 3 deletions rules/Php80/Rector/Class_/AttributeValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Rector\Php80\ValueObject\AttributeValueAndDocComment;
use Rector\Util\NewLineSplitter;

final class AttributeValueResolver
{
/**
* @var string
* @see https://regex101.com/r/CL9ktz/1
* @see https://regex101.com/r/CL9ktz/4
*/
private const END_SLASH_REGEX = '#\\\\\r?$#';
private const END_SLASH_REGEX = '#\\\\$#';

public function resolve(
AnnotationToAttribute $annotationToAttribute,
Expand All @@ -31,7 +32,7 @@ public function resolve(
// special case for newline
if (str_contains($docValue, "\n")) {
$keepJoining = true;
$docValueLines = explode("\n", $docValue);
$docValueLines = NewLineSplitter::split($docValue);

$joinDocValue = '';

Expand Down
Loading