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\Files\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\Exceptions\RuntimeException 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.         $lintPath escapeshellcmd($lintPath);
  75.         $cmd      '$lintPath --nosummary --notime --unix_mode '.escapeshellarg($fileName);
  76.         $msg      exec($cmd$output$retval);
  77.  
  78.         if (is_array($output=== false{
  79.             return;
  80.         }
  81.  
  82.         foreach ($output as $finding{
  83.             $matches    = array();
  84.             $numMatches preg_match('/^(.*):([0-9]+):\(.*?([0-9]+)\)(.*)$/'$finding$matches);
  85.             if ($numMatches === 0{
  86.                 continue;
  87.             }
  88.  
  89.             // Skip error codes we are ignoring.
  90.             $code $matches[3];
  91.             if (in_array($code$this->ignoreCodes=== true{
  92.                 continue;
  93.             }
  94.  
  95.             $line  = (int) $matches[2];
  96.             $error trim($matches[4]);
  97.  
  98.             $message 'gjslint says: (%s) %s';
  99.             $data    = array(
  100.                         $code,
  101.                         $error,
  102.                        );
  103.             if (in_array($code$this->errorCodes=== true{
  104.                 $phpcsFile->addErrorOnLine($message$line'ExternalToolError'$data);
  105.             else {
  106.                 $phpcsFile->addWarningOnLine($message$line'ExternalTool'$data);
  107.             }
  108.         }//end foreach
  109.  
  110.         // Ignore the rest of the file.
  111.         return ($phpcsFile->numTokens + 1);
  112.  
  113.     }//end process()
  114.  
  115.  
  116. }//end class

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