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

Source for file ColonSpacingSniff.php

Documentation is available at ColonSpacingSniff.php

  1. <?php
  2. /**
  3.  * Ensure there is no space before a colon and one space after it.
  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. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class ColonSpacingSniff implements Sniff
  17. {
  18.  
  19.     /**
  20.      * A list of tokenizers this sniff supports.
  21.      *
  22.      * @var array 
  23.      */
  24.     public $supportedTokenizers = array('CSS');
  25.  
  26.  
  27.     /**
  28.      * Returns the token types that this sniff is interested in.
  29.      *
  30.      * @return int[] 
  31.      */
  32.     public function register()
  33.     {
  34.         return array(T_COLON);
  35.  
  36.     }//end register()
  37.  
  38.  
  39.     /**
  40.      * Processes the tokens that this sniff is interested in.
  41.      *
  42.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
  43.      * @param int                         $stackPtr  The position in the stack where
  44.      *                                                the token was found.
  45.      *
  46.      * @return void 
  47.      */
  48.     public function process(File $phpcsFile$stackPtr)
  49.     {
  50.         $tokens $phpcsFile->getTokens();
  51.  
  52.         $prev $phpcsFile->findPrevious(Tokens::$emptyTokens($stackPtr - 1)nulltrue);
  53.         if ($tokens[$prev]['code'!== T_STYLE{
  54.             // The colon is not part of a style definition.
  55.             return;
  56.         }
  57.  
  58.         if ($tokens[$prev]['content'=== 'progid'{
  59.             // Special case for IE filters.
  60.             return;
  61.         }
  62.  
  63.         if ($tokens[($stackPtr - 1)]['code'=== T_WHITESPACE{
  64.             $error 'There must be no space before a colon in a style definition';
  65.             $fix   $phpcsFile->addFixableError($error$stackPtr'Before');
  66.             if ($fix === true{
  67.                 $phpcsFile->fixer->replaceToken(($stackPtr - 1)'');
  68.             }
  69.         }
  70.  
  71.         if ($tokens[($stackPtr + 1)]['code'=== T_SEMICOLON{
  72.             // Empty style definition, ignore it.
  73.             return;
  74.         }
  75.  
  76.         if ($tokens[($stackPtr + 1)]['code'!== T_WHITESPACE{
  77.             $error 'Expected 1 space after colon in style definition; 0 found';
  78.             $fix   $phpcsFile->addFixableError($error$stackPtr'NoneAfter');
  79.             if ($fix === true{
  80.                 $phpcsFile->fixer->addContent($stackPtr' ');
  81.             }
  82.         else {
  83.             $content $tokens[($stackPtr + 1)]['content'];
  84.             if (strpos($content$phpcsFile->eolChar=== false{
  85.                 $length strlen($content);
  86.                 if ($length !== 1{
  87.                     $error 'Expected 1 space after colon in style definition; %s found';
  88.                     $data  = array($length);
  89.                     $fix   $phpcsFile->addFixableError($error$stackPtr'After'$data);
  90.                     if ($fix === true{
  91.                         $phpcsFile->fixer->replaceToken(($stackPtr + 1)' ');
  92.                     }
  93.                 }
  94.             else {
  95.                 $error 'Expected 1 space after colon in style definition; newline found';
  96.                 $fix   $phpcsFile->addFixableError($error$stackPtr'AfterNewline');
  97.                 if ($fix === true{
  98.                     $phpcsFile->fixer->replaceToken(($stackPtr + 1)' ');
  99.                 }
  100.             }
  101.         }//end if
  102.  
  103.     }//end process()
  104.  
  105.  
  106. }//end class

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