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

Source for file CodeAnalyzerSniff.php

Documentation is available at CodeAnalyzerSniff.php

  1. <?php
  2. /**
  3.  * Runs the Zend Code Analyzer (from Zend Studio) on the file.
  4.  *
  5.  * @author    Holger Kral <holger.kral@zend.com>
  6.  * @author    Greg Sherwood <gsherwood@squiz.net>
  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\Zend\Sniffs\Debug;
  12.  
  13. use PHP_CodeSniffer\Sniffs\Sniff;
  14. use PHP_CodeSniffer\Files\File;
  15. use PHP_CodeSniffer\Config;
  16. use PHP_CodeSniffer\Exceptions\RuntimeException;
  17.  
  18. class CodeAnalyzerSniff implements Sniff
  19. {
  20.  
  21.  
  22.     /**
  23.      * Returns the token types that this sniff is interested in.
  24.      *
  25.      * @return int[] 
  26.      */
  27.     public function register()
  28.     {
  29.         return array(T_OPEN_TAG);
  30.  
  31.     }//end register()
  32.  
  33.  
  34.     /**
  35.      * Processes the tokens that this sniff is interested in.
  36.      *
  37.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
  38.      * @param int                         $stackPtr  The position in the stack where
  39.      *                                                the token was found.
  40.      *
  41.      * @return int 
  42.      */
  43.     public function process(File $phpcsFile$stackPtr)
  44.     {
  45.         $analyzerPath = Config::getExecutablePath('zend_ca');
  46.         if (is_null($analyzerPath=== true{
  47.             return;
  48.         }
  49.  
  50.         $fileName $phpcsFile->getFilename();
  51.  
  52.         // In the command, 2>&1 is important because the code analyzer sends its
  53.         // findings to stderr. $output normally contains only stdout, so using 2>&1
  54.         // will pipe even stderr to stdout.
  55.         $cmd escapeshellcmd($analyzerPath).' '.escapeshellarg($fileName).' 2>&1';
  56.  
  57.         // There is the possibility to pass "--ide" as an option to the analyzer.
  58.         // This would result in an output format which would be easier to parse.
  59.         // The problem here is that no cleartext error messages are returnwd; only
  60.         // error-code-labels. So for a start we go for cleartext output.
  61.         $exitCode exec($cmd$output$retval);
  62.  
  63.         // Variable $exitCode is the last line of $output if no error occures, on
  64.         // error it is numeric. Try to handle various error conditions and
  65.         // provide useful error reporting.
  66.         if (is_numeric($exitCode=== true && $exitCode > 0{
  67.             if (is_array($output=== true{
  68.                 $msg join('\n'$output);
  69.             }
  70.  
  71.             throw new RuntimeException("Failed invoking ZendCodeAnalyzer, exitcode was [$exitCode], retval was [$retval], output was [$msg]");
  72.         }
  73.  
  74.         if (is_array($output=== true{
  75.             foreach ($output as $finding{
  76.                 // The first two lines of analyzer output contain
  77.                 // something like this:
  78.                 // > Zend Code Analyzer 1.2.2
  79.                 // > Analyzing <filename>...
  80.                 // So skip these...
  81.                 $res preg_match("/^.+\(line ([0-9]+)\):(.+)$/"$finding$regs);
  82.                 if (empty($regs=== true || $res === false{
  83.                     continue;
  84.                 }
  85.  
  86.                 $phpcsFile->addWarningOnLine(trim($regs[2])$regs[1]'ExternalTool');
  87.             }
  88.         }
  89.  
  90.         // Ignore the rest of the file.
  91.         return ($phpcsFile->numTokens + 1);
  92.  
  93.     }//end process()
  94.  
  95.  
  96. }//end class

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