Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove useless class comment combined with other docblock #41

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/DocBlock/UselessDocBlockCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ final class UselessDocBlockCleaner
private const CLEANING_REGEXES = [
self::TODO_COMMENT_BY_PHPSTORM_REGEX,
self::TODO_IMPLEMENT_METHOD_COMMENT_BY_PHPSTORM_REGEX,
self::COMMENT_CLASS_REGEX,
self::STANDALONE_COMMENT_CLASS_REGEX,
self::INLINE_COMMENT_CLASS_REGEX,
self::COMMENT_CONSTRUCTOR_CLASS_REGEX,
];

Expand All @@ -35,23 +36,26 @@ final class UselessDocBlockCleaner
* @see https://regex101.com/r/RzTdFH/4
* @var string
*/
private const COMMENT_CLASS_REGEX = '#(\/\*{2}\s+?)?(\*|\/\/)\s+[cC]lass\s+[^\s]*(\s+\*\/)?$#';
private const STANDALONE_COMMENT_CLASS_REGEX = '#(\/\*{2}\s+?)?(\*|\/\/)\s+[cC]lass\s+[^\s]*(\s+\*\/)?$#';

/**
* @see https://regex101.com/r/bzbxXz/2
* @see https://regex101.com/r/RzTdFH/4
* @var string
*/
private const COMMENT_CONSTRUCTOR_CLASS_REGEX = '#^\s{0,}(\/\*{2}\s+?)?(\*|\/\/)\s+[^\s]*\s+[Cc]onstructor\.?(\s+\*\/)?$#';
private const INLINE_COMMENT_CLASS_REGEX = '#( \*|\/\/)\s+[cC]lass\s+(\w+)\n#';

/**
* @param Token[] $tokens
* @see https://regex101.com/r/bzbxXz/2
* @var string
*/
public function clearDocTokenContent(array $tokens, int $position, Token $currentToken): string
private const COMMENT_CONSTRUCTOR_CLASS_REGEX = '#^\s{0,}(\/\*{2}\s+?)?(\*|\/\/)\s+[^\s]*\s+[Cc]onstructor\.?(\s+\*\/)?$#';

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

foreach (self::CLEANING_REGEXES as $cleaningRegex) {
$docContent = Strings::replace($docContent, $cleaningRegex, '');
$docContent = Strings::replace($docContent, $cleaningRegex);
}

return $docContent;
Expand Down
10 changes: 4 additions & 6 deletions src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,13 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
continue;
}

$cleanedDocContent = $this->uselessDocBlockCleaner->clearDocTokenContent(
$reversedTokens,
$index,
$token
);

$cleanedDocContent = $this->uselessDocBlockCleaner->clearDocTokenContent($token);
if ($cleanedDocContent === '') {
// remove token
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
} else {
// update in case of other contents
$tokens[$index] = new Token([T_DOC_COMMENT, $cleanedDocContent]);
}
}
}
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;

/**
* Class RemoveFromPropertyTest
* @property SomeItem $item
*/
#[\AllowDynamicProperties]
final class RemoveFromPropertyTest
{
}

?>
-----
<?php

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

/**
* @property SomeItem $item
*/
#[\AllowDynamicProperties]
final class RemoveFromPropertyTest
{
}

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

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

class SomeClass
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

class SkipInlineSet
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

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

/**
* class to provide something
*/
class SomeClass5
{
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

class SomeClass
{
Expand All @@ -11,4 +11,4 @@ class SomeClass
{

}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

class SomeClass
{
Expand Down Expand Up @@ -47,7 +47,7 @@ class SomeClass
-----
<?php

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

class SomeClass
{
Expand Down
Loading