Skip to content

Commit a7fff18

Browse files
authored
Add empty constructor coverage to UselessDocBlockCleaner (#54)
1 parent 29dd54b commit a7fff18

File tree

6 files changed

+78
-10
lines changed

6 files changed

+78
-10
lines changed

src/DocBlock/UselessDocBlockCleaner.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,51 @@ final class UselessDocBlockCleaner
5353
* @see https://regex101.com/r/RzTdFH/4
5454
* @var string
5555
*/
56-
private const INLINE_COMMENT_CLASS_REGEX = '#( \*|\/\/)\s+(class|trait|interface)\s+(\w+)\n#i';
56+
private const INLINE_COMMENT_CLASS_REGEX = '#\s\*\s(class|trait|interface)\s+(\w)+$#i';
5757

5858
/**
59-
* @see https://regex101.com/r/bzbxXz/2
6059
* @var string
6160
*/
62-
private const COMMENT_CONSTRUCTOR_CLASS_REGEX = '#^\s{0,}(\/\*{2}\s+?)?(\*|\/\/)\s+[^\s]*\s+[Cc]onstructor\.?(\s+\*\/)?$#';
61+
private const COMMENT_CONSTRUCTOR_CLASS_REGEX = '#^(\/\/|(\s|\*)+)(\s\w+\s)?constructor(\.)?$#i';
6362

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

68-
foreach (self::CLEANING_REGEXES as $cleaningRegex) {
69-
$docContent = Strings::replace($docContent, $cleaningRegex);
67+
$cleanedCommentLines = [];
68+
69+
foreach (explode("\n", $docContent) as $key => $commentLine) {
70+
foreach (self::CLEANING_REGEXES as $cleaningRegex) {
71+
$commentLine = Strings::replace($commentLine, $cleaningRegex);
72+
}
73+
74+
$cleanedCommentLines[$key] = $commentLine;
7075
}
7176

72-
return $docContent;
77+
// remove empty lines
78+
$cleanedCommentLines = array_filter($cleanedCommentLines);
79+
$cleanedCommentLines = array_values($cleanedCommentLines);
80+
81+
// is totally empty?
82+
if ($this->isEmptyDocblock($cleanedCommentLines)) {
83+
return '';
84+
}
85+
86+
return implode("\n", $cleanedCommentLines);
87+
}
88+
89+
/**
90+
* @param string[] $commentLines
91+
*/
92+
private function isEmptyDocblock(array $commentLines): bool
93+
{
94+
if (count($commentLines) !== 2) {
95+
return false;
96+
}
97+
98+
$startCommentLine = $commentLines[0];
99+
$endCommentLine = $commentLines[1];
100+
101+
return $startCommentLine === '/**' && trim($endCommentLine) === '*/';
73102
}
74103
}

tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/interface_docblock.php.inc renamed to tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/class_interface_trait/interface_docblock.php.inc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ interface SomeInterface
1515

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

18-
/**
19-
*/
18+
2019
interface SomeInterface
2120
{
2221
}

tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/trait_docblock.php.inc renamed to tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/class_interface_trait/trait_docblock.php.inc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ trait SomeTrait
1515

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

18-
/**
19-
*/
18+
2019
trait SomeTrait
2120
{
2221
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture;
4+
5+
final class ConstructorBlank
6+
{
7+
/**
8+
* Constructor.
9+
*/
10+
public function __construct()
11+
{
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture;
20+
21+
final class ConstructorBlank
22+
{
23+
public function __construct()
24+
{
25+
}
26+
}
27+
28+
?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture;
4+
5+
final class SkipConstructorAsPartOfSentence
6+
{
7+
/**
8+
* Required private constructor.
9+
*/
10+
private function __construct()
11+
{
12+
}
13+
}

0 commit comments

Comments
 (0)