-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
SyntaxSniff.php
75 lines (61 loc) · 2.08 KB
/
SyntaxSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* Ensures PHP believes the syntax is clean.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Blaine Schmeisser <blainesch@gmail.com>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Common;
class SyntaxSniff implements Sniff
{
/**
* The path to the PHP version we are checking with.
*
* @var string
*/
private $phpPath = null;
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [
T_OPEN_TAG,
T_OPEN_TAG_WITH_ECHO,
];
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return int
*/
public function process(File $phpcsFile, $stackPtr)
{
if ($this->phpPath === null) {
$this->phpPath = Config::getExecutablePath('php');
}
$fileName = escapeshellarg($phpcsFile->getFilename());
$cmd = Common::escapeshellcmd($this->phpPath)." -l -d display_errors=1 -d error_prepend_string='' $fileName 2>&1";
$output = shell_exec($cmd);
$matches = [];
if (preg_match('/^.*error:(.*) in .* on line ([0-9]+)/m', trim($output), $matches) === 1) {
$error = trim($matches[1]);
$line = (int) $matches[2];
$phpcsFile->addErrorOnLine("PHP syntax error: $error", $line, 'PHPSyntax');
}
// Ignore the rest of the file.
return $phpcsFile->numTokens;
}//end process()
}//end class