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

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