Skip to content

Commit

Permalink
Remove useless class comment combined with other docblock (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Feb 23, 2024
1 parent 3f38152 commit 94d3f18
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 21 deletions.
18 changes: 11 additions & 7 deletions src/DocBlock/UselessDocBlockCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ final class UselessDocBlockCleaner
private const CLEANING_REGEXES = [
self::TODO_COMMENT_BY_PHPSTORM_REGEX,
self::TODO_IMPLEMENT_METHOD_COMMENT_BY_PHPSTORM_REGEX,
self::COMMENT_CLASS_REGEX,
self::STANDALONE_COMMENT_CLASS_REGEX,
self::INLINE_COMMENT_CLASS_REGEX,
self::COMMENT_CONSTRUCTOR_CLASS_REGEX,
];

Expand All @@ -35,23 +36,26 @@ final class UselessDocBlockCleaner
* @see https://regex101.com/r/RzTdFH/4
* @var string
*/
private const COMMENT_CLASS_REGEX = '#(\/\*{2}\s+?)?(\*|\/\/)\s+[cC]lass\s+[^\s]*(\s+\*\/)?$#';
private const STANDALONE_COMMENT_CLASS_REGEX = '#(\/\*{2}\s+?)?(\*|\/\/)\s+[cC]lass\s+[^\s]*(\s+\*\/)?$#';

/**
* @see https://regex101.com/r/bzbxXz/2
* @see https://regex101.com/r/RzTdFH/4
* @var string
*/
private const COMMENT_CONSTRUCTOR_CLASS_REGEX = '#^\s{0,}(\/\*{2}\s+?)?(\*|\/\/)\s+[^\s]*\s+[Cc]onstructor\.?(\s+\*\/)?$#';
private const INLINE_COMMENT_CLASS_REGEX = '#( \*|\/\/)\s+[cC]lass\s+(\w+)\n#';

/**
* @param Token[] $tokens
* @see https://regex101.com/r/bzbxXz/2
* @var string
*/
public function clearDocTokenContent(array $tokens, int $position, Token $currentToken): string
private const COMMENT_CONSTRUCTOR_CLASS_REGEX = '#^\s{0,}(\/\*{2}\s+?)?(\*|\/\/)\s+[^\s]*\s+[Cc]onstructor\.?(\s+\*\/)?$#';

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

foreach (self::CLEANING_REGEXES as $cleaningRegex) {
$docContent = Strings::replace($docContent, $cleaningRegex, '');
$docContent = Strings::replace($docContent, $cleaningRegex);
}

return $docContent;
Expand Down
10 changes: 4 additions & 6 deletions src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,13 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
continue;
}

$cleanedDocContent = $this->uselessDocBlockCleaner->clearDocTokenContent(
$reversedTokens,
$index,
$token
);

$cleanedDocContent = $this->uselessDocBlockCleaner->clearDocTokenContent($token);
if ($cleanedDocContent === '') {
// remove token
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
} else {
// update in case of other contents
$tokens[$index] = new Token([T_DOC_COMMENT, $cleanedDocContent]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

/**
* Class RemoveFromPropertyTest
* @property SomeItem $item
*/
#[\AllowDynamicProperties]
final class RemoveFromPropertyTest
{
}

?>
-----
<?php

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

/**
* @property SomeItem $item
*/
#[\AllowDynamicProperties]
final class RemoveFromPropertyTest
{
}

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

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

class SomeClass
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

class SkipInlineSet
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

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

/**
* class to provide something
*/
class SomeClass5
{
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

class SomeClass
{
Expand All @@ -11,4 +11,4 @@ class SomeClass
{

}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

class SomeClass
{
Expand Down Expand Up @@ -47,7 +47,7 @@ class SomeClass
-----
<?php

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

class SomeClass
{
Expand Down

0 comments on commit 94d3f18

Please sign in to comment.