diff --git a/src/PhpCs/FiveLab/ErrorCodes.php b/src/PhpCs/FiveLab/ErrorCodes.php index 7cf95d3..c2a2f5f 100644 --- a/src/PhpCs/FiveLab/ErrorCodes.php +++ b/src/PhpCs/FiveLab/ErrorCodes.php @@ -18,19 +18,19 @@ */ final class ErrorCodes { - public const MISSED_LINE_AFTER = 'MissedLineAfter'; - public const MISSED_LINE_BEFORE = 'MissedLineBefore'; - public const MISSED = 'Missed'; - public const WRONG_FORMAT = 'WrongFormat'; - public const MULTIPLE = 'Multiple'; - public const PROHIBITED = 'Prohibited'; - public const UNUSED = 'Unused'; - public const MISSED_PARAMETER_TYPE = 'MissedFunctionParameterType'; - public const MISSED_RETURN_TYPE = 'MissedFunctionReturnType'; - public const LINE_AFTER_NOT_ALLOWED = 'LineAfterNotAllowed'; - public const LINE_BEFORE_NOT_ALLOWED = 'LineBeforeNotAllowed'; - public const MISSED_CONSTANT_TYPE = 'MissedConstantType'; - public const NAMESPACE_WRONG = 'NamespaceWrong'; - public const ARRAYS_DOC_VECTOR = 'ArraysDocVector'; - public const COMMENT_OUTSIDE_FUNCTION_BODY = 'CommentOutsideFunctionBody'; + public const MISSED_LINE_AFTER = 'MissedLineAfter'; + public const MISSED_LINE_BEFORE = 'MissedLineBefore'; + public const MISSED = 'Missed'; + public const WRONG_FORMAT = 'WrongFormat'; + public const MULTIPLE = 'Multiple'; + public const PROHIBITED = 'Prohibited'; + public const UNUSED = 'Unused'; + public const MISSED_PARAMETER_TYPE = 'MissedFunctionParameterType'; + public const MISSED_RETURN_TYPE = 'MissedFunctionReturnType'; + public const LINE_AFTER_NOT_ALLOWED = 'LineAfterNotAllowed'; + public const LINE_BEFORE_NOT_ALLOWED = 'LineBeforeNotAllowed'; + public const MISSED_CONSTANT_TYPE = 'MissedConstantType'; + public const NAMESPACE_WRONG = 'NamespaceWrong'; + public const ARRAYS_DOC_VECTOR = 'ArraysDocVector'; + public const PHPDOC_NOT_ALLOWED = 'PhpDocNotAllowed'; } diff --git a/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedCommentsSniff.php b/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedCommentsSniff.php deleted file mode 100644 index 445cabb..0000000 --- a/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedCommentsSniff.php +++ /dev/null @@ -1,72 +0,0 @@ -getTokens(); - $commentToken = $tokens[$stackPtr]; - $isInsideFunction = false; - - if (\str_contains($commentToken['content'], '@phpstan')) { - return; - } - - if (\str_starts_with($commentToken['content'], '\*') || \str_starts_with($commentToken['content'], ' *') || \str_starts_with($commentToken['content'], '/*')) { - $beginPtr = $stackPtr; - - foreach ($tokens as $ptr => $token) { - if (T_NAMESPACE === $token['code']) { - if ($beginPtr < $ptr) { - return; - } - } - } - } - - foreach ($tokens as $token) { - if ($token['code'] === T_OPEN_CURLY_BRACKET) { - $conditionToken = $tokens[$token['scope_condition']]; - - if (\in_array($conditionToken['code'], [T_FUNCTION, T_CLOSURE], true)) { - if ($stackPtr > $token['scope_opener'] && $stackPtr < $token['scope_closer']) { - $isInsideFunction = true; - break; - } - } - } - } - - if (!$isInsideFunction) { - $phpcsFile->addError( - 'Simple comments who start with "//, #, /* " prohibited inside function bodies.', - $stackPtr, - ErrorCodes::COMMENT_OUTSIDE_FUNCTION_BODY - ); - } - } -} diff --git a/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniff.php b/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniff.php new file mode 100644 index 0000000..93d3227 --- /dev/null +++ b/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniff.php @@ -0,0 +1,104 @@ +getTokens(); + $commentToken = $tokens[$stackPtr]; + + $possiblyDeclaration = $phpcsFile->findNext([ + T_CLASS, T_INTERFACE, T_TRAIT, T_FUNCTION, + T_ABSTRACT, T_FINAL, T_ENUM + ], $commentToken['comment_closer'] + 1, local: true); + + if ($possiblyDeclaration) { + return; + } + + $varPtr = $phpcsFile->findNext(T_VARIABLE, $commentToken['comment_closer'] + 1, local: true); + + if ($varPtr) { + $commentTokenPtr = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($stackPtr + 1), $commentToken['comment_closer'], local: true); + + if (false === $commentTokenPtr) { + $phpcsFile->addError( + 'PHPDoc comment can contains only @var here.', + $stackPtr, + ErrorCodes::PHPDOC_NOT_ALLOWED + ); + + return; + } + } + + $isOnlyVarTag = $this->isHasOnlyVarTag($phpcsFile, $stackPtr, $commentToken['comment_closer'], $tokens); + + if (true === $isOnlyVarTag) { + return; + } + + $phpcsFile->addError( + 'PHPDoc comment is not allowed here.', + $stackPtr, + ErrorCodes::PHPDOC_NOT_ALLOWED + ); + } + + /** + * Check has T_DOC_COMMENT_TAG another tags + * + * @param File $phpcsFile + * @param int $startPtr + * @param int $endPtr + * @param array $tokens + * + * @return bool|null + */ + private function isHasOnlyVarTag(File $phpcsFile, int $startPtr, int $endPtr, array $tokens): ?bool + { + $commentTokenPtr = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($startPtr + 1), $endPtr); + + if (false === $commentTokenPtr) { + return null; + } + + if ($commentTokenPtr && $tokens[$commentTokenPtr]['content'] !== '@var') { + $phpcsFile->addError( + \sprintf('PHPDoc comment tag %s is not allowed here.', $tokens[$commentTokenPtr]['content']), + $commentTokenPtr, + ErrorCodes::PHPDOC_NOT_ALLOWED + ); + + return false; + } + + $this->isHasOnlyVarTag($phpcsFile, $commentTokenPtr, $endPtr, $tokens); + + return true; + } +} diff --git a/tests/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedCommentsSniffTest.php b/tests/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedCommentsSniffTest.php deleted file mode 100644 index 96c779c..0000000 --- a/tests/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedCommentsSniffTest.php +++ /dev/null @@ -1,82 +0,0 @@ - [ - __DIR__.'/Resources/prohibited-comments/success.php', - ], - - 'wrong' => [ - __DIR__.'/Resources/prohibited-comments/wrong.php', - [ - 'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.', - 'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody', - ], - [ - 'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.', - 'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody', - ], - [ - 'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.', - 'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody', - ], - [ - 'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.', - 'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody', - ], - [ - 'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.', - 'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody', - ], - [ - 'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.', - 'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody', - ], - [ - 'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.', - 'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody', - ], - [ - 'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.', - 'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody', - ], - [ - 'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.', - 'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody', - ], - [ - 'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.', - 'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody', - ], - [ - 'message' => 'Simple comments who start with "//, #, /* " prohibited inside function bodies.', - 'source' => 'FiveLab.Commenting.ProhibitedComments.CommentOutsideFunctionBody', - ], - ], - ]; - } -} diff --git a/tests/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniffTest.php b/tests/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniffTest.php new file mode 100644 index 0000000..cfe0916 --- /dev/null +++ b/tests/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniffTest.php @@ -0,0 +1,62 @@ + [ + __DIR__.'/Resources/prohibited-doc-comments/success.php', + ], + + 'wrong' => [ + __DIR__.'/Resources/prohibited-doc-comments/wrong.php', + [ + 'message' => 'PHPDoc comment is not allowed here.', + 'source' => 'FiveLab.Commenting.ProhibitedDocComments.PhpDocNotAllowed', + ], + [ + 'message' => 'PHPDoc comment can contains only @var here.', + 'source' => 'FiveLab.Commenting.ProhibitedDocComments.PhpDocNotAllowed', + ], + [ + 'message' => 'PHPDoc comment can contains only @var here.', + 'source' => 'FiveLab.Commenting.ProhibitedDocComments.PhpDocNotAllowed', + ], + [ + 'message' => 'PHPDoc comment tag @return is not allowed here.', + 'source' => 'FiveLab.Commenting.ProhibitedDocComments.PhpDocNotAllowed', + ], + [ + 'message' => 'PHPDoc comment can contains only @var here.', + 'source' => 'FiveLab.Commenting.ProhibitedDocComments.PhpDocNotAllowed', + ], + [ + 'message' => 'PHPDoc comment is not allowed here.', + 'source' => 'FiveLab.Commenting.ProhibitedDocComments.PhpDocNotAllowed', + ], + ], + ]; + } +} diff --git a/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/prohibited-comments/success.php b/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/prohibited-comments/success.php deleted file mode 100644 index 984f9a0..0000000 --- a/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/prohibited-comments/success.php +++ /dev/null @@ -1,39 +0,0 @@ -