1<?php
2/**
3 * Zend_Sniffs_Debug_CodeAnalyzerSniff.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Holger Kral <holger.kral@zend.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 * Zend_Sniffs_Debug_CodeAnalyzerSniff.
18 *
19 * Runs the Zend Code Analyzer (from Zend Studio) on the file.
20 *
21 * @category  PHP
22 * @package   PHP_CodeSniffer
23 * @author    Holger Kral <holger.kral@zend.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 Zend_Sniffs_Debug_CodeAnalyzerSniff implements PHP_CodeSniffer_Sniff
31{
32
33
34    /**
35     * Returns the token types that this sniff is interested in.
36     *
37     * @return int[]
38     */
39    public function register()
40    {
41        return array(T_OPEN_TAG);
42
43    }//end register()
44
45
46    /**
47     * Processes the tokens that this sniff is interested in.
48     *
49     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
50     * @param int                  $stackPtr  The position in the stack where
51     *                                        the token was found.
52     *
53     * @return int
54     */
55    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
56    {
57        $fileName     = $phpcsFile->getFilename();
58        $analyzerPath = PHP_CodeSniffer::getConfigData('zend_ca_path');
59        if (is_null($analyzerPath) === true) {
60            return;
61        }
62
63        // In the command, 2>&1 is important because the code analyzer sends its
64        // findings to stderr. $output normally contains only stdout, so using 2>&1
65        // will pipe even stderr to stdout.
66        $cmd = escapeshellcmd($analyzerPath).' '.escapeshellarg($fileName).' 2>&1';
67
68        // There is the possibility to pass "--ide" as an option to the analyzer.
69        // This would result in an output format which would be easier to parse.
70        // The problem here is that no cleartext error messages are returnwd; only
71        // error-code-labels. So for a start we go for cleartext output.
72        $exitCode = exec($cmd, $output, $retval);
73
74        // Variable $exitCode is the last line of $output if no error occures, on
75        // error it is numeric. Try to handle various error conditions and
76        // provide useful error reporting.
77        if (is_numeric($exitCode) === true && $exitCode > 0) {
78            if (is_array($output) === true) {
79                $msg = join('\n', $output);
80            }
81
82            throw new PHP_CodeSniffer_Exception("Failed invoking ZendCodeAnalyzer, exitcode was [$exitCode], retval was [$retval], output was [$msg]");
83        }
84
85        if (is_array($output) === true) {
86            foreach ($output as $finding) {
87                // The first two lines of analyzer output contain
88                // something like this:
89                // > Zend Code Analyzer 1.2.2
90                // > Analyzing <filename>...
91                // So skip these...
92                $res = preg_match("/^.+\(line ([0-9]+)\):(.+)$/", $finding, $regs);
93                if (empty($regs) === true || $res === false) {
94                    continue;
95                }
96
97                $phpcsFile->addWarningOnLine(trim($regs[2]), $regs[1], 'ExternalTool');
98            }
99        }
100
101        // Ignore the rest of the file.
102        return ($phpcsFile->numTokens + 1);
103
104    }//end process()
105
106
107}//end class
108