PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file CSSLintSniff.php

Documentation is available at CSSLintSniff.php

  1. <?php
  2. /**
  3.  * Runs csslint on the file.
  4.  *
  5.  * @author    Roman Levishchenko <index.0h@gmail.com>
  6.  * @copyright 2013-2014 Roman Levishchenko
  7.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8.  */
  9.  
  10. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Debug;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Config;
  15.  
  16. class CSSLintSniff implements Sniff
  17. {
  18.  
  19.     /**
  20.      * A list of tokenizers this sniff supports.
  21.      *
  22.      * @var array 
  23.      */
  24.     public $supportedTokenizers = array('CSS');
  25.  
  26.  
  27.     /**
  28.      * Returns the token types that this sniff is interested in.
  29.      *
  30.      * @return int[] 
  31.      */
  32.     public function register()
  33.     {
  34.         return array(T_OPEN_TAG);
  35.  
  36.     }//end register()
  37.  
  38.  
  39.     /**
  40.      * Processes the tokens that this sniff is interested in.
  41.      *
  42.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
  43.      * @param int                         $stackPtr  The position in the stack where
  44.      *                                                the token was found.
  45.      *
  46.      * @return void 
  47.      */
  48.     public function process(File $phpcsFile$stackPtr)
  49.     {
  50.         $csslintPath = Config::getExecutablePath('csslint');
  51.         if ($csslintPath === null{
  52.             return;
  53.         }
  54.  
  55.         $fileName $phpcsFile->getFilename();
  56.  
  57.         $cmd escapeshellcmd($csslintPath).' '.escapeshellarg($fileName).' 2>&1';
  58.         exec($cmd$output$retval);
  59.  
  60.         if (is_array($output=== false{
  61.             return;
  62.         }
  63.  
  64.         $count count($output);
  65.  
  66.         for ($i = 0; $i $count$i++{
  67.             $matches    = array();
  68.             $numMatches preg_match(
  69.                 '/(error|warning) at line (\d+)/',
  70.                 $output[$i],
  71.                 $matches
  72.             );
  73.  
  74.             if ($numMatches === 0{
  75.                 continue;
  76.             }
  77.  
  78.             $line    = (int) $matches[2];
  79.             $message 'csslint says: '.$output[($i + 1)];
  80.             // First line is message with error line and error code.
  81.             // Second is error message.
  82.             // Third is wrong line in file.
  83.             // Fourth is empty line.
  84.             $i += 4;
  85.  
  86.             $phpcsFile->addWarningOnLine($message$line'ExternalTool');
  87.         }//end for
  88.  
  89.         // Ignore the rest of the file.
  90.         return ($phpcsFile->numTokens + 1);
  91.  
  92.     }//end process()
  93.  
  94.  
  95. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:22 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.