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

Source for file NamedColoursSniff.php

Documentation is available at NamedColoursSniff.php

  1. <?php
  2. /**
  3.  * Ensure colour names are not used.
  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 NamedColoursSniff 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.      * A list of named colours.
  28.      *
  29.      * This is the list of standard colours defined in the CSS spec.
  30.      *
  31.      * @var array 
  32.      */
  33.     protected $colourNames = array(
  34.                               'aqua'    => 'aqua',
  35.                               'black'   => 'black',
  36.                               'blue'    => 'blue',
  37.                               'fuchsia' => 'fuchsia',
  38.                               'gray'    => 'gray',
  39.                               'green'   => 'green',
  40.                               'lime'    => 'lime',
  41.                               'maroon'  => 'maroon',
  42.                               'navy'    => 'navy',
  43.                               'olive'   => 'olive',
  44.                               'orange'  => 'orange',
  45.                               'purple'  => 'purple',
  46.                               'red'     => 'red',
  47.                               'silver'  => 'silver',
  48.                               'teal'    => 'teal',
  49.                               'white'   => 'white',
  50.                               'yellow'  => 'yellow',
  51.                              );
  52.  
  53.  
  54.     /**
  55.      * Returns the token types that this sniff is interested in.
  56.      *
  57.      * @return int[] 
  58.      */
  59.     public function register()
  60.     {
  61.         return array(T_STRING);
  62.  
  63.     }//end register()
  64.  
  65.  
  66.     /**
  67.      * Processes the tokens that this sniff is interested in.
  68.      *
  69.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
  70.      * @param int                         $stackPtr  The position in the stack where
  71.      *                                                the token was found.
  72.      *
  73.      * @return void 
  74.      */
  75.     public function process(File $phpcsFile$stackPtr)
  76.     {
  77.         $tokens $phpcsFile->getTokens();
  78.  
  79.         if ($tokens[($stackPtr - 1)]['code'=== T_HASH
  80.             || $tokens[($stackPtr - 1)]['code'=== T_STRING_CONCAT
  81.         {
  82.             // Class name.
  83.             return;
  84.         }
  85.  
  86.         if (isset($this->colourNames[strtolower($tokens[$stackPtr]['content'])]=== true{
  87.             $error 'Named colours are forbidden; use hex, rgb, or rgba values instead';
  88.             $phpcsFile->addError($error$stackPtr'Forbidden');
  89.         }
  90.  
  91.     }//end process()
  92.  
  93.  
  94. }//end class

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