diff --git a/src/DocBlock/UselessDocBlockCleaner.php b/src/DocBlock/UselessDocBlockCleaner.php index 08ab6e92..9700daa9 100644 --- a/src/DocBlock/UselessDocBlockCleaner.php +++ b/src/DocBlock/UselessDocBlockCleaner.php @@ -53,22 +53,51 @@ final class UselessDocBlockCleaner * @see https://regex101.com/r/RzTdFH/4 * @var string */ - private const INLINE_COMMENT_CLASS_REGEX = '#( \*|\/\/)\s+(class|trait|interface)\s+(\w+)\n#i'; + private const INLINE_COMMENT_CLASS_REGEX = '#\s\*\s(class|trait|interface)\s+(\w)+$#i'; /** - * @see https://regex101.com/r/bzbxXz/2 * @var string */ - private const COMMENT_CONSTRUCTOR_CLASS_REGEX = '#^\s{0,}(\/\*{2}\s+?)?(\*|\/\/)\s+[^\s]*\s+[Cc]onstructor\.?(\s+\*\/)?$#'; + private const COMMENT_CONSTRUCTOR_CLASS_REGEX = '#^(\/\/|(\s|\*)+)(\s\w+\s)?constructor(\.)?$#i'; public function clearDocTokenContent(Token $currentToken): string { $docContent = $currentToken->getContent(); - foreach (self::CLEANING_REGEXES as $cleaningRegex) { - $docContent = Strings::replace($docContent, $cleaningRegex); + $cleanedCommentLines = []; + + foreach (explode("\n", $docContent) as $key => $commentLine) { + foreach (self::CLEANING_REGEXES as $cleaningRegex) { + $commentLine = Strings::replace($commentLine, $cleaningRegex); + } + + $cleanedCommentLines[$key] = $commentLine; } - return $docContent; + // remove empty lines + $cleanedCommentLines = array_filter($cleanedCommentLines); + $cleanedCommentLines = array_values($cleanedCommentLines); + + // is totally empty? + if ($this->isEmptyDocblock($cleanedCommentLines)) { + return ''; + } + + return implode("\n", $cleanedCommentLines); + } + + /** + * @param string[] $commentLines + */ + private function isEmptyDocblock(array $commentLines): bool + { + if (count($commentLines) !== 2) { + return false; + } + + $startCommentLine = $commentLines[0]; + $endCommentLine = $commentLines[1]; + + return $startCommentLine === '/**' && trim($endCommentLine) === '*/'; } } diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/interface_docblock.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/class_interface_trait/interface_docblock.php.inc similarity index 97% rename from tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/interface_docblock.php.inc rename to tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/class_interface_trait/interface_docblock.php.inc index 09c9a3cc..b0c9cc4c 100644 --- a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/interface_docblock.php.inc +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/class_interface_trait/interface_docblock.php.inc @@ -15,8 +15,7 @@ interface SomeInterface namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture; -/** - */ + interface SomeInterface { } diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/trait_docblock.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/class_interface_trait/trait_docblock.php.inc similarity index 97% rename from tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/trait_docblock.php.inc rename to tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/class_interface_trait/trait_docblock.php.inc index 1eff079c..5d3dfb1d 100644 --- a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/trait_docblock.php.inc +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/class_interface_trait/trait_docblock.php.inc @@ -15,8 +15,7 @@ trait SomeTrait namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture; -/** - */ + trait SomeTrait { } diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/constructor/constructor_blank.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/constructor/constructor_blank.php.inc new file mode 100644 index 00000000..8105df13 --- /dev/null +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/constructor/constructor_blank.php.inc @@ -0,0 +1,28 @@ + +----- + diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/constructor/skip_constructor_as_part_of_sentence.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/constructor/skip_constructor_as_part_of_sentence.php.inc new file mode 100644 index 00000000..bd8bb9b3 --- /dev/null +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/constructor/skip_constructor_as_part_of_sentence.php.inc @@ -0,0 +1,13 @@ +