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

Source for file ClosureLinterSniff.php

Documentation is available at ClosureLinterSniff.php

  1. <?php
  2. /**
  3.  * Runs gjslint 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\Generic\Sniffs\Debug;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Config;
  15.  
  16. class ClosureLinterSniff implements Sniff
  17. {
  18.  
  19.     /**
  20.      * A list of error codes that should show errors.
  21.      *
  22.      * All other error codes will show warnings.
  23.      *
  24.      * @var integer 
  25.      */
  26.     public $errorCodes = array();
  27.  
  28.     /**
  29.      * A list of error codes to ignore.
  30.      *
  31.      * @var integer 
  32.      */
  33.     public $ignoreCodes = array();
  34.  
  35.     /**
  36.      * A list of tokenizers this sniff supports.
  37.      *
  38.      * @var array 
  39.      */
  40.     public $supportedTokenizers = array('JS');
  41.  
  42.  
  43.     /**
  44.      * Returns the token types that this sniff is interested in.
  45.      *
  46.      * @return int[] 
  47.      */
  48.     public function register()
  49.     {
  50.         return array(T_OPEN_TAG);
  51.  
  52.     }//end register()
  53.  
  54.  
  55.     /**
  56.      * Processes the tokens that this sniff is interested in.
  57.      *
  58.      * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
  59.      * @param int                  $stackPtr  The position in the stack where
  60.      *                                         the token was found.
  61.      *
  62.      * @return void 
  63.      * @throws PHP_CodeSniffer_Exception If jslint.js could not be run
  64.      */
  65.     public function process(File $phpcsFile$stackPtr)
  66.     {
  67.         $lintPath = Config::getExecutablePath('gjslint');
  68.         if ($lintPath === null{
  69.             return;
  70.         }
  71.  
  72.         $fileName $phpcsFile->getFilename();
  73.  
  74.         $cmd = "$lintPath --nosummary --notime --unix_mode \"$fileName\"";
  75.         $msg exec($cmd$output$retval);
  76.  
  77.         if (is_array($output=== false{
  78.             return;
  79.         }
  80.  
  81.         foreach ($output as $finding{
  82.             $matches    = array();
  83.             $numMatches preg_match('/^(.*):([0-9]+):\(.*?([0-9]+)\)(.*)$/'$finding$matches);
  84.             if ($numMatches === 0{
  85.                 continue;
  86.             }
  87.  
  88.             // Skip error codes we are ignoring.
  89.             $code $matches[3];
  90.             if (in_array($code$this->ignoreCodes=== true{
  91.                 continue;
  92.             }
  93.  
  94.             $line  = (int) $matches[2];
  95.             $error trim($matches[4]);
  96.  
  97.             $message 'gjslint says: (%s) %s';
  98.             $data    = array(
  99.                         $code,
  100.                         $error,
  101.                        );
  102.             if (in_array($code$this->errorCodes=== true{
  103.                 $phpcsFile->addErrorOnLine($message$line'ExternalToolError'$data);
  104.             else {
  105.                 $phpcsFile->addWarningOnLine($message$line'ExternalTool'$data);
  106.             }
  107.         }//end foreach
  108.  
  109.         // Ignore the rest of the file.
  110.         return ($phpcsFile->numTokens + 1);
  111.  
  112.     }//end process()
  113.  
  114.  
  115. }//end class

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