3
* Squiz_Sniffs_CSS_ShorthandSizeSniff.
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
16
* Squiz_Sniffs_CSS_ShorthandSizeSniff.
18
* Ensure sizes are defined using shorthand notation where possible, except in the
19
* case where shorthand becomes 3 values.
22
* @package PHP_CodeSniffer
23
* @author Greg Sherwood <gsherwood@squiz.net>
24
* @copyright 2006-2014 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: 1.5.5
27
* @link http://pear.php.net/package/PHP_CodeSniffer
29
class Squiz_Sniffs_CSS_ShorthandSizeSniff implements PHP_CodeSniffer_Sniff
33
* A list of tokenizers this sniff supports.
37
public $supportedTokenizers = array('CSS');
40
* A list of styles that we shouldn't check.
42
* These have values that looks like sizes, but are not.
46
public $excludeStyles = array(
47
'background-position',
50
'-webkit-transform-origin',
51
'-ms-transform-origin',
56
* Returns the token types that this sniff is interested in.
60
public function register()
62
return array(T_STYLE);
68
* Processes the tokens that this sniff is interested in.
70
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
71
* @param int $stackPtr The position in the stack where
72
* the token was found.
76
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
78
$tokens = $phpcsFile->getTokens();
80
// Some styles look like shorthand but are not actually a set of 4 sizes.
81
$style = strtolower($tokens[$stackPtr]['content']);
82
if (in_array($style, $this->excludeStyles) === true) {
86
// Get the whole style content.
87
$end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
88
$content = $phpcsFile->getTokensAsString(($stackPtr + 1), ($end - $stackPtr - 1));
89
$content = trim($content, ': ');
91
// Account for a !important annotation.
92
if (substr($content, -10) === '!important') {
93
$content = substr($content, 0, -10);
94
$content = trim($content);
97
// Check if this style value is a set of numbers with optional prefixes.
98
$content = preg_replace('/\s+/', ' ', $content);
100
$num = preg_match_all(
101
'/([0-9]+)([a-zA-Z]{2}\s+|%\s+|\s+)/',
107
// Only interested in styles that have multiple sizes defined.
112
// Rebuild the content we matched to ensure we got everything.
114
foreach ($values as $value) {
115
$matched .= $value[0];
118
if ($content !== trim($matched)) {
123
$expected = trim($content.' '.$values[1][1].$values[1][2]);
124
$error = 'Shorthand syntax not allowed here; use %s instead';
125
$data = array($expected);
126
$phpcsFile->addError($error, $stackPtr, 'NotAllowed', $data);
131
if ($values[0][0] !== $values[1][0]) {
132
// Both values are different, so it is already shorthand.
135
} else if ($values[0][0] !== $values[2][0] || $values[1][0] !== $values[3][0]) {
136
// Can't shorthand this.
140
if ($values[0][0] === $values[1][0]) {
141
// All values are the same.
142
$expected = $values[0][0];
144
$expected = $values[0][0].' '.$values[1][0];
147
$expected = preg_replace('/\s+/', ' ', trim($expected));
149
$error = 'Size definitions must use shorthand if available; expected "%s" but found "%s"';
155
$phpcsFile->addError($error, $stackPtr, 'NotUsed', $data);