From 4cf298c6194a92b07f9e9c7f19e94304cf100acf Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 23 Feb 2024 09:33:23 +0100 Subject: [PATCH] Remove useless class comment combined with other docblock --- src/DocBlock/UselessDocBlockCleaner.php | 18 +++++++----- .../RemoveUselessDefaultCommentFixer.php | 10 +++---- .../Fixture/remove_from_property.php.inc | 28 +++++++++++++++++++ ..._1_extra_word_class_method_comment.php.inc | 2 +- .../Fixture/skip_inline_set.php.inc | 2 +- .../skip_usefull_class_comment.php.inc | 4 +-- ..._usefull_class_constructor_comment.php.inc | 4 +-- .../useless_class_constructor_comment.php.inc | 4 +-- 8 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/remove_from_property.php.inc diff --git a/src/DocBlock/UselessDocBlockCleaner.php b/src/DocBlock/UselessDocBlockCleaner.php index 70689e813..3094cb1b0 100644 --- a/src/DocBlock/UselessDocBlockCleaner.php +++ b/src/DocBlock/UselessDocBlockCleaner.php @@ -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, ]; @@ -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; diff --git a/src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php b/src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php index 877e371c9..4f98a84c3 100644 --- a/src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php +++ b/src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php @@ -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]); } } } diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/remove_from_property.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/remove_from_property.php.inc new file mode 100644 index 000000000..bfd0d0d04 --- /dev/null +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/remove_from_property.php.inc @@ -0,0 +1,28 @@ + +----- + diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_1_extra_word_class_method_comment.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_1_extra_word_class_method_comment.php.inc index 58078306b..d50d3e6b3 100644 --- a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_1_extra_word_class_method_comment.php.inc +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_1_extra_word_class_method_comment.php.inc @@ -1,6 +1,6 @@