1<?php 2/** 3 * Generic_Sniffs_PHP_SyntaxSniff. 4 * 5 * PHP version 5 6 * 7 * @category PHP 8 * @package PHP_CodeSniffer 9 * @author Blaine Schmeisser <blainesch@gmail.com> 10 * @author Greg Sherwood <gsherwood@squiz.net> 11 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) 12 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence 13 * @link http://pear.php.net/package/PHP_CodeSniffer 14 */ 15 16/** 17 * Generic_Sniffs_PHP_SyntaxSniff. 18 * 19 * Ensures PHP believes the syntax is clean. 20 * 21 * @category PHP 22 * @package PHP_CodeSniffer 23 * @author Blaine Schmeisser <blainesch@gmail.com> 24 * @author Greg Sherwood <gsherwood@squiz.net> 25 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) 26 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence 27 * @version Release: @package_version@ 28 * @link http://pear.php.net/package/PHP_CodeSniffer 29 */ 30class Generic_Sniffs_PHP_SyntaxSniff implements PHP_CodeSniffer_Sniff 31{ 32 33 /** 34 * The path to the PHP version we are checking with. 35 * 36 * @var string 37 */ 38 private $_phpPath = null; 39 40 41 /** 42 * Returns an array of tokens this test wants to listen for. 43 * 44 * @return array 45 */ 46 public function register() 47 { 48 return array(T_OPEN_TAG); 49 50 }//end register() 51 52 53 /** 54 * Processes this test, when one of its tokens is encountered. 55 * 56 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 57 * @param int $stackPtr The position of the current token in 58 * the stack passed in $tokens. 59 * 60 * @return void 61 */ 62 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 63 { 64 if ($this->_phpPath === null) { 65 $this->_phpPath = PHP_CodeSniffer::getConfigData('php_path'); 66 if ($this->_phpPath === null) { 67 // PHP_BINARY is available in PHP 5.4+. 68 if (defined('PHP_BINARY') === true) { 69 $this->_phpPath = PHP_BINARY; 70 } else { 71 return; 72 } 73 } 74 } 75 76 $fileName = escapeshellarg($phpcsFile->getFilename()); 77 if (defined('HHVM_VERSION') === false) { 78 $cmd = escapeshellcmd($this->_phpPath)." -l -d error_prepend_string='' $fileName 2>&1"; 79 } else { 80 $cmd = escapeshellcmd($this->_phpPath)." -l $fileName 2>&1"; 81 } 82 83 $output = shell_exec($cmd); 84 $matches = array(); 85 if (preg_match('/^.*error:(.*) in .* on line ([0-9]+)/m', trim($output), $matches) === 1) { 86 $error = trim($matches[1]); 87 $line = (int) $matches[2]; 88 $phpcsFile->addErrorOnLine("PHP syntax error: $error", $line, 'PHPSyntax'); 89 } 90 91 // Ignore the rest of the file. 92 return ($phpcsFile->numTokens + 1); 93 94 }//end process() 95 96 97}//end class 98