Skip to content

Commit

Permalink
[DeadCode] Handle duplicated annotation on space before close parenth…
Browse files Browse the repository at this point in the history
…eses on RemoveUselessParamTagRector (#4795)

* Create do_not_duplicate_annotations.php.inc

rectorphp/rector#8101

* Fixes #4641 Closes rectorphp/rector#8101

* Fixed 🎉

* fix

* ensure bracket compared

* cs fix

* cs fix

---------

Co-authored-by: mkrauss <m.d.krauss@gmail.com>
  • Loading branch information
samsonasik and mkrauss committed Aug 15, 2023
1 parent d8d4e8d commit 83fdfb3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ private function mergeNestedDoctrineAnnotations(PhpDocNode $phpDocNode): void
continue;
}

if ($this->isClosedContent($genericTagValueNode->value)) {
$isNewLinedGenericTagValueNode = str_starts_with($genericTagValueNode->value, '(')
&& ! str_ends_with($genericTagValueNode->value, ')');
if ($this->isClosedContent($genericTagValueNode->value, $isNewLinedGenericTagValueNode)) {
break;
}

Expand Down Expand Up @@ -204,7 +206,7 @@ private function transformGenericTagValueNodesToDoctrineAnnotationTagValueNodes(
* This is closed block, e.g. {( ... )},
* false on: {( ... )
*/
private function isClosedContent(string $composedContent): bool
private function isClosedContent(string $composedContent, bool $isNewLined): bool
{
$composedTokenIterator = $this->tokenIteratorFactory->create($composedContent);
$tokenCount = $composedTokenIterator->count();
Expand All @@ -216,13 +218,6 @@ private function isClosedContent(string $composedContent): bool
}

do {
if ($composedTokenIterator->isCurrentTokenType(
Lexer::TOKEN_OPEN_CURLY_BRACKET,
Lexer::TOKEN_OPEN_PARENTHESES
) || \str_contains($composedTokenIterator->currentTokenValue(), '(')) {
++$openBracketCount;
}

if (
$composedTokenIterator->isCurrentTokenType(
Lexer::TOKEN_CLOSE_CURLY_BRACKET,
Expand All @@ -232,6 +227,24 @@ private function isClosedContent(string $composedContent): bool
++$closeBracketCount;
}

if ($composedTokenIterator->isCurrentTokenType(
Lexer::TOKEN_OPEN_CURLY_BRACKET,
Lexer::TOKEN_OPEN_PARENTHESES
) || \str_contains($composedTokenIterator->currentTokenValue(), '(')) {
++$openBracketCount;
}

if ($composedTokenIterator->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL)
&& $composedTokenIterator->getContentBetween(
$composedTokenIterator->currentPosition() - 1,
$composedTokenIterator->currentPosition()
) === '('
&& $isNewLined
&& $openBracketCount > $closeBracketCount
) {
--$openBracketCount;
}

$composedTokenIterator->next();
} while ($composedTokenIterator->currentPosition() < ($tokenCount - 1));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector\Fixture;

final class DoNotDuplicateAnnotations
{
/**
* @ApiDoc(
* description = "Some Description"
*
* )
*
* @Route("/some/route")
*
* @param string $thing
*/
public function foo(string $thing)
{
echo $thing;
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector\Fixture;

final class DoNotDuplicateAnnotations
{
/**
* @ApiDoc(
* description = "Some Description"
*
* )
*
* @Route("/some/route")
*/
public function foo(string $thing)
{
echo $thing;
}
}

?>

0 comments on commit 83fdfb3

Please sign in to comment.