Skip to content

Commit

Permalink
Merge pull request #250 from magento-commerce/imported-fredden-magent…
Browse files Browse the repository at this point in the history
…o-coding-standard-433

[Imported] Automatically remove useless comments
  • Loading branch information
sidolov authored Sep 26, 2023
2 parents a9d34af + 252371e commit d1711ba
Show file tree
Hide file tree
Showing 6 changed files with 396 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,29 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];

if ($this->PHPDocFormattingValidator->providesMeaning($namePtr, $commentStartPtr, $tokens) !== true) {
$phpcsFile->addWarning(
$fix = $phpcsFile->addFixableWarning(
sprintf(
'%s description must contain meaningful information beyond what its name provides or be removed.',
ucfirst($tokens[$stackPtr]['content'])
),
$stackPtr,
'InvalidDescription'
);

if ($fix) {
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

if ($tokens[$commentStartPtr - 1]['code'] === T_WHITESPACE
&& $tokens[$commentCloserPtr + 1]['code'] === T_WHITESPACE
) {
$phpcsFile->fixer->replaceToken($commentCloserPtr + 1, '');
}
}
}

if ($this->PHPDocFormattingValidator->hasDeprecatedWellFormatted($commentStartPtr, $tokens) !== true) {
Expand Down Expand Up @@ -105,11 +119,35 @@ private function validateTags(File $phpcsFile, $commentStartPtr, $tokens)
}

if (in_array($tokens[$i]['content'], $this->forbiddenTags) === true) {
$phpcsFile->addWarning(
$fix = $phpcsFile->addFixableWarning(
sprintf('Tag %s MUST NOT be used.', $tokens[$i]['content']),
$i,
'ForbiddenTags'
);

if ($fix) {
for ($j = $i - 1; $j > $commentStartPtr; $j--) {
if (!in_array($tokens[$j]['code'], [T_DOC_COMMENT_STAR, T_DOC_COMMENT_WHITESPACE], true)) {
break;
}

if ($tokens[$j]['code'] === T_DOC_COMMENT_WHITESPACE && $tokens[$j]['content'] === "\n") {
break;
}

$phpcsFile->fixer->replaceToken($j, '');
}

$phpcsFile->fixer->replaceToken($i, '');

for ($j = $i + 1; $j < $commentCloserPtr; $j++) {
$phpcsFile->fixer->replaceToken($j, '');

if ($tokens[$j]['code'] === T_DOC_COMMENT_WHITESPACE && $tokens[$j]['content'] === "\n") {
break;
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,10 @@ class AlsoDeprecatedButHandler

}

/**
* @package this tag should not be used
*/
class OnlyUselessCommentContent
{

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

/**
* Handler for PHP errors/warnings/notices that converts them to exceptions.
*/
class ErrorHandler
{

}

class NotAnErrorHandler
{

}

class FaultyHandler
{

}

class SomeHandler
{

}

class YetAnotherHandler
{

}

class GreenHandler
{

}

class EmptyHandler
{

}

/**
* Handler for PHP errors/warnings/notices that converts them to exceptions.
*
* @api is ok here
* @deprecated can be used in this context
* @see is ok here
*/
class ExampleHandler
{

}

/**
* @api
* @since 100.0.2
*/
class ApiHandler
{

}

/**
* @api
*/
class AsyncApiHandler
{

}

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class GroupRepositoryHandler
{

}

/**
* @deprecated
*/
class DeprecatedHandler
{

}

/**
* @deprecated Should not be used
*/
class AncientHandler
{

}

/**
* @deprecated
* @see
*/
class AgedHandler
{

}

/**
* @deprecated Should not be used
* @see
*/
class ArhaicHandler
{

}

/**
* @deprecated Should not be used
* @see Magento\Framework\NewHandler
*/
class OldHandler
{

}

/**
* @see Magento\Framework\NewHandler
*/
class SomethingHandler
{

}

/**
* @see
*/
class DoNotCareHandler
{

}

/**
* @deprecated
* @see Magento\Framework\NewHandler
*/
class OldHandler
{

}

/**
* @deprecated This class will be removed in version 1.0.0 without replacement
*/
class DeprecatedButHandler
{

}

/**
* @deprecated It's also deprecated - This class will be removed in version 1.0.0 without replacement
*/
class AlsoDeprecatedButHandler
{

}

class OnlyUselessCommentContent
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ interface DoNotCareHandler

}

/**
* @deprecated
* @see Magento\Framework\NewHandler
*/
interface OldHandler
{

}

/**
* @deprecated This interface will be removed in version 1.0.0 without replacement
*/
Expand All @@ -169,3 +178,11 @@ interface AlsoDeprecatedButHandler
{

}

/**
* @package this tag should not be used
*/
interface OnlyUselessCommentContent
{

}
Loading

0 comments on commit d1711ba

Please sign in to comment.