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

Source for file JSLintSniff.php

Documentation is available at JSLintSniff.php

  1. <?php
  2. /**
  3.  * Runs jslint.js on the file.
  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.  
  10. namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Debug;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Config;
  15.  
  16. class JSLintSniff implements Sniff
  17. {
  18.  
  19.     /**
  20.      * A list of tokenizers this sniff supports.
  21.      *
  22.      * @var array 
  23.      */
  24.     public $supportedTokenizers = array('JS');
  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.      * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If jslint.js could not be run
  48.      */
  49.     public function process(File $phpcsFile$stackPtr)
  50.     {
  51.         $rhinoPath  = Config::getExecutablePath('jslint');
  52.         $jslintPath = Config::getExecutablePath('jslint');
  53.         if ($rhinoPath === null || $jslintPath === null{
  54.             return;
  55.         }
  56.  
  57.         $fileName $phpcsFile->getFilename();
  58.  
  59.         $rhinoPath  escapeshellcmd($rhinoPath);
  60.         $jslintPath escapeshellcmd($jslintPath);
  61.  
  62.         $cmd = "$rhinoPath \"$jslintPath\" ".escapeshellarg($fileName);
  63.         $msg exec($cmd$output$retval);
  64.  
  65.         if (is_array($output=== true{
  66.             foreach ($output as $finding{
  67.                 $matches    = array();
  68.                 $numMatches preg_match('/Lint at line ([0-9]+).*:(.*)$/'$finding$matches);
  69.                 if ($numMatches === 0{
  70.                     continue;
  71.                 }
  72.  
  73.                 $line    = (int) $matches[1];
  74.                 $message 'jslint says: '.trim($matches[2]);
  75.                 $phpcsFile->addWarningOnLine($message$line'ExternalTool');
  76.             }
  77.         }
  78.  
  79.         // Ignore the rest of the file.
  80.         return ($phpcsFile->numTokens + 1);
  81.  
  82.     }//end process()
  83.  
  84.  
  85. }//end class

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