3
* Generic_Sniffs_PHP_SyntaxSniff.
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
17
* Generic_Sniffs_PHP_SyntaxSniff.
19
* Ensures PHP believes the syntax is clean.
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: 1.5.5
28
* @link http://pear.php.net/package/PHP_CodeSniffer
30
class Generic_Sniffs_PHP_SyntaxSniff implements PHP_CodeSniffer_Sniff
35
* Returns an array of tokens this test wants to listen for.
39
public function register()
41
return array(T_OPEN_TAG);
47
* Processes this test, when one of its tokens is encountered.
49
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
50
* @param int $stackPtr The position of the current token in
51
* the stack passed in $tokens.
55
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
57
$phpPath = PHP_CodeSniffer::getConfigData('php_path');
58
if ($phpPath === null) {
62
$fileName = $phpcsFile->getFilename();
63
$cmd = "$phpPath -l \"$fileName\" 2>&1";
64
$output = shell_exec($cmd);
67
if (preg_match('/^.*error:(.*) in .* on line ([0-9]+)/', $output, $matches)) {
68
$error = trim($matches[1]);
69
$line = (int) $matches[2];
71
$tokens = $phpcsFile->getTokens();
72
$numLines = $tokens[($phpcsFile->numTokens - 1)]['line'];
73
if ($line > $numLines) {
77
foreach ($tokens as $id => $token) {
78
if ($token['line'] === $line) {
79
$phpcsFile->addError("PHP syntax error: $error", $id, 'PHPSyntax');