Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Squiz_Sniffs_CSS_ColourDefinitionSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
11
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12
 * @version   CVS: $Id: ColourDefinitionSniff.php 267911 2008-10-28 05:17:23Z squiz $
13
 * @link      http://pear.php.net/package/PHP_CodeSniffer
14
 */
15
 
16
/**
17
 * Squiz_Sniffs_CSS_ColourDefinitionSniff.
18
 *
19
 * Ensure colours are defined in upper-case and use shortcuts where possible.
20
 *
21
 * @category  PHP
22
 * @package   PHP_CodeSniffer
23
 * @author    Greg Sherwood <gsherwood@squiz.net>
24
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
25
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
26
 * @version   Release: 1.2.1
27
 * @link      http://pear.php.net/package/PHP_CodeSniffer
28
 */
29
class Squiz_Sniffs_CSS_ColourDefinitionSniff implements PHP_CodeSniffer_Sniff
30
{
31
 
32
    /**
33
     * A list of tokenizers this sniff supports.
34
     *
35
     * @var array
36
     */
37
    public $supportedTokenizers = array('CSS');
38
 
39
 
40
    /**
41
     * Returns the token types that this sniff is interested in.
42
     *
43
     * @return array(int)
44
     */
45
    public function register()
46
    {
47
        return array(T_COLOUR);
48
 
49
    }//end register()
50
 
51
 
52
    /**
53
     * Processes the tokens that this sniff is interested in.
54
     *
55
     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
56
     * @param int                  $stackPtr  The position in the stack where
57
     *                                        the token was found.
58
     *
59
     * @return void
60
     */
61
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
62
    {
63
        $tokens = $phpcsFile->getTokens();
64
        $colour = $tokens[$stackPtr]['content'];
65
 
66
        $expected = strtoupper($colour);
67
        if ($colour !== $expected) {
68
            $error = "CSS colours must be defined in uppercase; expected $expected but found $colour";
69
            $phpcsFile->addError($error, $stackPtr);
70
        }
71
 
72
        // Now check if shorthand can be used.
73
        if (strlen($colour) !== 7) {
74
            return;
75
        }
76
 
77
        if ($colour{1} === $colour{2} && $colour{3} === $colour{4} && $colour{5} === $colour{6}) {
78
            $expected = '#'.$colour{1}.$colour{3}.$colour{5};
79
            $error    = "CSS colours must use shorthand if available; expected $expected but found $colour";
80
            $phpcsFile->addError($error, $stackPtr);
81
        }
82
 
83
    }//end process()
84
 
85
}//end class
86
?>