From efa5ad27d341d9e24b9394158bb6633d8690f973 Mon Sep 17 00:00:00 2001 From: Anastasiia Sherekina Date: Wed, 23 Apr 2025 16:20:28 +0300 Subject: [PATCH 1/3] add rules for phpDoc comments --- src/PhpCs/FiveLab/ErrorCodes.php | 30 ++--- .../Commenting/ProhibitedCommentsSniff.php | 72 ------------ .../Commenting/ProhibitedDocCommentsSniff.php | 107 ++++++++++++++++++ .../ProhibitedCommentsSniffTest.php | 82 -------------- .../ProhibitedDocCommentsSniffTest.php | 62 ++++++++++ .../Resources/prohibited-comments/success.php | 39 ------- .../Resources/prohibited-comments/wrong.php | 23 ---- .../prohibited-doc-comments/success.php | 45 ++++++++ .../prohibited-doc-comments/wrong.php | 41 +++++++ 9 files changed, 270 insertions(+), 231 deletions(-) delete mode 100644 src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedCommentsSniff.php create mode 100644 src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniff.php delete mode 100644 tests/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedCommentsSniffTest.php create mode 100644 tests/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniffTest.php delete mode 100644 tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/prohibited-comments/success.php delete mode 100644 tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/prohibited-comments/wrong.php create mode 100644 tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/prohibited-doc-comments/success.php create mode 100644 tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/prohibited-doc-comments/wrong.php 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..412a804 --- /dev/null +++ b/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniff.php @@ -0,0 +1,107 @@ +getTokens(); + $commentToken = $tokens[$stackPtr]; + + $possiblyDeclaration = $phpcsFile->findNext([ + T_CLASS, T_INTERFACE, T_TRAIT, T_FUNCTION, + T_ABSTRACT, T_FINAL, + ], $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 in function body.', + $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 + ); + + return; + + } + + /** + * 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 in function body.', $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..a6f9db6 --- /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 in function body.', + 'source' => 'FiveLab.Commenting.ProhibitedDocComments.PhpDocNotAllowed', + ], + [ + 'message' => 'PHPDoc comment can contains only @var in function body.', + 'source' => 'FiveLab.Commenting.ProhibitedDocComments.PhpDocNotAllowed', + ], + [ + 'message' => 'PHPDoc comment tag @return is not allowed in function body.', + 'source' => 'FiveLab.Commenting.ProhibitedDocComments.PhpDocNotAllowed', + ], + [ + 'message' => 'PHPDoc comment can contains only @var in function body.', + '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 @@ - Date: Wed, 23 Apr 2025 16:33:40 +0300 Subject: [PATCH 2/3] add rules for phpDoc comments --- .../Sniffs/Commenting/ProhibitedDocCommentsSniff.php | 4 ++-- .../Sniffs/Commenting/ProhibitedDocCommentsSniffTest.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniff.php b/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniff.php index 412a804..9c1c081 100644 --- a/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniff.php +++ b/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniff.php @@ -47,7 +47,7 @@ public function process(File $phpcsFile, mixed $stackPtr): void if (false === $commentTokenPtr) { $phpcsFile->addError( - 'PHPDoc comment can contains only @var in function body.', + 'PHPDoc comment can contains only @var here.', $stackPtr, ErrorCodes::PHPDOC_NOT_ALLOWED ); @@ -92,7 +92,7 @@ private function isHasOnlyVarTag(File $phpcsFile, int $startPtr, int $endPtr, ar if ($commentTokenPtr && $tokens[$commentTokenPtr]['content'] !== '@var') { $phpcsFile->addError( - \sprintf('PHPDoc comment tag %s is not allowed in function body.', $tokens[$commentTokenPtr]['content']), + \sprintf('PHPDoc comment tag %s is not allowed here.', $tokens[$commentTokenPtr]['content']), $commentTokenPtr, ErrorCodes::PHPDOC_NOT_ALLOWED ); diff --git a/tests/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniffTest.php b/tests/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniffTest.php index a6f9db6..cfe0916 100644 --- a/tests/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniffTest.php +++ b/tests/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniffTest.php @@ -37,19 +37,19 @@ public static function provideDataSet(): array 'source' => 'FiveLab.Commenting.ProhibitedDocComments.PhpDocNotAllowed', ], [ - 'message' => 'PHPDoc comment can contains only @var in function body.', + 'message' => 'PHPDoc comment can contains only @var here.', 'source' => 'FiveLab.Commenting.ProhibitedDocComments.PhpDocNotAllowed', ], [ - 'message' => 'PHPDoc comment can contains only @var in function body.', + 'message' => 'PHPDoc comment can contains only @var here.', 'source' => 'FiveLab.Commenting.ProhibitedDocComments.PhpDocNotAllowed', ], [ - 'message' => 'PHPDoc comment tag @return is not allowed in function body.', + 'message' => 'PHPDoc comment tag @return is not allowed here.', 'source' => 'FiveLab.Commenting.ProhibitedDocComments.PhpDocNotAllowed', ], [ - 'message' => 'PHPDoc comment can contains only @var in function body.', + 'message' => 'PHPDoc comment can contains only @var here.', 'source' => 'FiveLab.Commenting.ProhibitedDocComments.PhpDocNotAllowed', ], [ From 1544f3f29a1ed39f27222782c3bf42e138bf474a Mon Sep 17 00:00:00 2001 From: Anastasiia Sherekina Date: Wed, 23 Apr 2025 17:01:29 +0300 Subject: [PATCH 3/3] add rules for phpDoc comments --- .../FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniff.php | 5 +---- .../Commenting/Resources/prohibited-doc-comments/success.php | 5 +++++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniff.php b/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniff.php index 9c1c081..93d3227 100644 --- a/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniff.php +++ b/src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedDocCommentsSniff.php @@ -33,7 +33,7 @@ public function process(File $phpcsFile, mixed $stackPtr): void $possiblyDeclaration = $phpcsFile->findNext([ T_CLASS, T_INTERFACE, T_TRAIT, T_FUNCTION, - T_ABSTRACT, T_FINAL, + T_ABSTRACT, T_FINAL, T_ENUM ], $commentToken['comment_closer'] + 1, local: true); if ($possiblyDeclaration) { @@ -67,9 +67,6 @@ public function process(File $phpcsFile, mixed $stackPtr): void $stackPtr, ErrorCodes::PHPDOC_NOT_ALLOWED ); - - return; - } /** diff --git a/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/prohibited-doc-comments/success.php b/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/prohibited-doc-comments/success.php index 005c250..12298a4 100644 --- a/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/prohibited-doc-comments/success.php +++ b/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/prohibited-doc-comments/success.php @@ -43,3 +43,8 @@ trait Some4 {} * Interface */ interface Some5 {} + +/** + * Enum + */ +enum Some6 {}