Skip to content

Commit 186eea3

Browse files
committed
Added Squiz ForbiddenStylesSniff to ban the use of some deprecated browser-specific styles
1 parent 34528cf commit 186eea3

File tree

4 files changed

+284
-0
lines changed

4 files changed

+284
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
/**
3+
* Squiz_Sniffs_CSS_ForbiddenStylesSniff.
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_ForbiddenStylesSniff.
17+
*
18+
* Bans the use of some styles, such as deprecated or browser-specific styles.
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_ForbiddenStylesSniff 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+
* A list of forbidden styles with their alternatives.
40+
*
41+
* The value is NULL if no alternative exists. i.e., the
42+
* function should just not be used.
43+
*
44+
* @var array(string => string|null)
45+
*/
46+
protected $forbiddenStyles = array(
47+
'-moz-border-radius' => 'border-radius',
48+
'-webkit-border-radius' => 'border-radius',
49+
'-moz-border-radius-topleft' => 'border-top-left-radius',
50+
'-moz-border-radius-topright' => 'border-top-right-radius',
51+
'-moz-border-radius-bottomright' => 'border-bottom-right-radius',
52+
'-moz-border-radius-bottomleft' => 'border-bottom-left-radius',
53+
'-moz-box-shadow' => 'box-shadow',
54+
'-webkit-box-shadow' => 'box-shadow',
55+
);
56+
57+
/**
58+
* A cache of forbidden style names, for faster lookups.
59+
*
60+
* @var array(string)
61+
*/
62+
protected $forbiddenStyleNames = array();
63+
64+
/**
65+
* If true, forbidden styles will be considered regular expressions.
66+
*
67+
* @var bool
68+
*/
69+
protected $patternMatch = false;
70+
71+
/**
72+
* If true, an error will be thrown; otherwise a warning.
73+
*
74+
* @var bool
75+
*/
76+
public $error = true;
77+
78+
79+
/**
80+
* Returns an array of tokens this test wants to listen for.
81+
*
82+
* @return array
83+
*/
84+
public function register()
85+
{
86+
$this->forbiddenStyleNames = array_keys($this->forbiddenStyles);
87+
88+
if ($this->patternMatch === true) {
89+
foreach ($this->forbiddenStyleNames as $i => $name) {
90+
$this->forbiddenStyleNames[$i] = '/'.$name.'/i';
91+
}
92+
}
93+
94+
return array(T_STYLE);
95+
96+
}//end register()
97+
98+
99+
/**
100+
* Processes this test, when one of its tokens is encountered.
101+
*
102+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
103+
* @param int $stackPtr The position of the current token in
104+
* the stack passed in $tokens.
105+
*
106+
* @return void
107+
*/
108+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
109+
{
110+
$tokens = $phpcsFile->getTokens();
111+
$style = strtolower($tokens[$stackPtr]['content']);
112+
$pattern = null;
113+
114+
if ($this->patternMatch === true) {
115+
$count = 0;
116+
$pattern = preg_replace(
117+
$this->forbiddenStyleNames,
118+
$this->forbiddenStyleNames,
119+
$style,
120+
1,
121+
$count
122+
);
123+
124+
if ($count === 0) {
125+
return;
126+
}
127+
128+
// Remove the pattern delimiters and modifier.
129+
$pattern = substr($pattern, 1, -2);
130+
} else {
131+
if (in_array($style, $this->forbiddenStyleNames) === false) {
132+
return;
133+
}
134+
}
135+
136+
$this->addError($phpcsFile, $stackPtr, $style, $pattern);
137+
138+
}//end process()
139+
140+
141+
/**
142+
* Generates the error or warning for this sniff.
143+
*
144+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
145+
* @param int $stackPtr The position of the forbidden style
146+
* in the token array.
147+
* @param string $style The name of the forbidden style.
148+
* @param string $pattern The pattern used for the match.
149+
*
150+
* @return void
151+
*/
152+
protected function addError($phpcsFile, $stackPtr, $style, $pattern=null)
153+
{
154+
$data = array($style);
155+
$error = 'The use of style %s is ';
156+
if ($this->error === true) {
157+
$type = 'Found';
158+
$error .= 'forbidden';
159+
} else {
160+
$type = 'Discouraged';
161+
$error .= 'discouraged';
162+
}
163+
164+
if ($pattern === null) {
165+
$pattern = $style;
166+
}
167+
168+
if ($this->forbiddenStyles[$pattern] !== null) {
169+
$type .= 'WithAlternative';
170+
$data[] = $this->forbiddenStyles[$pattern];
171+
$error .= '; use %s instead';
172+
}
173+
174+
if ($this->error === true) {
175+
$phpcsFile->addError($error, $stackPtr, $type, $data);
176+
} else {
177+
$phpcsFile->addWarning($error, $stackPtr, $type, $data);
178+
}
179+
180+
}//end addError()
181+
182+
183+
}//end class
184+
185+
?>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#add-new-comment {
2+
-moz-border-radius: 1px;
3+
-webkit-border-radius: 1px;
4+
border-radius: 1px;
5+
6+
-moz-border-radius-topleft: 1px;
7+
-moz-border-radius-topright: 1px;
8+
-moz-border-radius-bottomright: 1px;
9+
-moz-border-radius-bottomleft: 1px;
10+
border-top-left-radius: 1px;
11+
border-top-right-radius: 1px;
12+
border-bottom-right-radius: 1px;
13+
border-bottom-left-radius: 1px;
14+
15+
-moz-box-shadow: 1px;
16+
-webkit-box-shadow: 1px;
17+
box-shadow: 1px;
18+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Unit test class for the ForbiddenStyles 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 ForbiddenStyles 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_ForbiddenStylesUnitTest 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+
3 => 1,
45+
6 => 1,
46+
7 => 1,
47+
8 => 1,
48+
9 => 1,
49+
15 => 1,
50+
16 => 1,
51+
);
52+
53+
}//end getErrorList()
54+
55+
56+
/**
57+
* Returns the lines where warnings should occur.
58+
*
59+
* The key of the array should represent the line number and the value
60+
* should represent the number of warnings that should occur on that line.
61+
*
62+
* @return array(int => int)
63+
*/
64+
public function getWarningList()
65+
{
66+
return array();
67+
68+
}//end getWarningList()
69+
70+
71+
}//end class
72+
73+
?>

package.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
4242
- Generated HTML docs now correctly show the open PHP tag in code comparison blocks
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
45+
- Added Squiz ForbiddenStylesSniff to ban the use of some deprecated browser-specific styles
4546
- PSR2 standard no longer enforces no whitespace between the closing parenthesis of a function call and the semicolon
4647
- PSR2 ClassDeclarationSniff now ignores empty classes when checking the end brace position
4748
- PSR2 SwitchDeclarationSniff no longer reports errors for empty lines between CASE statements
@@ -1451,6 +1452,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
14511452
<file baseinstalldir="PHP" name="EmptyStyleDefinitionSniff.php" role="php">
14521453
<tasks:replace from="@package_version@" to="version" type="package-info" />
14531454
</file>
1455+
<file baseinstalldir="PHP" name="ForbiddenStylesSniff.php" role="php">
1456+
<tasks:replace from="@package_version@" to="version" type="package-info" />
1457+
</file>
14541458
<file baseinstalldir="PHP" name="IndentationSniff.php" role="php">
14551459
<tasks:replace from="@package_version@" to="version" type="package-info" />
14561460
</file>
@@ -1829,6 +1833,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
18291833
<file baseinstalldir="PHP" name="EmptyStyleDefinitionUnitTest.php" role="test">
18301834
<tasks:replace from="@package_version@" to="version" type="package-info" />
18311835
</file>
1836+
<file baseinstalldir="PHP" name="ForbiddenStylesUnitTest.css" role="test" />
1837+
<file baseinstalldir="PHP" name="ForbiddenStylesUnitTest.php" role="test">
1838+
<tasks:replace from="@package_version@" to="version" type="package-info" />
1839+
</file>
18321840
<file baseinstalldir="PHP" name="IndentationUnitTest.css" role="test" />
18331841
<file baseinstalldir="PHP" name="IndentationUnitTest.php" role="test">
18341842
<tasks:replace from="@package_version@" to="version" type="package-info" />

0 commit comments

Comments
 (0)