Source for file MemberVarSpacingSniff.php
Documentation is available at MemberVarSpacingSniff.php
* Verifies that class members are spaced correctly.
* @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\WhiteSpace;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
class MemberVarSpacingSniff extends AbstractVariableSniff
* Processes the function tokens within the class.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
protected function processMemberVar (File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens ();
$ignore = Tokens ::$methodPrefixes;
$ignore[] = T_WHITESPACE;
$prev = $phpcsFile->findPrevious ($ignore, ($stackPtr - 1 ), null , true );
if (isset (Tokens ::$commentTokens[$tokens[$prev]['code']]) === true ) {
// Assume the comment belongs to the member var if it is on a line by itself.
$prevContent = $phpcsFile->findPrevious (Tokens ::$emptyTokens, ($prev - 1 ), null , true );
if ($tokens[$prevContent]['line'] !== $tokens[$prev]['line']) {
// Check the spacing, but then skip it.
$foundLines = ($tokens[$stackPtr]['line'] - $tokens[$prev]['line'] - 1 );
$error = 'Expected 0 blank lines after member var comment; %s found';
$data = array ($foundLines);
$fix = $phpcsFile->addFixableError ($error, $prev, 'AfterComment', $data);
$phpcsFile->fixer ->beginChangeset ();
// Inline comments have the newline included in the content but
if ($tokens[$prev]['code'] === T_COMMENT ) {
$phpcsFile->fixer ->replaceToken ($prev, rtrim($tokens[$prev]['content']));
for ($i = ($prev + 1 ); $i <= $stackPtr; $i++ ) {
if ($tokens[$i]['line'] === $tokens[$stackPtr]['line']) {
$phpcsFile->fixer ->replaceToken ($i, '');
$phpcsFile->fixer ->addNewline ($prev);
$phpcsFile->fixer ->endChangeset ();
// There needs to be 1 blank line before the var, not counting comments.
if ($start === $stackPtr) {
$first = $phpcsFile->findFirstOnLine (Tokens ::$emptyTokens, $start, true );
$first = $tokens[$start]['comment_opener'];
$first = $phpcsFile->findPrevious (Tokens ::$emptyTokens, ($start - 1 ), null , true );
$first = $phpcsFile->findNext (Tokens ::$commentTokens, ($first + 1 ));
$prev = $phpcsFile->findPrevious (Tokens ::$emptyTokens, ($first - 1 ), null , true );
$foundLines = ($tokens[$first]['line'] - $tokens[$prev]['line'] - 1 );
$error = 'Expected 1 blank line before member var; %s found';
$data = array ($foundLines);
$fix = $phpcsFile->addFixableError ($error, $stackPtr, 'Incorrect', $data);
$phpcsFile->fixer ->beginChangeset ();
for ($i = ($prev + 1 ); $i < $first; $i++ ) {
if ($tokens[$i]['line'] === $tokens[$prev]['line']) {
if ($tokens[$i]['line'] === $tokens[$first]['line']) {
$phpcsFile->fixer ->addNewline (($i - 1 ));
$phpcsFile->fixer ->replaceToken ($i, '');
$phpcsFile->fixer ->endChangeset ();
}//end processMemberVar()
* Processes normal variables.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
protected function processVariable (File $phpcsFile, $stackPtr)
We don't care about normal variables.
* Processes variables in double quoted strings.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
protected function processVariableInString (File $phpcsFile, $stackPtr)
We don't care about normal variables.
}//end processVariableInString()
Documentation generated on Mon, 11 Mar 2019 14:23:18 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|