3
* Zend_Sniffs_Debug_CodeAnalyzerSniff.
8
* @package PHP_CodeSniffer
9
* @author Holger Kral <holger.kral@zend.com>
10
* @author Greg Sherwood <gsherwood@squiz.net>
11
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
12
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
13
* @version CVS: $Id: CodeAnalyzerSniff.php,v 1.3 2008/02/19 00:38:00 squiz Exp $
14
* @link http://pear.php.net/package/PHP_CodeSniffer
18
* Zend_Sniffs_Debug_CodeAnalyzerSniff.
20
* Runs the Zend Code Analyzer (from Zend Studio) on the file.
23
* @package PHP_CodeSniffer
24
* @author Holger Kral <holger.kral@zend.com>
25
* @author Greg Sherwood <gsherwood@squiz.net>
26
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
27
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
28
* @version Release: 1.1.0
29
* @link http://pear.php.net/package/PHP_CodeSniffer
31
class Zend_Sniffs_Debug_CodeAnalyzerSniff implements PHP_CodeSniffer_Sniff
36
* Returns the token types that this sniff is interested in.
40
public function register()
42
return array(T_OPEN_TAG);
48
* Processes the tokens that this sniff is interested in.
50
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
51
* @param int $stackPtr The position in the stack where
52
* the token was found.
56
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
58
// Because we are analyzing the whole file in one step, execute this method
59
// only on first occurence of a T_OPEN_TAG.
60
$prevOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1));
61
if ($prevOpenTag !== false) {
65
$fileName = $phpcsFile->getFilename();
67
$analyzerPath = PHP_CodeSniffer::getConfigData('zend_ca_path');
68
if (is_null($analyzerPath) === true) {
72
// In the command, 2>&1 is important because the code analyzer sends its
73
// findings to stderr. $output normally contains only stdout, so using 2>&1
74
// will pipe even stderr to stdout.
75
$cmd = $analyzerPath.' '.$fileName.' 2>&1';
77
// There is the possibility to pass "--ide" as an option to the analyzer.
78
// This would result in an output format which would be easier to parse.
79
// The problem here is that no cleartext error messages are returnwd; only
80
// error-code-labels. So for a start we go for cleartext output.
81
$exitCode = exec($cmd, $output, $retval);
83
// $exitCode is the last line of $output if no error occures, on error it
84
// is numeric. Try to handle various error conditions and provide useful
86
if (is_numeric($exitCode) === true && $exitCode > 0) {
87
if (is_array($output) === true) {
88
$msg = join('\n', $output);
91
throw new PHP_CodeSniffer_Exception("Failed invoking ZendCodeAnalyzer, exitcode was [$exitCode], retval was [$retval], output was [$msg]");
94
if (is_array($output) === true) {
95
$tokens = $phpcsFile->getTokens();
97
foreach ($output as $finding) {
98
// The first two lines of analyzer output contain
99
// something like this:
100
// > Zend Code Analyzer 1.2.2
101
// > Analyzing <filename>...
103
$res = eregi("^.+\(line ([0-9]+)\):(.+)$", $finding, $regs);
104
if ($regs === null || $res === false) {
108
// Find the token at the start of the line.
110
foreach ($tokens as $ptr => $info) {
111
if ($info['line'] == $regs[1]) {
117
if ($lineToken !== null) {
118
$phpcsFile->addWarning(trim($regs[2]), $ptr);
b'\\ No newline at end of file'