php-codesniffer/src/DigiComp/Sniffs/Commenting/VariableCommentSniff.php
Ferdinand Kuhl d2e164d5e5
All checks were successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/code-style Pipeline was successful
fix possible bug in restoring the original severity if ignoring parent errors
2023-02-14 12:24:55 +01:00

66 lines
2.7 KiB
PHP

<?php
namespace DigiComp\PhpCodesniffer\DigiComp\Sniffs\Commenting;
use DigiComp\PhpCodesniffer\DigiComp\Sniffs\MemberVarUtility;
use PHP_CodeSniffer\Files\File;
class VariableCommentSniff extends \PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\VariableCommentSniff
{
public function processMemberVar(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$ignore = [
\T_PUBLIC => \T_PUBLIC,
\T_PRIVATE => \T_PRIVATE,
\T_PROTECTED => \T_PROTECTED,
\T_VAR => \T_VAR,
\T_NULL => \T_NULL,
\T_STATIC => \T_STATIC,
\T_TYPE_UNION => \T_TYPE_UNION,
\T_WHITESPACE => \T_WHITESPACE,
\T_STRING => \T_STRING,
\T_NS_SEPARATOR => \T_NS_SEPARATOR,
\T_NULLABLE => \T_NULLABLE,
];
$commentEnd = MemberVarUtility::findCommentEndWithoutAttributes($tokens, $ignore, $stackPtr);
// this error is copied from above, as it comes with a needed early return
if (
$commentEnd === false
|| ($tokens[$commentEnd]['code'] !== \T_DOC_COMMENT_CLOSE_TAG
&& $tokens[$commentEnd]['code'] !== \T_COMMENT)
) {
$phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing');
return;
}
$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;
}
// remove the error reporting from base class, as it does not accept union types
// https://github.com/squizlabs/PHP_CodeSniffer/pull/3757
$oldSeverity = $phpcsFile->ruleset->ruleset['DigiComp.Commenting.VariableComment.Missing']['severity'] ?? null;
$phpcsFile->ruleset->ruleset['DigiComp.Commenting.VariableComment.Missing']['severity'] = 0;
parent::processMemberVar($phpcsFile, $stackPtr);
if ($oldSeverity !== null) {
$phpcsFile->ruleset->ruleset['DigiComp.Commenting.VariableComment.Missing']['severity'] = $oldSeverity;
} else {
unset($phpcsFile->ruleset->ruleset['DigiComp.Commenting.VariableComment.Missing']['severity']);
}
}
}