Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docblock] Remove commment duplicating class name #55

Merged
merged 1 commit into from
Aug 8, 2024
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
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
{
}

?>
Loading