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

Source for file DisallowTabIndentSniff.php

Documentation is available at DisallowTabIndentSniff.php

  1. <?php
  2. /**
  3.  * Throws errors if tabs are used for indentation.
  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\Generic\Sniffs\WhiteSpace;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class DisallowTabIndentSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * A list of tokenizers this sniff supports.
  20.      *
  21.      * @var array 
  22.      */
  23.     public $supportedTokenizers = array(
  24.                                    'PHP',
  25.                                    'JS',
  26.                                    'CSS',
  27.                                   );
  28.  
  29.  
  30.     /**
  31.      * Returns an array of tokens this test wants to listen for.
  32.      *
  33.      * @return array 
  34.      */
  35.     public function register()
  36.     {
  37.         return array(T_OPEN_TAG);
  38.  
  39.     }//end register()
  40.  
  41.  
  42.     /**
  43.      * Processes this test, when one of its tokens is encountered.
  44.      *
  45.      * @param \PHP_CodeSniffer\Files\File $phpcsFile All the tokens found in the document.
  46.      * @param int                         $stackPtr  The position of the current token in
  47.      *                                                the stack passed in $tokens.
  48.      *
  49.      * @return void 
  50.      */
  51.     public function process(File $phpcsFile$stackPtr)
  52.     {
  53.         $tokens    $phpcsFile->getTokens();
  54.         $error     'Spaces must be used to indent lines; tabs are not allowed';
  55.         $errorCode 'TabsUsed';
  56.  
  57.         $checkTokens = array(
  58.                         T_WHITESPACE             => true,
  59.                         T_INLINE_HTML            => true,
  60.                         T_DOC_COMMENT_WHITESPACE => true,
  61.                         T_DOC_COMMENT_STRING     => true,
  62.                        );
  63.  
  64.         for ($i ($stackPtr + 1)$i $phpcsFile->numTokens; $i++{
  65.             if (isset($checkTokens[$tokens[$i]['code']]=== false{
  66.                 continue;
  67.             }
  68.  
  69.             // If tabs are being converted to spaces by the tokeniser, the
  70.             // original content should be checked instead of the converted content.
  71.             if (isset($tokens[$i]['orig_content']=== true{
  72.                 $content $tokens[$i]['orig_content'];
  73.             else {
  74.                 $content $tokens[$i]['content'];
  75.             }
  76.  
  77.             if ($content === ''{
  78.                 continue;
  79.             }
  80.  
  81.             if ($tokens[$i]['code'=== T_DOC_COMMENT_WHITESPACE && $content === ' '{
  82.                 // Ignore file/class-level DocBlock, especially for recording metrics.
  83.                 continue;
  84.             }
  85.  
  86.             $tabFound = false;
  87.             if ($tokens[$i]['column'=== 1{
  88.                 if ($content[0=== "\t"{
  89.                     $phpcsFile->recordMetric($i'Line indent''tabs');
  90.                     $tabFound = true;
  91.                 else if ($content[0=== ' '{
  92.                     if (strpos($content"\t"!== false{
  93.                         $phpcsFile->recordMetric($i'Line indent''mixed');
  94.                         $tabFound = true;
  95.                     else {
  96.                         $phpcsFile->recordMetric($i'Line indent''spaces');
  97.                     }
  98.                 }
  99.             else {
  100.                 // Look for tabs so we can report and replace, but don't
  101.                 // record any metrics about them because they aren't
  102.                 // line indent tokens.
  103.                 if (strpos($content"\t"!== false{
  104.                     $tabFound  = true;
  105.                     $error     'Spaces must be used for alignment; tabs are not allowed';
  106.                     $errorCode 'NonIndentTabsUsed';
  107.                 }
  108.             }//end if
  109.  
  110.             if ($tabFound === false{
  111.                 continue;
  112.             }
  113.  
  114.             $fix $phpcsFile->addFixableError($error$i$errorCode);
  115.             if ($fix === true{
  116.                 if (isset($tokens[$i]['orig_content']=== true{
  117.                     // Use the replacement that PHPCS has already done.
  118.                     $phpcsFile->fixer->replaceToken($i$tokens[$i]['content']);
  119.                 else {
  120.                     // Replace tabs with spaces, using an indent of 4 spaces.
  121.                     // Other sniffs can then correct the indent if they need to.
  122.                     $newContent str_replace("\t"'    '$tokens[$i]['content']);
  123.                     $phpcsFile->fixer->replaceToken($i$newContent);
  124.                 }
  125.             }
  126.         }//end for
  127.  
  128.         // Ignore the rest of the file.
  129.         return ($phpcsFile->numTokens + 1);
  130.  
  131.     }//end process()
  132.  
  133.  
  134. }//end class

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