Source for file ShorthandSizeSniff.php
Documentation is available at ShorthandSizeSniff.php
* Ensure sizes are defined using shorthand notation where possible.
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\CSS;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
class ShorthandSizeSniff implements Sniff
* A list of tokenizers this sniff supports.
public $supportedTokenizers = array ('CSS');
* A list of styles that we shouldn't check.
* These have values that looks like sizes, but are not.
protected $excludeStyles = array (
'background-position' => 'background-position',
'box-shadow' => 'box-shadow',
'transform-origin' => 'transform-origin',
'-webkit-transform-origin' => '-webkit-transform-origin',
'-ms-transform-origin' => '-ms-transform-origin',
* Returns the token types that this sniff is interested in.
public function register ()
* Processes the tokens that this sniff is interested in.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
* @param int $stackPtr The position in the stack where
public function process (File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens ();
// Some styles look like shorthand but are not actually a set of 4 sizes.
$style = strtolower($tokens[$stackPtr]['content']);
if (isset ($this->excludeStyles[$style]) === true ) {
// Get the whole style content.
$end = $phpcsFile->findNext (T_SEMICOLON, ($stackPtr + 1 ));
$origContent = $phpcsFile->getTokensAsString (($stackPtr + 1 ), ($end - $stackPtr - 1 ));
$origContent = trim($origContent, ': ');
// Account for a !important annotation.
if (substr($content, -10 ) === '!important') {
$content = substr($content, 0 , -10 );
$content = trim($content);
// Check if this style value is a set of numbers with optional prefixes.
'/([0-9]+)([a-zA-Z]{2}\s+|%\s+|\s+)/',
// Only interested in styles that have multiple sizes defined.
// Rebuild the content we matched to ensure we got everything.
foreach ($values as $value) {
if ($content !== trim($matched)) {
$expected = trim($content. ' '. $values[1 ][1 ]. $values[1 ][2 ]);
$error = 'Shorthand syntax not allowed here; use %s instead';
$data = array ($expected);
$fix = $phpcsFile->addFixableError ($error, $stackPtr, 'NotAllowed', $data);
$phpcsFile->fixer ->beginChangeset ();
if (substr($origContent, -10 ) === '!important') {
$expected .= ' !important';
$next = $phpcsFile->findNext (T_WHITESPACE , ($stackPtr + 2 ), null , true );
$phpcsFile->fixer ->replaceToken ($next, $expected);
for ($next++; $next < $end; $next++ ) {
$phpcsFile->fixer ->replaceToken ($next, '');
$phpcsFile->fixer ->endChangeset ();
if ($values[0 ][0 ] !== $values[1 ][0 ]) {
// Both values are different, so it is already shorthand.
} else if ($values[0 ][0 ] !== $values[2 ][0 ] || $values[1 ][0 ] !== $values[3 ][0 ]) {
if ($values[0 ][0 ] === $values[1 ][0 ]) {
// All values are the same.
$expected = $values[0 ][0 ];
$expected = $values[0 ][0 ]. ' '. $values[1 ][0 ];
$error = 'Size definitions must use shorthand if available; expected "%s" but found "%s"';
$fix = $phpcsFile->addFixableError ($error, $stackPtr, 'NotUsed', $data);
$phpcsFile->fixer ->beginChangeset ();
if (substr($origContent, -10 ) === '!important') {
$expected .= ' !important';
$next = $phpcsFile->findNext (T_WHITESPACE , ($stackPtr + 2 ), null , true );
$phpcsFile->fixer ->replaceToken ($next, $expected);
for ($next++; $next < $end; $next++ ) {
$phpcsFile->fixer ->replaceToken ($next, '');
$phpcsFile->fixer ->endChangeset ();
Documentation generated on Mon, 11 Mar 2019 15:27:46 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|