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 <index.0h@gmail.com> 10 * @copyright 2013-2014 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 <index.0h@gmail.com> 23 * @copyright 2013-2014 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 */ 28class 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 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 = escapeshellcmd($csslintPath).' '.escapeshellarg($fileName).' 2>&1'; 70 exec($cmd, $output, $retval); 71 72 if (is_array($output) === false) { 73 return; 74 } 75 76 $count = count($output); 77 78 for ($i = 0; $i < $count; $i++) { 79 $matches = array(); 80 $numMatches = preg_match( 81 '/(error|warning) at line (\d+)/', 82 $output[$i], 83 $matches 84 ); 85 86 if ($numMatches === 0) { 87 continue; 88 } 89 90 $line = (int) $matches[2]; 91 $message = 'csslint says: '.$output[($i + 1)]; 92 // First line is message with error line and error code. 93 // Second is error message. 94 // Third is wrong line in file. 95 // Fourth is empty line. 96 $i += 4; 97 98 $phpcsFile->addWarningOnLine($message, $line, 'ExternalTool'); 99 }//end for 100 101 // Ignore the rest of the file. 102 return ($phpcsFile->numTokens + 1); 103 104 }//end process() 105 106 107}//end class 108