1<?php
2/**
3 * Squiz_Sniffs_CSS_NamedColoursSniff.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Greg Sherwood <gsherwood@squiz.net>
10 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12 * @link      http://pear.php.net/package/PHP_CodeSniffer
13 */
14
15/**
16 * Squiz_Sniffs_CSS_NamedColoursSniff.
17 *
18 * Ensure colour names are not used.
19 *
20 * @category  PHP
21 * @package   PHP_CodeSniffer
22 * @author    Greg Sherwood <gsherwood@squiz.net>
23 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
24 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25 * @version   Release: @package_version@
26 * @link      http://pear.php.net/package/PHP_CodeSniffer
27 */
28class Squiz_Sniffs_CSS_NamedColoursSniff implements PHP_CodeSniffer_Sniff
29{
30
31    /**
32     * A list of tokenizers this sniff supports.
33     *
34     * @var array
35     */
36    public $supportedTokenizers = array('CSS');
37
38
39    /**
40     * A list of named colours.
41     *
42     * This is the list of standard colours defined in the CSS spec.
43     *
44     * @var array
45     */
46    protected $colourNames = array(
47                              'aqua'    => 'aqua',
48                              'black'   => 'black',
49                              'blue'    => 'blue',
50                              'fuchsia' => 'fuchsia',
51                              'gray'    => 'gray',
52                              'green'   => 'green',
53                              'lime'    => 'lime',
54                              'maroon'  => 'maroon',
55                              'navy'    => 'navy',
56                              'olive'   => 'olive',
57                              'orange'  => 'orange',
58                              'purple'  => 'purple',
59                              'red'     => 'red',
60                              'silver'  => 'silver',
61                              'teal'    => 'teal',
62                              'white'   => 'white',
63                              'yellow'  => 'yellow',
64                             );
65
66
67    /**
68     * Returns the token types that this sniff is interested in.
69     *
70     * @return int[]
71     */
72    public function register()
73    {
74        return array(T_STRING);
75
76    }//end register()
77
78
79    /**
80     * Processes the tokens that this sniff is interested in.
81     *
82     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
83     * @param int                  $stackPtr  The position in the stack where
84     *                                        the token was found.
85     *
86     * @return void
87     */
88    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
89    {
90        $tokens = $phpcsFile->getTokens();
91
92        if ($tokens[($stackPtr - 1)]['code'] === T_HASH
93            || $tokens[($stackPtr - 1)]['code'] === T_STRING_CONCAT
94        ) {
95            // Class name.
96            return;
97        }
98
99        if (isset($this->colourNames[strtolower($tokens[$stackPtr]['content'])]) === true) {
100            $error = 'Named colours are forbidden; use hex, rgb, or rgba values instead';
101            $phpcsFile->addError($error, $stackPtr, 'Forbidden');
102        }
103
104    }//end process()
105
106
107}//end class
108