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

Source for file DisallowSpaceIndentSniff.php

Documentation is available at DisallowSpaceIndentSniff.php

  1. <?php
  2. /**
  3.  * Throws errors if spaces 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 DisallowSpaceIndentSniff 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_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 fixing.
  63.                 // It shouldn't really matter because indent checks elsewhere in the
  64.                 // standard should fix things up.
  65.                 $this->tabWidth = 4;
  66.             else {
  67.                 $this->tabWidth $phpcsFile->config->tabWidth;
  68.             }
  69.         }
  70.  
  71.         $checkTokens = array(
  72.                         T_WHITESPACE             => true,
  73.                         T_INLINE_HTML            => true,
  74.                         T_DOC_COMMENT_WHITESPACE => true,
  75.                        );
  76.  
  77.         $tokens $phpcsFile->getTokens();
  78.         for ($i ($stackPtr + 1)$i $phpcsFile->numTokens; $i++{
  79.             if ($tokens[$i]['column'!== 1 || isset($checkTokens[$tokens[$i]['code']]=== false{
  80.                 continue;
  81.             }
  82.  
  83.             // If tabs are being converted to spaces, the original content
  84.             // should be used instead of the converted content.
  85.             if (isset($tokens[$i]['orig_content']=== true{
  86.                 $content $tokens[$i]['orig_content'];
  87.             else {
  88.                 $content $tokens[$i]['content'];
  89.             }
  90.  
  91.             if ($content[0=== ' '{
  92.                 if ($tokens[$i]['code'=== T_DOC_COMMENT_WHITESPACE && $content === ' '{
  93.                     // Ignore file/class-level DocBlock.
  94.                     continue;
  95.                 }
  96.  
  97.                 // Space are considered ok if they are proceeded by tabs and not followed
  98.                 // by tabs, as is the case with standard docblock comments.
  99.                 $phpcsFile->recordMetric($i'Line indent''spaces');
  100.                 $error 'Tabs must be used to indent lines; spaces are not allowed';
  101.                 $fix   $phpcsFile->addFixableError($error$i'SpacesUsed');
  102.                 if ($fix === true{
  103.                     $trimmed   ltrim($content' ');
  104.                     $numSpaces (strlen($contentstrlen($trimmed));
  105.                     if ($numSpaces $this->tabWidth{
  106.                         $numTabs = 1;
  107.                         $padding "\t";
  108.                     else {
  109.                         $numTabs   floor($numSpaces $this->tabWidth);
  110.                         $remaining ($numSpaces ($numTabs $this->tabWidth));
  111.                         $padding   str_repeat("\t"$numTabs).$padding str_repeat(' '$remaining);
  112.                     }
  113.  
  114.                     $phpcsFile->fixer->replaceToken($i$padding.$trimmed);
  115.                 }
  116.             else if ($content[0=== "\t"{
  117.                 $phpcsFile->recordMetric($i'Line indent''tabs');
  118.             }//end if
  119.         }//end for
  120.  
  121.         // Ignore the rest of the file.
  122.         return ($phpcsFile->numTokens + 1);
  123.  
  124.     }//end process()
  125.  
  126.  
  127. }//end class

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