From eb8c151ac84a0e432d8524caa5d0f22287459e0a Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 23 Feb 2024 13:57:49 +0100 Subject: [PATCH] Fix end docblock (#42) --- src/DocBlock/UselessDocBlockCleaner.php | 15 +++- .../RemoveUselessDefaultCommentFixer.php | 1 + ...comment.php.inc => class_docblock.php.inc} | 30 ------- .../Fixture/remove_from_bottom_line.php.inc | 28 +++++++ .../Fixture/simple_inline_comment.php.inc | 31 +++++++ ..._1_extra_word_class_method_comment.php.inc | 17 ++-- .../Fixture/skip_inline_set.php.inc | 6 +- .../Fixture/skip_non_phpstorm_comment.php.inc | 6 -- .../skip_usefull_class_comment.php.inc | 2 +- ..._usefull_class_constructor_comment.php.inc | 11 ++- .../Fixture/todo_comment.php.inc | 9 ++- .../useless_class_constructor_comment.php.inc | 80 +++++-------------- .../config/configured_rule.php | 6 +- 13 files changed, 118 insertions(+), 124 deletions(-) rename tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/{useless_class_comment.php.inc => class_docblock.php.inc} (64%) create mode 100644 tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/remove_from_bottom_line.php.inc create mode 100644 tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/simple_inline_comment.php.inc delete mode 100644 tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_non_phpstorm_comment.php.inc diff --git a/src/DocBlock/UselessDocBlockCleaner.php b/src/DocBlock/UselessDocBlockCleaner.php index 3094cb1b0..d07279fd5 100644 --- a/src/DocBlock/UselessDocBlockCleaner.php +++ b/src/DocBlock/UselessDocBlockCleaner.php @@ -15,9 +15,14 @@ final class UselessDocBlockCleaner private const CLEANING_REGEXES = [ self::TODO_COMMENT_BY_PHPSTORM_REGEX, self::TODO_IMPLEMENT_METHOD_COMMENT_BY_PHPSTORM_REGEX, + self::COMMENT_CONSTRUCTOR_CLASS_REGEX, + + // must run first + self::STANDALONE_DOCBLOCK_CLASS_REGEX, + // then this one self::STANDALONE_COMMENT_CLASS_REGEX, + // then this one self::INLINE_COMMENT_CLASS_REGEX, - self::COMMENT_CONSTRUCTOR_CLASS_REGEX, ]; /** @@ -36,7 +41,13 @@ final class UselessDocBlockCleaner * @see https://regex101.com/r/RzTdFH/4 * @var string */ - private const STANDALONE_COMMENT_CLASS_REGEX = '#(\/\*{2}\s+?)?(\*|\/\/)\s+[cC]lass\s+[^\s]*(\s+\*\/)?$#'; + private const STANDALONE_DOCBLOCK_CLASS_REGEX = '#(\/\*\*\s+)\*\s+[cC]lass\s+[^\s]*(\s+\*\/)$#'; + + /** + * @see https://regex101.com/r/RzTdFH/4 + * @var string + */ + private const STANDALONE_COMMENT_CLASS_REGEX = '#\/\/\s+[cC]lass\s+\w+$#'; /** * @see https://regex101.com/r/RzTdFH/4 diff --git a/src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php b/src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php index 4f98a84c3..9a2e64f68 100644 --- a/src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php +++ b/src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php @@ -64,6 +64,7 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void } $cleanedDocContent = $this->uselessDocBlockCleaner->clearDocTokenContent($token); + if ($cleanedDocContent === '') { // remove token $tokens->clearTokenAndMergeSurroundingWhitespace($index); diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/useless_class_comment.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/class_docblock.php.inc similarity index 64% rename from tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/useless_class_comment.php.inc rename to tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/class_docblock.php.inc index 8679af82b..725ee08d9 100644 --- a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/useless_class_comment.php.inc +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/class_docblock.php.inc @@ -16,22 +16,6 @@ class SomeClass2 { } -/** - * class SomeClass - */ -class SomeClass3 -{ -} - -// class SomeClass -class SomeClass4 -{ -} - -// class SomeClass -class SomeClass5 -{ -} ?> ----- diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/remove_from_bottom_line.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/remove_from_bottom_line.php.inc new file mode 100644 index 000000000..fb1c84571 --- /dev/null +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/remove_from_bottom_line.php.inc @@ -0,0 +1,28 @@ + +----- + diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/simple_inline_comment.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/simple_inline_comment.php.inc new file mode 100644 index 000000000..652da9adf --- /dev/null +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/simple_inline_comment.php.inc @@ -0,0 +1,31 @@ + +----- + 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 d50d3e6b3..b49193ca0 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 @@ -2,15 +2,12 @@ namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture; -class SomeClass +final class Skip1ExtraWordClassMethodComment { -/** - * Get special translator - */ - public function getTranslator() - { - - } + /** + * Get special translator + */ + public function getTranslator() + { + } } - -?> diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_inline_set.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_inline_set.php.inc index eb4493351..703c1768c 100644 --- a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_inline_set.php.inc +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_inline_set.php.inc @@ -4,9 +4,9 @@ namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCom class SkipInlineSet { - public function getTranslator() - { + public function getTranslator() + { // set value $value = 1000; - } + } } diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_non_phpstorm_comment.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_non_phpstorm_comment.php.inc deleted file mode 100644 index 186788577..000000000 --- a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_non_phpstorm_comment.php.inc +++ /dev/null @@ -1,6 +0,0 @@ - diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_usefull_class_comment.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_usefull_class_comment.php.inc index 3fe36ef13..9b5517a3a 100644 --- a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_usefull_class_comment.php.inc +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_usefull_class_comment.php.inc @@ -5,6 +5,6 @@ namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCom /** * class to provide something */ -class SomeClass5 +class SkipUsefullClassComment { } diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_usefull_class_constructor_comment.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_usefull_class_constructor_comment.php.inc index c9d827f87..32c253c0a 100644 --- a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_usefull_class_constructor_comment.php.inc +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/skip_usefull_class_constructor_comment.php.inc @@ -2,13 +2,12 @@ namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture; -class SomeClass +class SkipUsefullClassConstructorComment { - /** + /** * A usefull comment. */ - public function __construct() - { - - } + public function __construct() + { + } } diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/todo_comment.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/todo_comment.php.inc index d2766b421..2f73050ac 100644 --- a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/todo_comment.php.inc +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/todo_comment.php.inc @@ -1,25 +1,28 @@ ----- diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/useless_class_constructor_comment.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/useless_class_constructor_comment.php.inc index e2cae63a3..0f6532276 100644 --- a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/useless_class_constructor_comment.php.inc +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/useless_class_constructor_comment.php.inc @@ -2,86 +2,42 @@ namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture; -class SomeClass +final class UselessClassConstructorComment { -/** - * SomeClass constructor. - */ - public function __construct() - { - - } -} - -class SomeClass -{ -/** - * SomeClass Constructor. - */ - public function __construct() - { - - } + /** + * SomeClass constructor. + */ + public function __construct() + { + } } class SomeClass { -/** - * SomeClass Constructor - */ - public function __construct() - { - - } + // SomeClass constructor + public function __construct() + { + } } -class SomeClass -{ -// SomeClass constructor - public function __construct() - { - - } -} ?> ----- diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/config/configured_rule.php b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/config/configured_rule.php index 3a61b1ca8..c08063c9d 100644 --- a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/config/configured_rule.php +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/config/configured_rule.php @@ -1,10 +1,14 @@ rule(RemoveUselessDefaultCommentFixer::class); + $ecsConfig->rules([ + RemoveUselessDefaultCommentFixer::class, + BracesFixer::class, + ]); };