Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
skip already added
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 9, 2021
1 parent 5387851 commit e005544
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
namespace Symplify\CodingStandard\Fixer\Annotation;

use Doctrine\Common\Annotations\DocLexer;
use PhpCsFixer\Doctrine\Annotation\Token;
use PhpCsFixer\Doctrine\Annotation\Token as DoctrineAnnotationToken;
use PhpCsFixer\Doctrine\Annotation\Tokens as DoctrineAnnotationTokens;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Analyzer\NamespaceUsesAnalyzer;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer;
use Symplify\CodingStandard\TokenAnalyzer\DoctrineAnnotationElementAnalyzer;
Expand Down Expand Up @@ -101,7 +102,7 @@ public function fix(\SplFileInfo $fileInfo, Tokens $tokens): void

// fetch indexes one time, this is safe as we never add or remove a token during fixing

/** @var \PhpCsFixer\Tokenizer\Token[] $docCommentTokens */
/** @var Token[] $docCommentTokens */
$docCommentTokens = $tokens->findGivenKind(T_DOC_COMMENT);
foreach ($docCommentTokens as $index => $docCommentToken) {
if (! $this->doctrineAnnotationElementAnalyzer->detect($tokens, $index)) {
Expand All @@ -111,12 +112,12 @@ public function fix(\SplFileInfo $fileInfo, Tokens $tokens): void
$doctrineAnnotationTokens = DoctrineAnnotationTokens::createFromDocComment($docCommentToken, []);
$this->fixAnnotations($doctrineAnnotationTokens, $useDeclarations);

$tokens[$index] = new \PhpCsFixer\Tokenizer\Token([T_DOC_COMMENT, $doctrineAnnotationTokens->getCode()]);
$tokens[$index] = new Token([T_DOC_COMMENT, $doctrineAnnotationTokens->getCode()]);
}
}

/**
* @param DoctrineAnnotationTokens<Token> $tokens
* @param DoctrineAnnotationTokens<DoctrineAnnotationToken> $tokens
*/
private function fixAnnotations(DoctrineAnnotationTokens $tokens, $useDeclarations): void
{
Expand Down Expand Up @@ -145,7 +146,7 @@ private function fixAnnotations(DoctrineAnnotationTokens $tokens, $useDeclaratio
continue;
}

/** @var Token $braceToken */
/** @var DoctrineAnnotationToken $braceToken */
$braceToken = $tokens[$braceIndex];
if (! $this->doctrineAnnotationElementAnalyzer->isOpeningBracketFollowedByAnnotation(
$braceToken,
Expand All @@ -156,10 +157,10 @@ private function fixAnnotations(DoctrineAnnotationTokens $tokens, $useDeclaratio
}

// add closing brace
$tokens->insertAt($closingBraceIndex, new Token(DocLexer::T_OPEN_CURLY_BRACES, '}'));
$tokens->insertAt($closingBraceIndex, new DoctrineAnnotationToken(DocLexer::T_OPEN_CURLY_BRACES, '}'));

// add opening brace
$tokens->insertAt($braceIndex + 1, new Token(DocLexer::T_OPEN_CURLY_BRACES, '{'));
$tokens->insertAt($braceIndex + 1, new DoctrineAnnotationToken(DocLexer::T_OPEN_CURLY_BRACES, '{'));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function detect(Tokens $tokens, int $index): bool
/**
* We look for "(@SomeAnnotation"
*
* @param DoctrineAnnotationTokens<Token> $tokens
* @param DoctrineAnnotationTokens<DoctrineAnnotationToken> $tokens
*/
public function isOpeningBracketFollowedByAnnotation(
Token $token,
Expand All @@ -62,7 +62,7 @@ public function isOpeningBracketFollowedByAnnotation(
return false;
}

$nextTokenIndex = $tokens->getNextMeaningfulToken($braceIndex + 1);
$nextTokenIndex = $tokens->getNextMeaningfulToken($braceIndex);
if ($nextTokenIndex === null) {
return false;
}
Expand Down

0 comments on commit e005544

Please sign in to comment.