diff --git a/src/DigiComp/Sniffs/Commenting/VariableCommentSniff.php b/src/DigiComp/Sniffs/Commenting/VariableCommentSniff.php new file mode 100644 index 0000000..d181735 --- /dev/null +++ b/src/DigiComp/Sniffs/Commenting/VariableCommentSniff.php @@ -0,0 +1,48 @@ +getTokens(); + $ignore = [ + \T_PUBLIC, + \T_PRIVATE, + \T_PROTECTED, + \T_VAR, + \T_STATIC, + \T_WHITESPACE, + \T_STRING, + \T_NS_SEPARATOR, + \T_NULLABLE, + ]; + + $commentEnd = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true); + + + $commentStart = $tokens[$commentEnd]['comment_opener']; + + $foundVar = null; + foreach ($tokens[$commentStart]['comment_tags'] as $tag) { + if ( + $tokens[$tag]['content'] === '@var' + || $tokens[$tag]['content'] === '@inheritDoc' + ) { + $foundVar = $tag; + } + } + + // The @var tag is the only one we require. + if ($foundVar === null) { + $error = 'Missing @var or @inheritDoc tag in member variable comment'; + $phpcsFile->addWarning($error, $commentEnd, 'MissingVarOrInheritDoc'); + return; + } + + parent::processMemberVar($phpcsFile, $stackPtr); + } +} diff --git a/src/DigiComp/ruleset.xml b/src/DigiComp/ruleset.xml index fa0b776..398e8ae 100644 --- a/src/DigiComp/ruleset.xml +++ b/src/DigiComp/ruleset.xml @@ -9,13 +9,14 @@ - - - - + + + + + - - + + warning 4 diff --git a/src/Inwebs/ruleset.xml b/src/Inwebs/ruleset.xml index d02d568..f18982a 100644 --- a/src/Inwebs/ruleset.xml +++ b/src/Inwebs/ruleset.xml @@ -13,7 +13,11 @@ - + + error + 6 + + error 6 diff --git a/tests/DigiComp/Sniffs/Commenting/VariableCommentSniffTest.php b/tests/DigiComp/Sniffs/Commenting/VariableCommentSniffTest.php new file mode 100644 index 0000000..b153d4c --- /dev/null +++ b/tests/DigiComp/Sniffs/Commenting/VariableCommentSniffTest.php @@ -0,0 +1,14 @@ +getWarnings()[20][6]); + } +} diff --git a/tests/DigiComp/Sniffs/Commenting/data/comments.php b/tests/DigiComp/Sniffs/Commenting/data/comments.php new file mode 100644 index 0000000..4452582 --- /dev/null +++ b/tests/DigiComp/Sniffs/Commenting/data/comments.php @@ -0,0 +1,22 @@ +