Source for file VariableCommentSniff.php
Documentation is available at VariableCommentSniff.php
* Parses and verifies the variable doc comment.
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Common;
class VariableCommentSniff extends AbstractVariableSniff
* Called to process class member vars.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
public function processMemberVar (File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens ();
$commentEnd = $phpcsFile->findPrevious ($ignore, ($stackPtr - 1 ), null , true );
if ($commentEnd === false
&& $tokens[$commentEnd]['code'] !== T_COMMENT )
$phpcsFile->addError ('Missing member variable doc comment', $stackPtr, 'Missing');
if ($tokens[$commentEnd]['code'] === T_COMMENT ) {
$phpcsFile->addError ('You must use "/**" style comments for a member variable comment', $stackPtr, 'WrongStyle');
$commentStart = $tokens[$commentEnd]['comment_opener'];
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
if ($tokens[$tag]['content'] === '@var') {
if ($foundVar !== null ) {
$error = 'Only one @var tag is allowed in a member variable comment';
$phpcsFile->addError ($error, $tag, 'DuplicateVar');
} else if ($tokens[$tag]['content'] === '@see') {
// Make sure the tag isn't empty.
if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) {
$error = 'Content missing for @see tag in member variable comment';
$phpcsFile->addError ($error, $tag, 'EmptySees');
$error = '%s tag is not allowed in member variable comment';
$data = array ($tokens[$tag]['content']);
$phpcsFile->addWarning ($error, $tag, 'TagNotAllowed', $data);
// The @var tag is the only one we require.
if ($foundVar === null ) {
$error = 'Missing @var tag in member variable comment';
$phpcsFile->addError ($error, $commentEnd, 'MissingVar');
$firstTag = $tokens[$commentStart]['comment_tags'][0 ];
if ($foundVar !== null && $tokens[$firstTag]['content'] !== '@var') {
$error = 'The @var tag must be the first tag in a member variable comment';
$phpcsFile->addError ($error, $foundVar, 'VarOrder');
// Make sure the tag isn't empty and has the correct padding.
if ($string === false || $tokens[$string]['line'] !== $tokens[$foundVar]['line']) {
$error = 'Content missing for @var tag in member variable comment';
$phpcsFile->addError ($error, $foundVar, 'EmptyVar');
$varType = $tokens[($foundVar + 2 )]['content'];
$suggestedType = Common ::suggestType ($varType);
if ($varType !== $suggestedType) {
$error = 'Expected "%s" but found "%s" for @var tag in member variable comment';
$fix = $phpcsFile->addFixableError ($error, ($foundVar + 2 ), 'IncorrectVarType', $data);
$phpcsFile->fixer ->replaceToken (($foundVar + 2 ), $suggestedType);
}//end processMemberVar()
* Called to process a normal variable.
* Not required for this sniff.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this token was found.
* @param int $stackPtr The position where the double quoted
protected function processVariable (File $phpcsFile, $stackPtr)
* Called to process variables found in double quoted strings.
* Not required for this sniff.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this token was found.
* @param int $stackPtr The position where the double quoted
protected function processVariableInString (File $phpcsFile, $stackPtr)
}//end processVariableInString()
Documentation generated on Mon, 11 Mar 2019 15:27:52 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|