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

Source for file OpacitySniff.php

Documentation is available at OpacitySniff.php

  1. <?php
  2. /**
  3.  * Ensure that opacity values start with a 0 if it is not a whole number.
  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 OpacitySniff 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.         if ($tokens[$stackPtr]['content'!== 'opacity'{
  52.             return;
  53.         }
  54.  
  55.         $next $phpcsFile->findNext(array(T_COLONT_WHITESPACE)($stackPtr + 1)nulltrue);
  56.  
  57.         if ($next === false
  58.             || ($tokens[$next]['code'!== T_DNUMBER
  59.             && $tokens[$next]['code'!== T_LNUMBER)
  60.         {
  61.             return;
  62.         }
  63.  
  64.         $value $tokens[$next]['content'];
  65.         if ($tokens[$next]['code'=== T_LNUMBER{
  66.             if ($value !== '0' && $value !== '1'{
  67.                 $error 'Opacity values must be between 0 and 1';
  68.                 $phpcsFile->addError($error$next'Invalid');
  69.             }
  70.         else {
  71.             if (strlen($value> 3{
  72.                 $error 'Opacity values must have a single value after the decimal point';
  73.                 $phpcsFile->addError($error$next'DecimalPrecision');
  74.             else if ($value === '0.0' || $value === '1.0'{
  75.                 $error 'Opacity value does not require decimal point; use %s instead';
  76.                 $data  = array($value{0});
  77.                 $fix   $phpcsFile->addFixableError($error$next'PointNotRequired'$data);
  78.                 if ($fix === true{
  79.                     $phpcsFile->fixer->replaceToken($next$value{0});
  80.                 }
  81.             else if ($value{0=== '.'{
  82.                 $error 'Opacity values must not start with a decimal point; use 0%s instead';
  83.                 $data  = array($value);
  84.                 $fix   $phpcsFile->addFixableError($error$next'StartWithPoint'$data);
  85.                 if ($fix === true{
  86.                     $phpcsFile->fixer->replaceToken($next'0'.$value);
  87.                 }
  88.             else if ($value{0!== '0'{
  89.                 $error 'Opacity values must be between 0 and 1';
  90.                 $phpcsFile->addError($error$next'Invalid');
  91.             }//end if
  92.         }//end if
  93.  
  94.     }//end process()
  95.  
  96.  
  97. }//end class

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