|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Generic_Sniffs_Debug_CSSLintSniff. |
| 4 | + * |
| 5 | + * PHP version 5 |
| 6 | + * |
| 7 | + * @category PHP |
| 8 | + * @package PHP_CodeSniffer |
| 9 | + * @author Roman Levishchenko <[email protected]> |
| 10 | + * @copyright 2013 Roman Levishchenko |
| 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 | + * Generic_Sniffs_Debug_CSSLintSniff. |
| 17 | + * |
| 18 | + * Runs csslint on the file. |
| 19 | + * |
| 20 | + * @category PHP |
| 21 | + * @package PHP_CodeSniffer |
| 22 | + * @author Roman Levishchenko <[email protected]> |
| 23 | + * @copyright 2013 Roman Levishchenko |
| 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 | + */ |
| 28 | +class Generic_Sniffs_Debug_CSSLintSniff 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 | + /** |
| 40 | + * Returns the token types that this sniff is interested in. |
| 41 | + * |
| 42 | + * @return array(int) |
| 43 | + */ |
| 44 | + public function register() |
| 45 | + { |
| 46 | + return array(T_OPEN_TAG); |
| 47 | + |
| 48 | + }//end register() |
| 49 | + |
| 50 | + |
| 51 | + /** |
| 52 | + * Processes the tokens that this sniff is interested in. |
| 53 | + * |
| 54 | + * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
| 55 | + * @param int $stackPtr The position in the stack where |
| 56 | + * the token was found. |
| 57 | + * |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
| 61 | + { |
| 62 | + $fileName = $phpcsFile->getFilename(); |
| 63 | + |
| 64 | + $csslintPath = PHP_CodeSniffer::getConfigData('csslint_path'); |
| 65 | + if ($csslintPath === null) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + $cmd = $csslintPath.' '.escapeshellarg($fileName); |
| 70 | + exec($cmd, $output, $retval); |
| 71 | + |
| 72 | + if (is_array($output) === false) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + $tokens = $phpcsFile->getTokens(); |
| 77 | + $count = count($output); |
| 78 | + |
| 79 | + for ($i = 0; $i < $count; $i++) { |
| 80 | + $matches = array(); |
| 81 | + $numMatches = preg_match( |
| 82 | + '/(error|warning) at line (\d+)/', |
| 83 | + $output[$i], |
| 84 | + $matches |
| 85 | + ); |
| 86 | + |
| 87 | + if ($numMatches === 0) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + $line = (int) $matches[2]; |
| 92 | + $message = 'csslint says: '.$output[($i + 1)]; |
| 93 | + // 1-st line is message with error line and error code. |
| 94 | + // 2-nd error message. |
| 95 | + // 3-d wrong line in file. |
| 96 | + // 4-th empty line. |
| 97 | + $i += 4; |
| 98 | + |
| 99 | + $lineToken = null; |
| 100 | + foreach ($tokens as $ptr => $info) { |
| 101 | + if ($info['line'] === $line) { |
| 102 | + $lineToken = $ptr; |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if ($lineToken !== null) { |
| 108 | + $phpcsFile->addWarning($message, $lineToken, 'ExternalTool'); |
| 109 | + } |
| 110 | + }//end for |
| 111 | + |
| 112 | + }//end process() |
| 113 | + |
| 114 | + |
| 115 | +}//end class |
| 116 | + |
| 117 | +?> |
0 commit comments