From 308391d6c711be54434289b0762b0b7afc8dbba9 Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Sat, 30 Apr 2022 21:27:42 +0200 Subject: [PATCH] Allow @inheritdoc instead of @var on property --- .../Commenting/VariableCommentSniff.php | 48 +++++++++++++++++++ src/DigiComp/ruleset.xml | 13 ++--- src/Inwebs/ruleset.xml | 6 ++- .../Commenting/VariableCommentSniffTest.php | 14 ++++++ .../Sniffs/Commenting/data/comments.php | 22 +++++++++ 5 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 src/DigiComp/Sniffs/Commenting/VariableCommentSniff.php create mode 100644 tests/DigiComp/Sniffs/Commenting/VariableCommentSniffTest.php create mode 100644 tests/DigiComp/Sniffs/Commenting/data/comments.php 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 @@ +