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

Source for file IndentationSniff.php

Documentation is available at IndentationSniff.php

  1. <?php
  2. /**
  3.  * Ensures styles are indented 4 spaces.
  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\Squiz\Sniffs\CSS;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class IndentationSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * A list of tokenizers this sniff supports.
  20.      *
  21.      * @var array 
  22.      */
  23.     public $supportedTokenizers = array('CSS');
  24.  
  25.     /**
  26.      * The number of spaces code should be indented.
  27.      *
  28.      * @var integer 
  29.      */
  30.     public $indent = 4;
  31.  
  32.  
  33.     /**
  34.      * Returns the token types that this sniff is interested in.
  35.      *
  36.      * @return int[] 
  37.      */
  38.     public function register()
  39.     {
  40.         return array(T_OPEN_TAG);
  41.  
  42.     }//end register()
  43.  
  44.  
  45.     /**
  46.      * Processes the tokens that this sniff is interested in.
  47.      *
  48.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
  49.      * @param int                         $stackPtr  The position in the stack where
  50.      *                                                the token was found.
  51.      *
  52.      * @return void 
  53.      */
  54.     public function process(File $phpcsFile$stackPtr)
  55.     {
  56.         $tokens $phpcsFile->getTokens();
  57.  
  58.         $numTokens    (count($tokens- 2);
  59.         $indentLevel  = 0;
  60.         $nestingLevel = 0;
  61.         for ($i = 1; $i $numTokens$i++{
  62.             if ($tokens[$i]['code'=== T_COMMENT{
  63.                 // Don't check the indent of comments.
  64.                 continue;
  65.             }
  66.  
  67.             if ($tokens[$i]['code'=== T_OPEN_CURLY_BRACKET{
  68.                 $indentLevel++;
  69.  
  70.                 // Check for nested class definitions.
  71.                 $found $phpcsFile->findNext(
  72.                     T_OPEN_CURLY_BRACKET,
  73.                     ($i + 1),
  74.                     $tokens[$i]['bracket_closer']
  75.                 );
  76.  
  77.                 if ($found !== false{
  78.                     $nestingLevel $indentLevel;
  79.                 }
  80.             }
  81.  
  82.             if (($tokens[$i]['code'=== T_CLOSE_CURLY_BRACKET
  83.                 && $tokens[$i]['line'!== $tokens[($i - 1)]['line'])
  84.                 || ($tokens[($i + 1)]['code'=== T_CLOSE_CURLY_BRACKET
  85.                 && $tokens[$i]['line'=== $tokens[($i + 1)]['line'])
  86.             {
  87.                 $indentLevel--;
  88.                 if ($indentLevel === 0{
  89.                     $nestingLevel = 0;
  90.                 }
  91.             }
  92.  
  93.             if ($tokens[$i]['column'!== 1
  94.                 || $tokens[$i]['code'=== T_OPEN_CURLY_BRACKET
  95.                 || $tokens[$i]['code'=== T_CLOSE_CURLY_BRACKET
  96.             {
  97.                 continue;
  98.             }
  99.  
  100.             // We started a new line, so check indent.
  101.             if ($tokens[$i]['code'=== T_WHITESPACE{
  102.                 $content     str_replace($phpcsFile->eolChar''$tokens[$i]['content']);
  103.                 $foundIndent strlen($content);
  104.             else {
  105.                 $foundIndent = 0;
  106.             }
  107.  
  108.             $expectedIndent ($indentLevel $this->indent);
  109.             if ($expectedIndent > 0
  110.                 && strpos($tokens[$i]['content']$phpcsFile->eolChar!== false
  111.             {
  112.                 if ($nestingLevel !== $indentLevel{
  113.                     $error 'Blank lines are not allowed in class definitions';
  114.                     $fix   $phpcsFile->addFixableError($error$i'BlankLine');
  115.                     if ($fix === true{
  116.                         $phpcsFile->fixer->replaceToken($i'');
  117.                     }
  118.                 }
  119.             else if ($foundIndent !== $expectedIndent{
  120.                 $error 'Line indented incorrectly; expected %s spaces, found %s';
  121.                 $data  = array(
  122.                           $expectedIndent,
  123.                           $foundIndent,
  124.                          );
  125.  
  126.                 $fix $phpcsFile->addFixableError($error$i'Incorrect'$data);
  127.                 if ($fix === true{
  128.                     $indent str_repeat(' '$expectedIndent);
  129.                     if ($foundIndent === 0{
  130.                         $phpcsFile->fixer->addContentBefore($i$indent);
  131.                     else {
  132.                         $phpcsFile->fixer->replaceToken($i$indent);
  133.                     }
  134.                 }
  135.             }//end if
  136.         }//end for
  137.  
  138.     }//end process()
  139.  
  140.  
  141. }//end class

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