You are here

class ColourDefinitionSniff in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/CSS/ColourDefinitionSniff.php \Drupal\Sniffs\CSS\ColourDefinitionSniff
  2. 8.3.x coder_sniffer/Drupal/Sniffs/CSS/ColourDefinitionSniff.php \Drupal\Sniffs\CSS\ColourDefinitionSniff

\Drupal\Sniffs\CSS\ColourDefinitionSniff.

Ensure colours are defined in lower-case.

@category PHP @package PHP_CodeSniffer @link http://pear.php.net/package/PHP_CodeSniffer

Hierarchy

Expanded class hierarchy of ColourDefinitionSniff

File

coder_sniffer/Drupal/Sniffs/CSS/ColourDefinitionSniff.php, line 24

Namespace

Drupal\Sniffs\CSS
View source
class ColourDefinitionSniff implements Sniff {

  /**
   * A list of tokenizers this sniff supports.
   *
   * @var array
   */
  public $supportedTokenizers = array(
    'CSS',
  );

  /**
   * Returns the token types that this sniff is interested in.
   *
   * @return array(int)
   */
  public function register() {
    return array(
      T_COLOUR,
    );
  }

  //end 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
   *                                               the token was found.
   *
   * @return void
   */
  public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile
      ->getTokens();
    $colour = $tokens[$stackPtr]['content'];
    $expected = strtolower($colour);
    if ($colour !== $expected) {
      $error = 'CSS colours must be defined in lowercase; expected %s but found %s';
      $data = array(
        $expected,
        $colour,
      );
      $fix = $phpcsFile
        ->addFixableError($error, $stackPtr, 'NotLower', $data);
      if ($fix === true) {
        $phpcsFile->fixer
          ->replaceToken($stackPtr, $expected);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ColourDefinitionSniff::$supportedTokenizers public property A list of tokenizers this sniff supports.
ColourDefinitionSniff::process public function Processes the tokens that this sniff is interested in.
ColourDefinitionSniff::register public function Returns the token types that this sniff is interested in.