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

Source for file SuperfluousWhitespaceSniff.php

Documentation is available at SuperfluousWhitespaceSniff.php

  1. <?php
  2. /**
  3.  * Checks for unneeded whitespace.
  4.  *
  5.  * Checks that no whitespace preceeds the first content of the file, exists
  6.  * after the last content of the file, resides after content on any line, or
  7.  * are two empty lines in functions.
  8.  *
  9.  * @author    Greg Sherwood <gsherwood@squiz.net>
  10.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  11.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  12.  */
  13.  
  14. namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace;
  15.  
  16. use PHP_CodeSniffer\Sniffs\Sniff;
  17. use PHP_CodeSniffer\Files\File;
  18.  
  19. class SuperfluousWhitespaceSniff implements Sniff
  20. {
  21.  
  22.     /**
  23.      * A list of tokenizers this sniff supports.
  24.      *
  25.      * @var array 
  26.      */
  27.     public $supportedTokenizers = array(
  28.                                    'PHP',
  29.                                    'JS',
  30.                                    'CSS',
  31.                                   );
  32.  
  33.     /**
  34.      * If TRUE, whitespace rules are not checked for blank lines.
  35.      *
  36.      * Blank lines are those that contain only whitespace.
  37.      *
  38.      * @var boolean 
  39.      */
  40.     public $ignoreBlankLines = false;
  41.  
  42.  
  43.     /**
  44.      * Returns an array of tokens this test wants to listen for.
  45.      *
  46.      * @return array 
  47.      */
  48.     public function register()
  49.     {
  50.         return array(
  51.                 T_OPEN_TAG,
  52.                 T_CLOSE_TAG,
  53.                 T_WHITESPACE,
  54.                 T_COMMENT,
  55.                 T_DOC_COMMENT_WHITESPACE,
  56.                 T_CLOSURE,
  57.                );
  58.  
  59.     }//end register()
  60.  
  61.  
  62.     /**
  63.      * Processes this sniff, when one of its tokens is encountered.
  64.      *
  65.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  66.      * @param int                         $stackPtr  The position of the current token in the
  67.      *                                                stack passed in $tokens.
  68.      *
  69.      * @return void 
  70.      */
  71.     public function process(File $phpcsFile$stackPtr)
  72.     {
  73.         $tokens $phpcsFile->getTokens();
  74.  
  75.         if ($tokens[$stackPtr]['code'=== T_OPEN_TAG{
  76.             /*
  77.                 Check for start of file whitespace.
  78.             */
  79.  
  80.             if ($phpcsFile->tokenizerType !== 'PHP'{
  81.                 // The first token is always the open tag inserted when tokenizsed
  82.                 // and the second token is always the first piece of content in
  83.                 // the file. If the second token is whitespace, there was
  84.                 // whitespace at the start of the file.
  85.                 if ($tokens[($stackPtr + 1)]['code'!== T_WHITESPACE{
  86.                     return;
  87.                 }
  88.  
  89.                 if ($phpcsFile->fixer->enabled === true{
  90.                     $stackPtr $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  91.                 }
  92.             else {
  93.                 // If it's the first token, then there is no space.
  94.                 if ($stackPtr === 0{
  95.                     return;
  96.                 }
  97.  
  98.                 for ($i ($stackPtr - 1)$i >= 0; $i--{
  99.                     // If we find something that isn't inline html then there is something previous in the file.
  100.                     if ($tokens[$i]['type'!== 'T_INLINE_HTML'{
  101.                         return;
  102.                     }
  103.  
  104.                     // If we have ended up with inline html make sure it isn't just whitespace.
  105.                     $tokenContent trim($tokens[$i]['content']);
  106.                     if ($tokenContent !== ''{
  107.                         return;
  108.                     }
  109.                 }
  110.             }//end if
  111.  
  112.             $fix $phpcsFile->addFixableError('Additional whitespace found at start of file'$stackPtr'StartFile');
  113.             if ($fix === true{
  114.                 $phpcsFile->fixer->beginChangeset();
  115.                 for ($i = 0; $i $stackPtr$i++{
  116.                     $phpcsFile->fixer->replaceToken($i'');
  117.                 }
  118.  
  119.                 $phpcsFile->fixer->endChangeset();
  120.             }
  121.         else if ($tokens[$stackPtr]['code'=== T_CLOSE_TAG{
  122.             /*
  123.                 Check for end of file whitespace.
  124.             */
  125.  
  126.             if ($phpcsFile->tokenizerType === 'PHP'{
  127.                 if (isset($tokens[($stackPtr + 1)]=== false{
  128.                     // The close PHP token is the last in the file.
  129.                     return;
  130.                 }
  131.  
  132.                 for ($i ($stackPtr + 1)$i $phpcsFile->numTokens; $i++{
  133.                     // If we find something that isn't inline HTML then there
  134.                     // is more to the file.
  135.                     if ($tokens[$i]['type'!== 'T_INLINE_HTML'{
  136.                         return;
  137.                     }
  138.  
  139.                     // If we have ended up with inline html make sure it
  140.                     // isn't just whitespace.
  141.                     $tokenContent trim($tokens[$i]['content']);
  142.                     if (empty($tokenContent=== false{
  143.                         return;
  144.                     }
  145.                 }
  146.             else {
  147.                 // The last token is always the close tag inserted when tokenized
  148.                 // and the second last token is always the last piece of content in
  149.                 // the file. If the second last token is whitespace, there was
  150.                 // whitespace at the end of the file.
  151.                 $stackPtr--;
  152.  
  153.                 // The pointer is now looking at the last content in the file and
  154.                 // not the fake PHP end tag the tokenizer inserted.
  155.                 if ($tokens[$stackPtr]['code'!== T_WHITESPACE{
  156.                     return;
  157.                 }
  158.  
  159.                 // Allow a single newline at the end of the last line in the file.
  160.                 if ($tokens[($stackPtr - 1)]['code'!== T_WHITESPACE
  161.                     && $tokens[$stackPtr]['content'=== $phpcsFile->eolChar
  162.                 {
  163.                     return;
  164.                 }
  165.  
  166.                 if ($phpcsFile->fixer->enabled === true{
  167.                     $prev     $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1)nulltrue);
  168.                     $stackPtr ($prev + 1);
  169.                 }
  170.             }//end if
  171.  
  172.             $fix $phpcsFile->addFixableError('Additional whitespace found at end of file'$stackPtr'EndFile');
  173.             if ($fix === true{
  174.                 $phpcsFile->fixer->beginChangeset();
  175.                 for ($i ($stackPtr + 1)$i $phpcsFile->numTokens; $i++{
  176.                     $phpcsFile->fixer->replaceToken($i'');
  177.                 }
  178.  
  179.                 $phpcsFile->fixer->endChangeset();
  180.             }
  181.         else {
  182.             /*
  183.                 Check for end of line whitespace.
  184.             */
  185.  
  186.             // Ignore whitespace that is not at the end of a line.
  187.             if (isset($tokens[($stackPtr + 1)]['line']=== true
  188.                 && $tokens[($stackPtr + 1)]['line'=== $tokens[$stackPtr]['line']
  189.             {
  190.                 return;
  191.             }
  192.  
  193.             // Ignore blank lines if required.
  194.             if ($this->ignoreBlankLines === true
  195.                 && $tokens[($stackPtr - 1)]['line'!== $tokens[$stackPtr]['line']
  196.             {
  197.                 return;
  198.             }
  199.  
  200.             $tokenContent rtrim($tokens[$stackPtr]['content']$phpcsFile->eolChar);
  201.             if (empty($tokenContent=== false{
  202.                 if ($tokenContent !== rtrim($tokenContent)) {
  203.                     $fix $phpcsFile->addFixableError('Whitespace found at end of line'$stackPtr'EndLine');
  204.                     if ($fix === true{
  205.                         $phpcsFile->fixer->replaceToken($stackPtrrtrim($tokenContent).$phpcsFile->eolChar);
  206.                     }
  207.                 }
  208.             else if ($tokens[($stackPtr - 1)]['content'!== rtrim($tokens[($stackPtr - 1)]['content'])
  209.                 && $tokens[($stackPtr - 1)]['line'=== $tokens[$stackPtr]['line']
  210.             {
  211.                 $fix $phpcsFile->addFixableError('Whitespace found at end of line'($stackPtr - 1)'EndLine');
  212.                 if ($fix === true{
  213.                     $phpcsFile->fixer->replaceToken(($stackPtr - 1)rtrim($tokens[($stackPtr - 1)]['content']));
  214.                 }
  215.             }
  216.  
  217.             /*
  218.                 Check for multiple blank lines in a function.
  219.             */
  220.  
  221.             if (($phpcsFile->hasCondition($stackPtrT_FUNCTION=== true
  222.                 || $phpcsFile->hasCondition($stackPtrT_CLOSURE=== true)
  223.                 && $tokens[($stackPtr - 1)]['line'$tokens[$stackPtr]['line']
  224.                 && $tokens[($stackPtr - 2)]['line'=== $tokens[($stackPtr - 1)]['line']
  225.             {
  226.                 // This is an empty line and the line before this one is not
  227.                 // empty, so this could be the start of a multiple empty
  228.                 // line block.
  229.                 $next  $phpcsFile->findNext(T_WHITESPACE$stackPtrnulltrue);
  230.                 $lines ($tokens[$next]['line'$tokens[$stackPtr]['line']);
  231.                 if ($lines > 1{
  232.                     $error 'Functions must not contain multiple empty lines in a row; found %s empty lines';
  233.                     $fix   $phpcsFile->addFixableError($error$stackPtr'EmptyLines'array($lines));
  234.                     if ($fix === true{
  235.                         $phpcsFile->fixer->beginChangeset();
  236.                         $i $stackPtr;
  237.                         while ($tokens[$i]['line'!== $tokens[$next]['line']{
  238.                             $phpcsFile->fixer->replaceToken($i'');
  239.                             $i++;
  240.                         }
  241.  
  242.                         $phpcsFile->fixer->addNewlineBefore($i);
  243.                         $phpcsFile->fixer->endChangeset();
  244.                     }
  245.                 }
  246.             }//end if
  247.         }//end if
  248.  
  249.     }//end process()
  250.  
  251.  
  252. }//end class

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