From 5d2299635cd090716ddfca0c365d066b9597d85c Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 1 Aug 2024 09:14:27 +0200 Subject: [PATCH] Add empty constructor coverage to UselessDocBlockCleaner --- src/DocBlock/UselessDocBlockCleaner.php | 41 ++++++++++++++++--- .../StandaloneLineConstructorParamFixer.php | 2 +- .../interface_docblock.php.inc | 3 +- .../trait_docblock.php.inc | 3 +- .../constructor/constructor_blank.php.inc | 28 +++++++++++++ ...ip_constructor_as_part_of_sentence.php.inc | 13 ++++++ .../useless_class_constructor_comment.php.inc | 0 7 files changed, 79 insertions(+), 11 deletions(-) rename tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/{ => class_interface_trait}/interface_docblock.php.inc (97%) rename tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/{ => class_interface_trait}/trait_docblock.php.inc (97%) create mode 100644 tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/constructor/constructor_blank.php.inc create mode 100644 tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/constructor/skip_constructor_as_part_of_sentence.php.inc rename tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/{ => constructor}/useless_class_constructor_comment.php.inc (100%) 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/src/Fixer/Spacing/StandaloneLineConstructorParamFixer.php b/src/Fixer/Spacing/StandaloneLineConstructorParamFixer.php index d902bb61..c1687b45 100644 --- a/src/Fixer/Spacing/StandaloneLineConstructorParamFixer.php +++ b/src/Fixer/Spacing/StandaloneLineConstructorParamFixer.php @@ -17,7 +17,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Symplify\CodingStandard\Tests\Fixer\Spacing\StandaloneLineConstructorParamFixer\StandaloneLineConstructorParamFixerTest + * @see \Symplify\CodingStandard\Tests\Fixer\Spacing\StandaloneLinearamFixer\StandaloneLinearamFixerTest */ final class StandaloneLineConstructorParamFixer extends AbstractSymplifyFixer implements DocumentedRuleInterface { 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 @@ +