Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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 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 253180 2008-02-19 00:38:00Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Zend_Sniffs_Debug_CodeAnalyzerSniff.
19
 *
20
 * Runs the Zend Code Analyzer (from Zend Studio) on the file.
21
 *
22
 * @category  PHP
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.2.1
29
 * @link      http://pear.php.net/package/PHP_CodeSniffer
30
 */
31
class Zend_Sniffs_Debug_CodeAnalyzerSniff implements PHP_CodeSniffer_Sniff
32
{
33
 
34
 
35
    /**
36
     * Returns the token types that this sniff is interested in.
37
     *
38
     * @return array(int)
39
     */
40
    public function register()
41
    {
42
        return array(T_OPEN_TAG);
43
 
44
    }//end register()
45
 
46
 
47
    /**
48
     * Processes the tokens that this sniff is interested in.
49
     *
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.
53
     *
54
     * @return void
55
     */
56
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
57
    {
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) {
62
            return;
63
        }
64
 
65
        $fileName = $phpcsFile->getFilename();
66
 
67
        $analyzerPath = PHP_CodeSniffer::getConfigData('zend_ca_path');
68
        if (is_null($analyzerPath) === true) {
69
            return;
70
        }
71
 
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';
76
 
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);
82
 
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
85
        // error reporting.
86
        if (is_numeric($exitCode) === true && $exitCode > 0) {
87
            if (is_array($output) === true) {
88
                $msg = join('\n', $output);
89
            }
90
 
91
            throw new PHP_CodeSniffer_Exception("Failed invoking ZendCodeAnalyzer, exitcode was [$exitCode], retval was [$retval], output was [$msg]");
92
        }
93
 
94
        if (is_array($output) === true) {
95
            $tokens = $phpcsFile->getTokens();
96
 
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>...
102
                // So skip these...
103
                $res = eregi("^.+\(line ([0-9]+)\):(.+)$", $finding, $regs);
104
                if ($regs === null || $res === false) {
105
                    continue;
106
                }
107
 
108
                // Find the token at the start of the line.
109
                $lineToken = null;
110
                foreach ($tokens as $ptr => $info) {
111
                    if ($info['line'] == $regs[1]) {
112
                        $lineToken = $ptr;
113
                        break;
114
                    }
115
                }
116
 
117
                if ($lineToken !== null) {
118
                    $phpcsFile->addWarning(trim($regs[2]), $ptr);
119
                }
120
            }//end foreach
121
        }//end if
122
 
123
    }//end process()
124
 
125
}//end class
126
?>