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 other than precision 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\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 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 by the tokeniser, the
  84.             // original content should be checked 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 this is an inline HTML token, split the content into
  92.             // indentation whitespace and the actual HTML/text.
  93.             $nonWhitespace '';
  94.             if ($tokens[$i]['code'=== T_INLINE_HTML && preg_match('`^(\s*)(\S.*)`s'$content$matches> 0{
  95.                 if (isset($matches[1]=== true{
  96.                     $content $matches[1];
  97.                 }
  98.  
  99.                 if (isset($matches[2]=== true{
  100.                     $nonWhitespace $matches[2];
  101.                 }
  102.             }
  103.  
  104.             $hasSpaces strpos($content' ');
  105.             $hasTabs   strpos($content"\t");
  106.  
  107.             if ($hasSpaces === false && $hasTabs === false{
  108.                 // Empty line.
  109.                 continue;
  110.             }
  111.  
  112.             if ($hasSpaces === false && $hasTabs !== false{
  113.                 // All ok, nothing to do.
  114.                 $phpcsFile->recordMetric($i'Line indent''tabs');
  115.                 continue;
  116.             }
  117.  
  118.             if ($tokens[$i]['code'=== T_DOC_COMMENT_WHITESPACE && $content === ' '{
  119.                 // Ignore file/class-level DocBlock, especially for recording metrics.
  120.                 continue;
  121.             }
  122.  
  123.             // OK, by now we know there will be spaces.
  124.             // We just don't know yet whether they need to be replaced or
  125.             // are precision indentation, nor whether they are correctly
  126.             // placed at the end of the whitespace.
  127.             $trimmed        str_replace(' '''$content);
  128.             $numSpaces      (strlen($contentstrlen($trimmed));
  129.             $numTabs        = (int) floor($numSpaces $this->tabWidth);
  130.             $tabAfterSpaces strpos($content"\t"$hasSpaces);
  131.  
  132.             if ($hasTabs === false{
  133.                 $phpcsFile->recordMetric($i'Line indent''spaces');
  134.  
  135.                 if ($numTabs === 0{
  136.                     // Ignore: precision indentation.
  137.                     continue;
  138.                 }
  139.             else {
  140.                 if ($numTabs === 0{
  141.                     // Precision indentation.
  142.                     $phpcsFile->recordMetric($i'Line indent''tabs');
  143.  
  144.                     if ($tabAfterSpaces === false{
  145.                         // Ignore: precision indentation is already at the
  146.                         // end of the whitespace.
  147.                         continue;
  148.                     }
  149.                 else {
  150.                     $phpcsFile->recordMetric($i'Line indent''mixed');
  151.                 }
  152.             }//end if
  153.  
  154.             $error 'Tabs must be used to indent lines; spaces are not allowed';
  155.             $fix   $phpcsFile->addFixableError($error$i'SpacesUsed');
  156.             if ($fix === true{
  157.                 $remaining ($numSpaces $this->tabWidth);
  158.                 $padding   str_repeat("\t"$numTabs);
  159.                 $padding  .= str_repeat(' '$remaining);
  160.                 $phpcsFile->fixer->replaceToken($i$trimmed.$padding.$nonWhitespace);
  161.             }
  162.         }//end for
  163.  
  164.         // Ignore the rest of the file.
  165.         return ($phpcsFile->numTokens + 1);
  166.  
  167.     }//end process()
  168.  
  169.  
  170. }//end class

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