Skip to content

Commit

Permalink
[docblock] Remove commment duplicating class name (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Aug 8, 2024
1 parent a7fff18 commit 5de5266
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/DocBlock/UselessDocBlockCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ final class UselessDocBlockCleaner
*/
private const COMMENT_CONSTRUCTOR_CLASS_REGEX = '#^(\/\/|(\s|\*)+)(\s\w+\s)?constructor(\.)?$#i';

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

$cleanedCommentLines = [];

foreach (explode("\n", $docContent) as $key => $commentLine) {
if ($this->isClassLikeName($commentLine, $classLikeName)) {
continue;
}

foreach (self::CLEANING_REGEXES as $cleaningRegex) {
$commentLine = Strings::replace($commentLine, $cleaningRegex);
}
Expand Down Expand Up @@ -100,4 +104,13 @@ private function isEmptyDocblock(array $commentLines): bool

return $startCommentLine === '/**' && trim($endCommentLine) === '*/';
}

private function isClassLikeName(string $commentLine, ?string $classLikeName): bool
{
if ($classLikeName === null) {
return false;
}

return trim($commentLine, '* ') === $classLikeName;
}
}
8 changes: 6 additions & 2 deletions src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SplFileInfo;
use Symplify\CodingStandard\DocBlock\UselessDocBlockCleaner;
use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer;
use Symplify\CodingStandard\Fixer\Naming\ClassNameResolver;
use Symplify\CodingStandard\TokenRunner\Traverser\TokenReverser;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -28,7 +29,8 @@ final class RemoveUselessDefaultCommentFixer extends AbstractSymplifyFixer imple

public function __construct(
private readonly UselessDocBlockCleaner $uselessDocBlockCleaner,
private readonly TokenReverser $tokenReverser
private readonly TokenReverser $tokenReverser,
private readonly ClassNameResolver $classNameResolver,
) {
}

Expand Down Expand Up @@ -63,8 +65,10 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
continue;
}

$classLikeName = $this->classNameResolver->resolveClassName($fileInfo, $tokens);

$originalContent = $token->getContent();
$cleanedDocContent = $this->uselessDocBlockCleaner->clearDocTokenContent($token);
$cleanedDocContent = $this->uselessDocBlockCleaner->clearDocTokenContent($token, $classLikeName);

if ($cleanedDocContent === '') {
// remove token
Expand Down
60 changes: 60 additions & 0 deletions src/Fixer/Naming/ClassNameResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Symplify\CodingStandard\Fixer\Naming;

use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;

final class ClassNameResolver
{
/**
* @var array<string, string>
*/
private array $classNameByFilePath = [];

/**
* @param Tokens<Token> $tokens
*/
public function resolveClassName(SplFileInfo $splFileInfo, Tokens $tokens): ?string
{
$filePath = $splFileInfo->getRealPath();

if (isset($this->classNameByFilePath[$filePath])) {
return $this->classNameByFilePath[$filePath];
}

$classLikeName = $this->resolveFromTokens($tokens);
if (! is_string($classLikeName)) {
return null;
}

$this->classNameByFilePath[$filePath] = $classLikeName;

return $classLikeName;
}

/**
* @param Tokens<Token> $tokens
*/
private function resolveFromTokens(Tokens $tokens): ?string
{
foreach ($tokens as $position => $token) {
if (! $token->isGivenKind([T_CLASS, T_TRAIT, T_INTERFACE])) {
continue;
}

$nextNextMeaningfulTokenIndex = $tokens->getNextMeaningfulToken($position + 1);
$nextNextMeaningfulToken = $tokens[$nextNextMeaningfulTokenIndex];

// skip anonymous classes
if (! $nextNextMeaningfulToken->isGivenKind(T_STRING)) {
continue;
}

return $nextNextMeaningfulToken->getContent();
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture;

/**
* RemoveBareClassName
*/
class RemoveBareClassName
{
}

?>
-----
<?php

namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture;


class RemoveBareClassName
{
}

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

namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture;

/**
* useful comment here
*
* ThisIsOnlyTrait
*/
trait ThisIsOnlyTrait
{
}

?>
-----
<?php

namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture;

/**
* useful comment here
*
*/
trait ThisIsOnlyTrait
{
}

?>

0 comments on commit 5de5266

Please sign in to comment.