PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file MemberVarSpacingSniff.php

Documentation is available at MemberVarSpacingSniff.php

  1. <?php
  2. /**
  3.  * Verifies that class members are spaced correctly.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  6.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8.  */
  9.  
  10. namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace;
  11.  
  12. use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class MemberVarSpacingSniff extends AbstractVariableSniff
  17. {
  18.  
  19.     /**
  20.      * The number of blank lines between member vars.
  21.      *
  22.      * @var integer 
  23.      */
  24.     public $spacing = 1;
  25.  
  26.     /**
  27.      * The number of blank lines before the fist member var.
  28.      *
  29.      * @var integer 
  30.      */
  31.     public $spacingBeforeFirst = 1;
  32.  
  33.  
  34.     /**
  35.      * Processes the function tokens within the class.
  36.      *
  37.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  38.      * @param int                         $stackPtr  The position where the token was found.
  39.      *
  40.      * @return void 
  41.      */
  42.     protected function processMemberVar(File $phpcsFile, $stackPtr)
  43.     {
  44.         $tokens = $phpcsFile->getTokens();
  45.  
  46.         $ignore   = Tokens::$methodPrefixes;
  47.         $ignore[] = T_VAR;
  48.         $ignore[] = T_WHITESPACE;
  49.  
  50.         $start = $stackPtr;
  51.         $prev  = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
  52.         if (isset(Tokens::$commentTokens[$tokens[$prev]['code']]) === true) {
  53.             // Assume the comment belongs to the member var if it is on a line by itself.
  54.             $prevContent = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true);
  55.             if ($tokens[$prevContent]['line'] !== $tokens[$prev]['line']) {
  56.                 // Check the spacing, but then skip it.
  57.                 $foundLines = ($tokens[$stackPtr]['line'] - $tokens[$prev]['line'] - 1);
  58.                 if ($foundLines > 0) {
  59.                     $error = 'Expected 0 blank lines after member var comment; %s found';
  60.                     $data  = array($foundLines);
  61.                     $fix   = $phpcsFile->addFixableError($error, $prev, 'AfterComment', $data);
  62.                     if ($fix === true) {
  63.                         $phpcsFile->fixer->beginChangeset();
  64.                         // Inline comments have the newline included in the content but
  65.                         // docblock do not.
  66.                         if ($tokens[$prev]['code'] === T_COMMENT) {
  67.                             $phpcsFile->fixer->replaceToken($prev, rtrim($tokens[$prev]['content']));
  68.                         }
  69.  
  70.                         for ($i = ($prev + 1); $i <= $stackPtr; $i++) {
  71.                             if ($tokens[$i]['line'] === $tokens[$stackPtr]['line']) {
  72.                                 break;
  73.                             }
  74.  
  75.                             $phpcsFile->fixer->replaceToken($i, '');
  76.                         }
  77.  
  78.                         $phpcsFile->fixer->addNewline($prev);
  79.                         $phpcsFile->fixer->endChangeset();
  80.                     }
  81.                 }//end if
  82.  
  83.                 $start = $prev;
  84.             }//end if
  85.         }//end if
  86.  
  87.         // There needs to be n blank lines before the var, not counting comments.
  88.         if ($start === $stackPtr) {
  89.             // No comment found.
  90.             $first = $phpcsFile->findFirstOnLine(Tokens::$emptyTokens, $start, true);
  91.             if ($first === false) {
  92.                 $first = $start;
  93.             }
  94.         } else if ($tokens[$start]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
  95.             $first = $tokens[$start]['comment_opener'];
  96.         } else {
  97.             $first = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($start - 1), null, true);
  98.             $first = $phpcsFile->findNext(Tokens::$commentTokens, ($first + 1));
  99.         }
  100.  
  101.         // Determine if this is the first member var.
  102.         $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($first - 1), null, true);
  103.         if ($tokens[$prev]['code'] === T_OPEN_CURLY_BRACKET
  104.             && isset(Tokens::$ooScopeTokens[$tokens[$tokens[$prev]['scope_condition']]['code']]) === true
  105.         ) {
  106.             $errorMsg  = 'Expected %s blank line(s) before first member var; %s found';
  107.             $errorCode = 'FirstIncorrect';
  108.             $spacing   = (int) $this->spacingBeforeFirst;
  109.         } else {
  110.             $errorMsg  = 'Expected %s blank line(s) before member var; %s found';
  111.             $errorCode = 'Incorrect';
  112.             $spacing   = (int) $this->spacing;
  113.         }
  114.  
  115.         $foundLines = ($tokens[$first]['line'] - $tokens[$prev]['line'] - 1);
  116.         if ($foundLines === $spacing) {
  117.             return;
  118.         }
  119.  
  120.         $data = array(
  121.                  $spacing,
  122.                  $foundLines,
  123.                 );
  124.  
  125.         $fix = $phpcsFile->addFixableError($errorMsg, $stackPtr, $errorCode, $data);
  126.         if ($fix === true) {
  127.             $phpcsFile->fixer->beginChangeset();
  128.             for ($i = ($prev + 1); $i < $first; $i++) {
  129.                 if ($tokens[$i]['line'] === $tokens[$prev]['line']) {
  130.                     continue;
  131.                 }
  132.  
  133.                 if ($tokens[$i]['line'] === $tokens[$first]['line']) {
  134.                     for ($x = 1; $x <= $spacing; $x++) {
  135.                         $phpcsFile->fixer->addNewlineBefore($i);
  136.                     }
  137.  
  138.                     break;
  139.                 }
  140.  
  141.                 $phpcsFile->fixer->replaceToken($i, '');
  142.             }
  143.  
  144.             $phpcsFile->fixer->endChangeset();
  145.         }//end if
  146.  
  147.     }//end processMemberVar()
  148.  
  149.  
  150.     /**
  151.      * Processes normal variables.
  152.      *
  153.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  154.      * @param int                         $stackPtr  The position where the token was found.
  155.      *
  156.      * @return void 
  157.      */
  158.     protected function processVariable(File $phpcsFile, $stackPtr)
  159.     {
  160.         /*
  161.             We don't care about normal variables.
  162.         */
  163.  
  164.     }//end processVariable()
  165.  
  166.  
  167.     /**
  168.      * Processes variables in double quoted strings.
  169.      *
  170.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  171.      * @param int                         $stackPtr  The position where the token was found.
  172.      *
  173.      * @return void 
  174.      */
  175.     protected function processVariableInString(File $phpcsFile, $stackPtr)
  176.     {
  177.         /*
  178.             We don't care about normal variables.
  179.         */
  180.  
  181.     }//end processVariableInString()
  182.  
  183.  
  184. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:37 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.