Subversion Repositories Applications.framework

Rev

Rev 5 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
34 aurelien 1
#!/usr/local/bin/php
5 aurelien 2
<?php
3
/**
4
 * A commit hook for SVN.
5
 *
6
 * PHP version 5
7
 *
8
 * @category  PHP
9
 * @package   PHP_CodeSniffer
10
 * @author    Jack Bates <ms419@freezone.co.uk>
11
 * @author    Greg Sherwood <gsherwood@squiz.net>
12
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
13
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
34 aurelien 14
 * @version   CVS: $Id$
5 aurelien 15
 * @link      http://pear.php.net/package/PHP_CodeSniffer
16
 */
17
 
18
if (is_file(dirname(__FILE__).'/../CodeSniffer/CLI.php') === true) {
19
    include_once dirname(__FILE__).'/../CodeSniffer/CLI.php';
20
} else {
21
    include_once 'PHP/CodeSniffer/CLI.php';
22
}
23
 
34 aurelien 24
define('PHP_CODESNIFFER_SVNLOOK', '/usr/local/bin/svnlook');
5 aurelien 25
 
26
 
27
/**
28
 * A class to process command line options.
29
 *
30
 * @category  PHP
31
 * @package   PHP_CodeSniffer
32
 * @author    Jack Bates <ms419@freezone.co.uk>
33
 * @author    Greg Sherwood <gsherwood@squiz.net>
34
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
35
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
36
 * @version   Release: @package_version@
37
 * @link      http://pear.php.net/package/PHP_CodeSniffer
38
 */
39
class PHP_CodeSniffer_SVN_Hook extends PHP_CodeSniffer_CLI
40
{
41
 
42
 
43
    /**
44
     * Get a list of default values for all possible command line arguments.
45
     *
46
     * @return array
47
     */
48
    public function getDefaults()
49
    {
50
        $defaults = parent::getDefaults();
51
 
52
        $defaults['svnArgs'] = array();
53
        return $defaults;
54
 
55
    }//end getDefaults()
56
 
57
 
58
    /**
59
     * Processes an unknown command line argument.
60
     *
61
     * All unkown args are sent to SVN commands.
62
     *
63
     * @param string $arg    The command line argument.
64
     * @param int    $pos    The position of the argument on the command line.
65
     * @param array  $values An array of values determined from CLI args.
66
     *
67
     * @return array The updated CLI values.
68
     * @see getCommandLineValues()
69
     */
70
    public function processUnknownArgument($arg, $pos, $values)
71
    {
72
        $values['svnArgs'][] = $arg;
73
        return $values;
74
 
75
    }//end processUnknownArgument()
76
 
77
 
78
    /**
79
     * Runs PHP_CodeSniffer over files are directories.
80
     *
81
     * @param array $values An array of values determined from CLI args.
82
     *
83
     * @return int The number of error and warning messages shown.
84
     * @see getCommandLineValues()
85
     */
86
    public function process($values=array())
87
    {
88
        if (empty($values) === true) {
89
            $values = parent::getCommandLineValues();
90
        }
91
 
92
        // Get list of files in this transaction.
93
        $command = PHP_CODESNIFFER_SVNLOOK.' changed '.implode(' ', $values['svnArgs']);
94
        $handle  = popen($command, 'r');
95
        if ($handle === false) {
96
            echo 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
97
            exit(2);
98
        }
99
 
100
        $contents = stream_get_contents($handle);
101
        fclose($handle);
102
 
103
        // Do not check deleted paths.
104
        $contents = preg_replace('/^D.*/m', null, $contents);
105
 
106
        // Drop the four characters representing the action which precede the path on
107
        // each line.
108
        $contents = preg_replace('/^.{4}/m', null, $contents);
109
 
110
        $values['standard'] = $this->validateStandard($values['standard']);
111
        if (PHP_CodeSniffer::isInstalledStandard($values['standard']) === false) {
112
            // They didn't select a valid coding standard, so help them
113
            // out by letting them know which standards are installed.
114
            echo 'ERROR: the "'.$values['standard'].'" coding standard is not installed. ';
115
            $this->printInstalledStandards();
116
            exit(2);
117
        }
118
 
119
        $phpcs = new PHP_CodeSniffer($values['verbosity'], $values['tabWidth']);
120
 
121
        // Set file extensions if they were specified. Otherwise,
122
        // let PHP_CodeSniffer decide on the defaults.
123
        if (empty($values['extensions']) === false) {
124
            $phpcs->setAllowedFileExtensions($values['extensions']);
125
        }
126
 
127
        // Set ignore patterns if they were specified.
128
        if (empty($values['ignored']) === false) {
129
            $phpcs->setIgnorePatterns($values['ignored']);
130
        }
131
 
132
        $phpcs->setTokenListeners($values['standard']);
133
 
134
        foreach (preg_split('/\v/', $contents, -1, PREG_SPLIT_NO_EMPTY) as $path) {
135
            // No need to process folders as each changed file is checked.
136
            if (substr($path, -1) === '/') {
137
                continue;
138
            }
139
 
140
            // Get the contents of each file, as it would be after this transaction.
141
            $command = PHP_CODESNIFFER_SVNLOOK.' cat '.implode(' ', $values['svnArgs']).' '.$path;
142
            $handle  = popen($command, 'r');
143
            if ($handle === false) {
144
                echo 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
145
                exit(2);
146
            }
147
 
148
            $contents = stream_get_contents($handle);
149
            fclose($handle);
150
 
151
            $phpcs->processFile($path, $contents);
152
        }//end foreach
153
 
154
        return parent::printErrorReport(
155
            $phpcs,
156
            $values['report'],
157
            $values['showWarnings'],
158
            $values['showSources'],
159
            $values['reportFile']
160
        );
161
 
162
    }//end process()
163
 
164
 
165
    /**
166
     * Prints out the usage information for this script.
167
     *
168
     * @return void
169
     */
170
    public function printUsage()
171
    {
172
        parent::printUsage();
173
 
174
        echo PHP_EOL;
175
        echo '    Each additional argument is passed to the `svnlook changed ...`'.PHP_EOL;
176
        echo '    and `svnlook cat ...` commands.  The report is printed on standard output,'.PHP_EOL;
177
        echo '    however Subversion displays only standard error to the user, so in a'.PHP_EOL;
178
        echo '    pre-commit hook, this script should be invoked as follows:'.PHP_EOL;
179
        echo PHP_EOL;
180
        echo '    '.basename($_SERVER['argv'][0]).' ... "$REPOS" -t "$TXN" >&2 || exit 1'.PHP_EOL;
181
 
182
    }//end printUsage()
183
 
184
 
185
}//end class
186
 
187
$phpcs = new PHP_CodeSniffer_SVN_Hook();
188
$phpcs->checkRequirements();
189
 
190
$numErrors = $phpcs->process();
191
if ($numErrors !== 0) {
192
    exit(1);
193
}
194
 
195
?>