Skip to content

Commit 5de5266

Browse files
authored
[docblock] Remove commment duplicating class name (#55)
1 parent a7fff18 commit 5de5266

File tree

5 files changed

+131
-3
lines changed

5 files changed

+131
-3
lines changed

src/DocBlock/UselessDocBlockCleaner.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@ final class UselessDocBlockCleaner
6060
*/
6161
private const COMMENT_CONSTRUCTOR_CLASS_REGEX = '#^(\/\/|(\s|\*)+)(\s\w+\s)?constructor(\.)?$#i';
6262

63-
public function clearDocTokenContent(Token $currentToken): string
63+
public function clearDocTokenContent(Token $currentToken, ?string $classLikeName): string
6464
{
6565
$docContent = $currentToken->getContent();
6666

6767
$cleanedCommentLines = [];
6868

6969
foreach (explode("\n", $docContent) as $key => $commentLine) {
70+
if ($this->isClassLikeName($commentLine, $classLikeName)) {
71+
continue;
72+
}
73+
7074
foreach (self::CLEANING_REGEXES as $cleaningRegex) {
7175
$commentLine = Strings::replace($commentLine, $cleaningRegex);
7276
}
@@ -100,4 +104,13 @@ private function isEmptyDocblock(array $commentLines): bool
100104

101105
return $startCommentLine === '/**' && trim($endCommentLine) === '*/';
102106
}
107+
108+
private function isClassLikeName(string $commentLine, ?string $classLikeName): bool
109+
{
110+
if ($classLikeName === null) {
111+
return false;
112+
}
113+
114+
return trim($commentLine, '* ') === $classLikeName;
115+
}
103116
}

src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use SplFileInfo;
1212
use Symplify\CodingStandard\DocBlock\UselessDocBlockCleaner;
1313
use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer;
14+
use Symplify\CodingStandard\Fixer\Naming\ClassNameResolver;
1415
use Symplify\CodingStandard\TokenRunner\Traverser\TokenReverser;
1516
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
1617
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -28,7 +29,8 @@ final class RemoveUselessDefaultCommentFixer extends AbstractSymplifyFixer imple
2829

2930
public function __construct(
3031
private readonly UselessDocBlockCleaner $uselessDocBlockCleaner,
31-
private readonly TokenReverser $tokenReverser
32+
private readonly TokenReverser $tokenReverser,
33+
private readonly ClassNameResolver $classNameResolver,
3234
) {
3335
}
3436

@@ -63,8 +65,10 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
6365
continue;
6466
}
6567

68+
$classLikeName = $this->classNameResolver->resolveClassName($fileInfo, $tokens);
69+
6670
$originalContent = $token->getContent();
67-
$cleanedDocContent = $this->uselessDocBlockCleaner->clearDocTokenContent($token);
71+
$cleanedDocContent = $this->uselessDocBlockCleaner->clearDocTokenContent($token, $classLikeName);
6872

6973
if ($cleanedDocContent === '') {
7074
// remove token
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Symplify\CodingStandard\Fixer\Naming;
4+
5+
use PhpCsFixer\Tokenizer\Token;
6+
use PhpCsFixer\Tokenizer\Tokens;
7+
use SplFileInfo;
8+
9+
final class ClassNameResolver
10+
{
11+
/**
12+
* @var array<string, string>
13+
*/
14+
private array $classNameByFilePath = [];
15+
16+
/**
17+
* @param Tokens<Token> $tokens
18+
*/
19+
public function resolveClassName(SplFileInfo $splFileInfo, Tokens $tokens): ?string
20+
{
21+
$filePath = $splFileInfo->getRealPath();
22+
23+
if (isset($this->classNameByFilePath[$filePath])) {
24+
return $this->classNameByFilePath[$filePath];
25+
}
26+
27+
$classLikeName = $this->resolveFromTokens($tokens);
28+
if (! is_string($classLikeName)) {
29+
return null;
30+
}
31+
32+
$this->classNameByFilePath[$filePath] = $classLikeName;
33+
34+
return $classLikeName;
35+
}
36+
37+
/**
38+
* @param Tokens<Token> $tokens
39+
*/
40+
private function resolveFromTokens(Tokens $tokens): ?string
41+
{
42+
foreach ($tokens as $position => $token) {
43+
if (! $token->isGivenKind([T_CLASS, T_TRAIT, T_INTERFACE])) {
44+
continue;
45+
}
46+
47+
$nextNextMeaningfulTokenIndex = $tokens->getNextMeaningfulToken($position + 1);
48+
$nextNextMeaningfulToken = $tokens[$nextNextMeaningfulTokenIndex];
49+
50+
// skip anonymous classes
51+
if (! $nextNextMeaningfulToken->isGivenKind(T_STRING)) {
52+
continue;
53+
}
54+
55+
return $nextNextMeaningfulToken->getContent();
56+
}
57+
58+
return null;
59+
}
60+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture;
4+
5+
/**
6+
* RemoveBareClassName
7+
*/
8+
class RemoveBareClassName
9+
{
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture;
17+
18+
19+
class RemoveBareClassName
20+
{
21+
}
22+
23+
?>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture;
4+
5+
/**
6+
* useful comment here
7+
*
8+
* ThisIsOnlyTrait
9+
*/
10+
trait ThisIsOnlyTrait
11+
{
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture;
19+
20+
/**
21+
* useful comment here
22+
*
23+
*/
24+
trait ThisIsOnlyTrait
25+
{
26+
}
27+
28+
?>

0 commit comments

Comments
 (0)