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.     /**
  21.      * Processes the function tokens within the class.
  22.      *
  23.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  24.      * @param int                         $stackPtr  The position where the token was found.
  25.      *
  26.      * @return void 
  27.      */
  28.     protected function processMemberVar(File $phpcsFile$stackPtr)
  29.     {
  30.         $tokens $phpcsFile->getTokens();
  31.  
  32.         $ignore   = Tokens::$methodPrefixes;
  33.         $ignore[= T_VAR;
  34.         $ignore[= T_WHITESPACE;
  35.  
  36.         $start $stackPtr;
  37.         $prev  $phpcsFile->findPrevious($ignore($stackPtr - 1)nulltrue);
  38.         if (isset(Tokens::$commentTokens[$tokens[$prev]['code']]=== true{
  39.             // Assume the comment belongs to the member var if it is on a line by itself.
  40.             $prevContent $phpcsFile->findPrevious(Tokens::$emptyTokens($prev - 1)nulltrue);
  41.             if ($tokens[$prevContent]['line'!== $tokens[$prev]['line']{
  42.                 // Check the spacing, but then skip it.
  43.                 $foundLines ($tokens[$stackPtr]['line'$tokens[$prev]['line'- 1);
  44.                 if ($foundLines > 0{
  45.                     $error 'Expected 0 blank lines after member var comment; %s found';
  46.                     $data  = array($foundLines);
  47.                     $fix   $phpcsFile->addFixableError($error$prev'AfterComment'$data);
  48.                     if ($fix === true{
  49.                         $phpcsFile->fixer->beginChangeset();
  50.                         // Inline comments have the newline included in the content but
  51.                         // docblock do not.
  52.                         if ($tokens[$prev]['code'=== T_COMMENT{
  53.                             $phpcsFile->fixer->replaceToken($prevrtrim($tokens[$prev]['content']));
  54.                         }
  55.  
  56.                         for ($i ($prev + 1)$i <= $stackPtr$i++{
  57.                             if ($tokens[$i]['line'=== $tokens[$stackPtr]['line']{
  58.                                 break;
  59.                             }
  60.  
  61.                             $phpcsFile->fixer->replaceToken($i'');
  62.                         }
  63.  
  64.                         $phpcsFile->fixer->addNewline($prev);
  65.                         $phpcsFile->fixer->endChangeset();
  66.                     }
  67.                 }//end if
  68.  
  69.                 $start $prev;
  70.             }//end if
  71.         }//end if
  72.  
  73.         // There needs to be 1 blank line before the var, not counting comments.
  74.         if ($start === $stackPtr{
  75.             // No comment found.
  76.             $first $phpcsFile->findFirstOnLine(Tokens::$emptyTokens$starttrue);
  77.             if ($first === false{
  78.                 $first $start;
  79.             }
  80.         else if ($tokens[$start]['code'=== T_DOC_COMMENT_CLOSE_TAG{
  81.             $first $tokens[$start]['comment_opener'];
  82.         else {
  83.             $first $phpcsFile->findPrevious(Tokens::$emptyTokens($start - 1)nulltrue);
  84.             $first $phpcsFile->findNext(Tokens::$commentTokens($first + 1));
  85.         }
  86.  
  87.         $prev       $phpcsFile->findPrevious(Tokens::$emptyTokens($first - 1)nulltrue);
  88.         $foundLines ($tokens[$first]['line'$tokens[$prev]['line'- 1);
  89.         if ($foundLines === 1{
  90.             return;
  91.         }
  92.  
  93.         $error 'Expected 1 blank line before member var; %s found';
  94.         $data  = array($foundLines);
  95.         $fix   $phpcsFile->addFixableError($error$stackPtr'Incorrect'$data);
  96.         if ($fix === true{
  97.             $phpcsFile->fixer->beginChangeset();
  98.             for ($i ($prev + 1)$i $first$i++{
  99.                 if ($tokens[$i]['line'=== $tokens[$prev]['line']{
  100.                     continue;
  101.                 }
  102.  
  103.                 if ($tokens[$i]['line'=== $tokens[$first]['line']{
  104.                     $phpcsFile->fixer->addNewline(($i - 1));
  105.                     break;
  106.                 }
  107.  
  108.                 $phpcsFile->fixer->replaceToken($i'');
  109.             }
  110.  
  111.             $phpcsFile->fixer->endChangeset();
  112.         }//end if
  113.  
  114.     }//end processMemberVar()
  115.  
  116.  
  117.     /**
  118.      * Processes normal variables.
  119.      *
  120.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  121.      * @param int                         $stackPtr  The position where the token was found.
  122.      *
  123.      * @return void 
  124.      */
  125.     protected function processVariable(File $phpcsFile$stackPtr)
  126.     {
  127.         /*
  128.             We don't care about normal variables.
  129.         */
  130.  
  131.     }//end processVariable()
  132.  
  133.  
  134.     /**
  135.      * Processes variables in double quoted strings.
  136.      *
  137.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  138.      * @param int                         $stackPtr  The position where the token was found.
  139.      *
  140.      * @return void 
  141.      */
  142.     protected function processVariableInString(File $phpcsFile$stackPtr)
  143.     {
  144.         /*
  145.             We don't care about normal variables.
  146.         */
  147.  
  148.     }//end processVariableInString()
  149.  
  150.  
  151. }//end class

Documentation generated on Mon, 11 Mar 2019 14:18:04 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.