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
* The number of blank lines between member vars.
* The number of blank lines before the fist member var.
public $spacingBeforeFirst = 1;
* 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 n blank lines 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 ));
// Determine if this is the first member var.
$prev = $phpcsFile->findPrevious (Tokens ::$emptyTokens, ($first - 1 ), null , true );
&& isset (Tokens ::$ooScopeTokens[$tokens[$tokens[$prev]['scope_condition']]['code']]) === true
$errorMsg = 'Expected %s blank line(s) before first member var; %s found';
$errorCode = 'FirstIncorrect';
$spacing = (int) $this->spacingBeforeFirst;
$errorMsg = 'Expected %s blank line(s) before member var; %s found';
$errorCode = 'Incorrect';
$spacing = (int) $this->spacing;
$foundLines = ($tokens[$first]['line'] - $tokens[$prev]['line'] - 1 );
if ($foundLines === $spacing) {
$fix = $phpcsFile->addFixableError ($errorMsg, $stackPtr, $errorCode, $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']) {
for ($x = 1; $x <= $spacing; $x++ ) {
$phpcsFile->fixer ->addNewlineBefore ($i);
$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 15:27:37 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|