Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Squiz_Sniffs_Debug_JavaScriptLintSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
11
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12
 * @version   CVS: $Id: JavaScriptLintSniff.php 289841 2009-10-22 04:02:20Z squiz $
13
 * @link      http://pear.php.net/package/PHP_CodeSniffer
14
 */
15
 
16
/**
17
 * Squiz_Sniffs_Debug_JavaScriptLintSniff.
18
 *
19
 * Runs JavaScipt Lint on the file.
20
 *
21
 * @category  PHP
22
 * @package   PHP_CodeSniffer
23
 * @author    Greg Sherwood <gsherwood@squiz.net>
24
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
25
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
26
 * @version   Release: 1.2.1
27
 * @link      http://pear.php.net/package/PHP_CodeSniffer
28
 */
29
class Squiz_Sniffs_Debug_JavaScriptLintSniff implements PHP_CodeSniffer_Sniff
30
{
31
 
32
    /**
33
     * A list of tokenizers this sniff supports.
34
     *
35
     * @var array
36
     */
37
    public $supportedTokenizers = array('JS');
38
 
39
 
40
    /**
41
     * Returns the token types that this sniff is interested in.
42
     *
43
     * @return array(int)
44
     */
45
    public function register()
46
    {
47
        return array(T_OPEN_TAG);
48
 
49
    }//end register()
50
 
51
 
52
    /**
53
     * Processes the tokens that this sniff is interested in.
54
     *
55
     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
56
     * @param int                  $stackPtr  The position in the stack where
57
     *                                        the token was found.
58
     *
59
     * @return void
60
     */
61
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
62
    {
63
        $fileName = $phpcsFile->getFilename();
64
 
65
        $jslPath = PHP_CodeSniffer::getConfigData('jsl_path');
66
        if (is_null($jslPath) === true) {
67
            return;
68
        }
69
 
70
        $cmd = '"'.$jslPath.'" -nologo -nofilelisting -nocontext -nosummary -output-format __LINE__:__ERROR__ -process "'.$fileName.'"';
71
        $msg = exec($cmd, $output, $retval);
72
 
73
        // $exitCode is the last line of $output if no error occures, on error it
74
        // is numeric. Try to handle various error conditions and provide useful
75
        // error reporting.
76
        if ($retval === 2 || $retval === 4) {
77
            if (is_array($output) === true) {
78
                $msg = join('\n', $output);
79
            }
80
 
81
            throw new PHP_CodeSniffer_Exception("Failed invoking JavaScript Lint, retval was [$retval], output was [$msg]");
82
        }
83
 
84
 
85
        if (is_array($output) === true) {
86
            $tokens = $phpcsFile->getTokens();
87
 
88
            foreach ($output as $finding) {
89
                $split   = strpos($finding, ':');
90
                $line    = substr($finding, 0, $split);
91
                $message = substr($finding, ($split + 1));
92
 
93
                // Find the token at the start of the line.
94
                $lineToken = null;
95
                foreach ($tokens as $ptr => $info) {
96
                    if ($info['line'] == $line) {
97
                        $lineToken = $ptr;
98
                        break;
99
                    }
100
                }
101
 
102
                if ($lineToken !== null) {
103
                    $phpcsFile->addWarning(trim($message), $ptr);
104
                }
105
            }//end foreach
106
        }//end if
107
 
108
    }//end process()
109
 
110
}//end class
111
?>