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

Source for file SemicolonSpacingSniff.php

Documentation is available at SemicolonSpacingSniff.php

  1. <?php
  2. /**
  3.  * Ensure each style definition has a semi-colon and it is spaced correctly.
  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 SemicolonSpacingSniff 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.     /**
  27.      * Returns the token types that this sniff is interested in.
  28.      *
  29.      * @return int[] 
  30.      */
  31.     public function register()
  32.     {
  33.         return array(T_STYLE);
  34.  
  35.     }//end register()
  36.  
  37.  
  38.     /**
  39.      * Processes the tokens that this sniff is interested in.
  40.      *
  41.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
  42.      * @param int                         $stackPtr  The position in the stack where
  43.      *                                                the token was found.
  44.      *
  45.      * @return void 
  46.      */
  47.     public function process(File $phpcsFile$stackPtr)
  48.     {
  49.         $tokens $phpcsFile->getTokens();
  50.  
  51.         $semicolon $phpcsFile->findNext(T_SEMICOLON($stackPtr + 1));
  52.         if ($semicolon === false || $tokens[$semicolon]['line'!== $tokens[$stackPtr]['line']{
  53.             $error 'Style definitions must end with a semicolon';
  54.             $phpcsFile->addError($error$stackPtr'NotAtEnd');
  55.             return;
  56.         }
  57.  
  58.         if ($tokens[($semicolon - 1)]['code'=== T_WHITESPACE{
  59.             $length strlen($tokens[($semicolon - 1)]['content']);
  60.             $error  'Expected 0 spaces before semicolon in style definition; %s found';
  61.             $data   = array($length);
  62.             $fix    $phpcsFile->addFixableError($error$stackPtr'SpaceFound'$data);
  63.             if ($fix === true{
  64.                 $phpcsFile->fixer->replaceToken(($semicolon - 1)'');
  65.             }
  66.         }
  67.  
  68.     }//end process()
  69.  
  70.  
  71. }//end class

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