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

Source for file JSHintSniff.php

Documentation is available at JSHintSniff.php

  1. <?php
  2. /**
  3.  * Runs jshint.js on the file.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  6.  * @author    Alexander Wei§ <aweisswa@gmx.de>
  7.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  8.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  9.  */
  10.  
  11. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Debug;
  12.  
  13. use PHP_CodeSniffer\Sniffs\Sniff;
  14. use PHP_CodeSniffer\Files\File;
  15. use PHP_CodeSniffer\Config;
  16.  
  17. class JSHintSniff implements Sniff
  18. {
  19.  
  20.     /**
  21.      * A list of tokenizers this sniff supports.
  22.      *
  23.      * @var array 
  24.      */
  25.     public $supportedTokenizers = array('JS');
  26.  
  27.  
  28.     /**
  29.      * Returns the token types that this sniff is interested in.
  30.      *
  31.      * @return int[] 
  32.      */
  33.     public function register()
  34.     {
  35.         return array(T_OPEN_TAG);
  36.  
  37.     }//end register()
  38.  
  39.  
  40.     /**
  41.      * Processes the tokens that this sniff is interested in.
  42.      *
  43.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
  44.      * @param int                         $stackPtr  The position in the stack where
  45.      *                                                the token was found.
  46.      *
  47.      * @return void 
  48.      * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If jshint.js could not be run
  49.      */
  50.     public function process(File $phpcsFile$stackPtr)
  51.     {
  52.         $rhinoPath  = Config::getExecutablePath('rhino');
  53.         $jshintPath = Config::getExecutablePath('jshint');
  54.         if ($rhinoPath === null || $jshintPath === null{
  55.             return;
  56.         }
  57.  
  58.         $fileName $phpcsFile->getFilename();
  59.  
  60.         $rhinoPath  escapeshellcmd($rhinoPath);
  61.         $jshintPath escapeshellcmd($jshintPath);
  62.  
  63.         $cmd = "$rhinoPath \"$jshintPath\" ".escapeshellarg($fileName);
  64.         $msg exec($cmd$output$retval);
  65.  
  66.         if (is_array($output=== true{
  67.             foreach ($output as $finding{
  68.                 $matches    = array();
  69.                 $numMatches preg_match('/^(.+)\(.+:([0-9]+).*:[0-9]+\)$/'$finding$matches);
  70.                 if ($numMatches === 0{
  71.                     continue;
  72.                 }
  73.  
  74.                 $line    = (int) $matches[2];
  75.                 $message 'jshint says: '.trim($matches[1]);
  76.                 $phpcsFile->addWarningOnLine($message$line'ExternalTool');
  77.             }
  78.         }
  79.  
  80.         // Ignore the rest of the file.
  81.         return ($phpcsFile->numTokens + 1);
  82.  
  83.     }//end process()
  84.  
  85.  
  86. }//end class

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