Skip to content

Commit

Permalink
Add empty constructor coverage to UselessDocBlockCleaner (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Aug 1, 2024
1 parent 29dd54b commit a7fff18
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 10 deletions.
41 changes: 35 additions & 6 deletions src/DocBlock/UselessDocBlockCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) === '*/';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ interface SomeInterface

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

/**
*/

interface SomeInterface
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ trait SomeTrait

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

/**
*/

trait SomeTrait
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

final class ConstructorBlank
{
/**
* Constructor.
*/
public function __construct()
{
}
}

?>
-----
<?php

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

final class ConstructorBlank
{
public function __construct()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

final class SkipConstructorAsPartOfSentence
{
/**
* Required private constructor.
*/
private function __construct()
{
}
}

0 comments on commit a7fff18

Please sign in to comment.