1<?php
2/**
3 * Ensure that each style definition is on a line by itself.
4 *
5 * @author    Greg Sherwood <gsherwood@squiz.net>
6 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8 */
9
10namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\CSS;
11
12use PHP_CodeSniffer\Files\File;
13use PHP_CodeSniffer\Sniffs\Sniff;
14
15class DisallowMultipleStyleDefinitionsSniff implements Sniff
16{
17
18    /**
19     * A list of tokenizers this sniff supports.
20     *
21     * @var array
22     */
23    public $supportedTokenizers = ['CSS'];
24
25
26    /**
27     * Returns the token types that this sniff is interested in.
28     *
29     * @return int[]
30     */
31    public function register()
32    {
33        return [T_STYLE];
34
35    }//end register()
36
37
38    /**
39     * Processes the tokens that this sniff is interested in.
40     *
41     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
42     * @param int                         $stackPtr  The position in the stack where
43     *                                               the token was found.
44     *
45     * @return void
46     */
47    public function process(File $phpcsFile, $stackPtr)
48    {
49        $tokens = $phpcsFile->getTokens();
50        $next   = $phpcsFile->findNext(T_STYLE, ($stackPtr + 1));
51        if ($next === false) {
52            return;
53        }
54
55        if ($tokens[$next]['content'] === 'progid') {
56            // Special case for IE filters.
57            return;
58        }
59
60        if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) {
61            $error = 'Each style definition must be on a line by itself';
62            $fix   = $phpcsFile->addFixableError($error, $next, 'Found');
63            if ($fix === true) {
64                $phpcsFile->fixer->addNewlineBefore($next);
65            }
66        }
67
68    }//end process()
69
70
71}//end class
72