1<?php
2/**
3 * MySource_Sniffs_CSS_BrowserSpecificStylesSniff.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
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
13 */
14
15/**
16 * MySource_Sniffs_CSS_BrowserSpecificStylesSniff.
17 *
18 * Ensure that browser-specific styles are not used.
19 *
20 * @category  PHP
21 * @package   PHP_CodeSniffer
22 * @author    Greg Sherwood <gsherwood@squiz.net>
23 * @copyright 2006-2014 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 */
28class MySource_Sniffs_CSS_BrowserSpecificStylesSniff 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 specific stylesheet suffixes we allow.
40     *
41     * These stylesheets contain browser specific styles
42     * so this sniff ignore them files in the form:
43     * *_moz.css and *_ie7.css etc.
44     *
45     * @var array
46     */
47    protected $specificStylesheets = array(
48                                      'moz'    => true,
49                                      'ie'     => true,
50                                      'ie7'    => true,
51                                      'ie8'    => true,
52                                      'webkit' => true,
53                                     );
54
55
56    /**
57     * Returns the token types that this sniff is interested in.
58     *
59     * @return int[]
60     */
61    public function register()
62    {
63        return array(T_STYLE);
64
65    }//end register()
66
67
68    /**
69     * Processes the tokens that this sniff is interested in.
70     *
71     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
72     * @param int                  $stackPtr  The position in the stack where
73     *                                        the token was found.
74     *
75     * @return void
76     */
77    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
78    {
79        // Ignore files with browser-specific suffixes.
80        $filename  = $phpcsFile->getFilename();
81        $breakChar = strrpos($filename, '_');
82        if ($breakChar !== false && substr($filename, -4) === '.css') {
83            $specific = substr($filename, ($breakChar + 1), -4);
84            if (isset($this->specificStylesheets[$specific]) === true) {
85                return;
86            }
87        }
88
89        $tokens  = $phpcsFile->getTokens();
90        $content = $tokens[$stackPtr]['content'];
91
92        if ($content{0} === '-') {
93            $error = 'Browser-specific styles are not allowed';
94            $phpcsFile->addError($error, $stackPtr, 'ForbiddenStyle');
95        }
96
97    }//end process()
98
99
100}//end class
101