Skip to content

Commit 651a757

Browse files
committed
Added Squiz NamedColoursSniff to ban the use of colour names
1 parent 186eea3 commit 651a757

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 <[email protected]>
10+
* @copyright 2006-2012 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 <[email protected]>
23+
* @copyright 2006-2012 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+
*/
28+
class 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+
public $colourNames = array(
47+
'aqua',
48+
'black',
49+
'blue',
50+
'fuchsia',
51+
'gray',
52+
'green',
53+
'lime',
54+
'maroon',
55+
'navy',
56+
'olive',
57+
'orange',
58+
'purple',
59+
'red',
60+
'silver',
61+
'teal',
62+
'white',
63+
'yellow',
64+
);
65+
66+
67+
/**
68+
* Returns the token types that this sniff is interested in.
69+
*
70+
* @return array(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 (in_array(strtolower($tokens[$stackPtr]['content']), $this->colourNames) === 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+
}//end class
107+
?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#red {
2+
background-color: red;
3+
}
4+
5+
.red {
6+
border-bottom: 1px dotted black;
7+
border-top: 1px dotted gray;
8+
}
9+
10+
#red.yellow {
11+
background: yellow url(diag_lines_bg.gif) top left;
12+
text-shadow: 0 1px 0 white;
13+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Unit test class for the NamedColours sniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2012 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+
* Unit test class for the NamedColours sniff.
17+
*
18+
* A sniff unit test checks a .inc file for expected violations of a single
19+
* coding standard. Expected errors and warnings are stored in this class.
20+
*
21+
* @category PHP
22+
* @package PHP_CodeSniffer
23+
* @author Greg Sherwood <[email protected]>
24+
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
25+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
26+
* @version Release: @package_version@
27+
* @link http://pear.php.net/package/PHP_CodeSniffer
28+
*/
29+
class Squiz_Tests_CSS_NamedColoursUnitTest extends AbstractSniffUnitTest
30+
{
31+
32+
/**
33+
* Returns the lines where errors should occur.
34+
*
35+
* The key of the array should represent the line number and the value
36+
* should represent the number of errors that should occur on that line.
37+
*
38+
* @return array(int => int)
39+
*/
40+
public function getErrorList()
41+
{
42+
return array(
43+
2 => 1,
44+
6 => 1,
45+
7 => 1,
46+
11 => 1,
47+
12 => 1,
48+
);
49+
50+
}//end getErrorList()
51+
52+
53+
/**
54+
* Returns the lines where warnings should occur.
55+
*
56+
* The key of the array should represent the line number and the value
57+
* should represent the number of warnings that should occur on that line.
58+
*
59+
* @return array(int => int)
60+
*/
61+
public function getWarningList()
62+
{
63+
return array();
64+
65+
}//end getWarningList()
66+
67+
68+
}//end class
69+
70+
?>

package.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
4343
- Added Generic InlineHTMLSniff to ensure a file only contains PHP code
4444
- Added Squiz ShorthandSizeSniff to check that CSS sizes are using shorthand notation only when 1 or 2 values are used
4545
- Added Squiz ForbiddenStylesSniff to ban the use of some deprecated browser-specific styles
46+
- Added Squiz NamedColoursSniff to ban the use of colour names
4647
- PSR2 standard no longer enforces no whitespace between the closing parenthesis of a function call and the semicolon
4748
- PSR2 ClassDeclarationSniff now ignores empty classes when checking the end brace position
4849
- PSR2 SwitchDeclarationSniff no longer reports errors for empty lines between CASE statements
@@ -1464,6 +1465,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
14641465
<file baseinstalldir="PHP" name="MissingColonSniff.php" role="php">
14651466
<tasks:replace from="@package_version@" to="version" type="package-info" />
14661467
</file>
1468+
<file baseinstalldir="PHP" name="NamedColoursSniff.php" role="php">
1469+
<tasks:replace from="@package_version@" to="version" type="package-info" />
1470+
</file>
14671471
<file baseinstalldir="PHP" name="OpacitySniff.php" role="php">
14681472
<tasks:replace from="@package_version@" to="version" type="package-info" />
14691473
</file>
@@ -1849,6 +1853,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
18491853
<file baseinstalldir="PHP" name="MissingColonUnitTest.php" role="test">
18501854
<tasks:replace from="@package_version@" to="version" type="package-info" />
18511855
</file>
1856+
<file baseinstalldir="PHP" name="NamedColoursUnitTest.css" role="test" />
1857+
<file baseinstalldir="PHP" name="NamedColoursUnitTest.php" role="test">
1858+
<tasks:replace from="@package_version@" to="version" type="package-info" />
1859+
</file>
18521860
<file baseinstalldir="PHP" name="OpacityUnitTest.css" role="test" />
18531861
<file baseinstalldir="PHP" name="OpacityUnitTest.php" role="test">
18541862
<tasks:replace from="@package_version@" to="version" type="package-info" />

0 commit comments

Comments
 (0)