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.      * The --tab-width CLI value that is being used.
  31.      *
  32.      * @var integer 
  33.      */
  34.     private $tabWidth = null;
  35.  
  36.  
  37.     /**
  38.      * Returns an array of tokens this test wants to listen for.
  39.      *
  40.      * @return array 
  41.      */
  42.     public function register()
  43.     {
  44.         return array(T_OPEN_TAG);
  45.  
  46.     }//end register()
  47.  
  48.  
  49.     /**
  50.      * Processes this test, when one of its tokens is encountered.
  51.      *
  52.      * @param \PHP_CodeSniffer\Files\File $phpcsFile All the tokens found in the document.
  53.      * @param int                         $stackPtr  The position of the current token in
  54.      *                                                the stack passed in $tokens.
  55.      *
  56.      * @return void 
  57.      */
  58.     public function process(File $phpcsFile$stackPtr)
  59.     {
  60.         if ($this->tabWidth === null{
  61.             if (isset($phpcsFile->config->tabWidth=== false || $phpcsFile->config->tabWidth === 0{
  62.                 // We have no idea how wide tabs are, so assume 4 spaces for metrics.
  63.                 $this->tabWidth = 4;
  64.             else {
  65.                 $this->tabWidth $phpcsFile->config->tabWidth;
  66.             }
  67.         }
  68.  
  69.         $tokens    $phpcsFile->getTokens();
  70.         $error     'Spaces must be used to indent lines; tabs are not allowed';
  71.         $errorCode 'TabsUsed';
  72.  
  73.         $checkTokens = array(
  74.                         T_WHITESPACE             => true,
  75.                         T_INLINE_HTML            => true,
  76.                         T_DOC_COMMENT_WHITESPACE => true,
  77.                         T_DOC_COMMENT_STRING     => true,
  78.                        );
  79.  
  80.         for ($i ($stackPtr + 1)$i $phpcsFile->numTokens; $i++{
  81.             if (isset($checkTokens[$tokens[$i]['code']]=== false{
  82.                 continue;
  83.             }
  84.  
  85.             // If tabs are being converted to spaces by the tokeniser, the
  86.             // original content should be checked instead of the converted content.
  87.             if (isset($tokens[$i]['orig_content']=== true{
  88.                 $content $tokens[$i]['orig_content'];
  89.             else {
  90.                 $content $tokens[$i]['content'];
  91.             }
  92.  
  93.             if ($content === ''{
  94.                 continue;
  95.             }
  96.  
  97.             if ($tokens[$i]['code'=== T_DOC_COMMENT_WHITESPACE && $content === ' '{
  98.                 // Ignore file/class-level DocBlock, especially for recording metrics.
  99.                 continue;
  100.             }
  101.  
  102.             $recordMetrics = true;
  103.             if (isset($tokens[($i + 1)]=== true
  104.                 && $tokens[$i]['line'$tokens[($i + 1)]['line']
  105.             {
  106.                 // Don't record metrics for empty lines.
  107.                 $recordMetrics = false;
  108.             }
  109.  
  110.             $tabFound = false;
  111.             if ($tokens[$i]['column'=== 1{
  112.                 if ($content[0=== "\t"{
  113.                     $tabFound = true;
  114.                     if ($recordMetrics === true{
  115.                         $spacePosition  strpos($content' ');
  116.                         $tabAfterSpaces strpos($content"\t"$spacePosition);
  117.                         if ($spacePosition !== false && $tabAfterSpaces !== false{
  118.                             $phpcsFile->recordMetric($i'Line indent''mixed');
  119.                         else {
  120.                             // Check for use of precision spaces.
  121.                             $trimmed   str_replace(' '''$content);
  122.                             $numSpaces (strlen($contentstrlen($trimmed));
  123.                             $numTabs   = (int) floor($numSpaces $this->tabWidth);
  124.                             if ($numTabs === 0{
  125.                                 $phpcsFile->recordMetric($i'Line indent''tabs');
  126.                             else {
  127.                                 $phpcsFile->recordMetric($i'Line indent''mixed');
  128.                             }
  129.                         }
  130.                     }
  131.                 else if ($content[0=== ' '{
  132.                     if (strpos($content"\t"!== false{
  133.                         if ($recordMetrics === true{
  134.                             $phpcsFile->recordMetric($i'Line indent''mixed');
  135.                         }
  136.  
  137.                         $tabFound = true;
  138.                     else if ($recordMetrics === true{
  139.                         $phpcsFile->recordMetric($i'Line indent''spaces');
  140.                     }
  141.                 }//end if
  142.             else {
  143.                 // Look for tabs so we can report and replace, but don't
  144.                 // record any metrics about them because they aren't
  145.                 // line indent tokens.
  146.                 if (strpos($content"\t"!== false{
  147.                     $tabFound  = true;
  148.                     $error     'Spaces must be used for alignment; tabs are not allowed';
  149.                     $errorCode 'NonIndentTabsUsed';
  150.                 }
  151.             }//end if
  152.  
  153.             if ($tabFound === false{
  154.                 continue;
  155.             }
  156.  
  157.             $fix $phpcsFile->addFixableError($error$i$errorCode);
  158.             if ($fix === true{
  159.                 if (isset($tokens[$i]['orig_content']=== true{
  160.                     // Use the replacement that PHPCS has already done.
  161.                     $phpcsFile->fixer->replaceToken($i$tokens[$i]['content']);
  162.                 else {
  163.                     // Replace tabs with spaces, using an indent of 4 spaces.
  164.                     // Other sniffs can then correct the indent if they need to.
  165.                     $newContent str_replace("\t"'    '$tokens[$i]['content']);
  166.                     $phpcsFile->fixer->replaceToken($i$newContent);
  167.                 }
  168.             }
  169.         }//end for
  170.  
  171.         // Ignore the rest of the file.
  172.         return ($phpcsFile->numTokens + 1);
  173.  
  174.     }//end process()
  175.  
  176.  
  177. }//end class

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