Skip to content

Commit a24b29b

Browse files
committed
Merge branch 'master' into report-memory-improvements
2 parents b4c42ec + 651a757 commit a24b29b

File tree

7 files changed

+482
-0
lines changed

7 files changed

+482
-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: 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: 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+
?>

0 commit comments

Comments
 (0)