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

Source for file SyntaxSniff.php

Documentation is available at SyntaxSniff.php

  1. <?php
  2. /**
  3.  * Ensures PHP believes the syntax is clean.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  6.  * @author    Blaine Schmeisser <blainesch@gmail.com>
  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\PHP;
  12.  
  13. use PHP_CodeSniffer\Sniffs\Sniff;
  14. use PHP_CodeSniffer\Files\File;
  15. use PHP_CodeSniffer\Config;
  16.  
  17. class SyntaxSniff implements Sniff
  18. {
  19.  
  20.     /**
  21.      * The path to the PHP version we are checking with.
  22.      *
  23.      * @var string 
  24.      */
  25.     private $phpPath = null;
  26.  
  27.  
  28.     /**
  29.      * Returns an array of tokens this test wants to listen for.
  30.      *
  31.      * @return array 
  32.      */
  33.     public function register()
  34.     {
  35.         return array(T_OPEN_TAG);
  36.  
  37.     }//end register()
  38.  
  39.  
  40.     /**
  41.      * Processes this test, when one of its tokens is encountered.
  42.      *
  43.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  44.      * @param int                         $stackPtr  The position of the current token in
  45.      *                                                the stack passed in $tokens.
  46.      *
  47.      * @return void 
  48.      */
  49.     public function process(File $phpcsFile$stackPtr)
  50.     {
  51.         if ($this->phpPath === null{
  52.             $this->phpPath = Config::getExecutablePath('php');
  53.             if ($this->phpPath === null{
  54.                 // PHP_BINARY is available in PHP 5.4+.
  55.                 if (defined('PHP_BINARY'=== true{
  56.                     $this->phpPath = PHP_BINARY;
  57.                 else {
  58.                     return;
  59.                 }
  60.             }
  61.         }
  62.  
  63.         $fileName escapeshellarg($phpcsFile->getFilename());
  64.         if (defined('HHVM_VERSION'=== false{
  65.             $cmd escapeshellcmd($this->phpPath)." -l -d error_prepend_string='' $fileName 2>&1";
  66.         else {
  67.             $cmd escapeshellcmd($this->phpPath)." -l $fileName 2>&1";
  68.         }
  69.  
  70.         $output  shell_exec($cmd);
  71.         $matches = array();
  72.         if (preg_match('/^.*error:(.*) in .* on line ([0-9]+)/m'trim($output)$matches=== 1{
  73.             $error trim($matches[1]);
  74.             $line  = (int) $matches[2];
  75.             $phpcsFile->addErrorOnLine("PHP syntax error: $error"$line'PHPSyntax');
  76.         }
  77.  
  78.         // Ignore the rest of the file.
  79.         return ($phpcsFile->numTokens + 1);
  80.  
  81.     }//end process()
  82.  
  83.  
  84. }//end class

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