12 use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
13 use PHP_CodeSniffer\Files\File;
14 use PHP_CodeSniffer\Util\Common;
31 $tokens = $phpcsFile->getTokens();
41 $commentEnd = $phpcsFile->findPrevious($ignore, ($stackPtr - 1),
null,
true);
42 if ($commentEnd ===
false 43 || (
$tokens[$commentEnd][
'code'] !== T_DOC_COMMENT_CLOSE_TAG
44 &&
$tokens[$commentEnd][
'code'] !== T_COMMENT)
46 $phpcsFile->addError(
'Missing member variable doc comment', $stackPtr,
'Missing');
50 if (
$tokens[$commentEnd][
'code'] === T_COMMENT) {
51 $phpcsFile->addError(
'You must use "/**" style comments for a member variable comment', $stackPtr,
'WrongStyle');
55 $commentStart =
$tokens[$commentEnd][
'comment_opener'];
58 foreach (
$tokens[$commentStart][
'comment_tags'] as $tag) {
59 if (
$tokens[$tag][
'content'] ===
'@var') {
60 if ($foundVar !==
null) {
61 $error =
'Only one @var tag is allowed in a member variable comment';
62 $phpcsFile->addError($error, $tag,
'DuplicateVar');
66 }
else if (
$tokens[$tag][
'content'] ===
'@see') {
68 $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
69 if ($string ===
false ||
$tokens[$string][
'line'] !==
$tokens[$tag][
'line']) {
70 $error =
'Content missing for @see tag in member variable comment';
71 $phpcsFile->addError($error, $tag,
'EmptySees');
74 $error =
'%s tag is not allowed in member variable comment';
76 $phpcsFile->addWarning($error, $tag,
'TagNotAllowed',
$data);
81 if ($foundVar ===
null) {
82 $error =
'Missing @var tag in member variable comment';
83 $phpcsFile->addError($error, $commentEnd,
'MissingVar');
87 $firstTag =
$tokens[$commentStart][
'comment_tags'][0];
88 if ($foundVar !==
null &&
$tokens[$firstTag][
'content'] !==
'@var') {
89 $error =
'The @var tag must be the first tag in a member variable comment';
90 $phpcsFile->addError($error, $foundVar,
'VarOrder');
94 $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $foundVar, $commentEnd);
95 if ($string ===
false ||
$tokens[$string][
'line'] !==
$tokens[$foundVar][
'line']) {
96 $error =
'Content missing for @var tag in member variable comment';
97 $phpcsFile->addError($error, $foundVar,
'EmptyVar');
101 $varType =
$tokens[($foundVar + 2)][
'content'];
102 $suggestedType = Common::suggestType($varType);
103 if ($varType !== $suggestedType) {
104 $error =
'Expected "%s" but found "%s" for @var tag in member variable comment';
110 $fix = $phpcsFile->addFixableError($error, ($foundVar + 2),
'IncorrectVarType',
$data);
112 $phpcsFile->fixer->replaceToken(($foundVar + 2), $suggestedType);