1<?php
2/**
3 * Svnblame report for PHP_CodeSniffer.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Gabriele Santini <gsantini@sqli.com>
10 * @author    Greg Sherwood <gsherwood@squiz.net>
11 * @copyright 2009-2014 SQLI <www.sqli.com>
12 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
13 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
14 * @link      http://pear.php.net/package/PHP_CodeSniffer
15 */
16
17/**
18 * Svnblame report for PHP_CodeSniffer.
19 *
20 * PHP version 5
21 *
22 * @category  PHP
23 * @package   PHP_CodeSniffer
24 * @author    Gabriele Santini <gsantini@sqli.com>
25 * @author    Greg Sherwood <gsherwood@squiz.net>
26 * @copyright 2009-2014 SQLI <www.sqli.com>
27 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
28 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
29 * @version   Release: @package_version@
30 * @link      http://pear.php.net/package/PHP_CodeSniffer
31 */
32class PHP_CodeSniffer_Reports_Svnblame extends PHP_CodeSniffer_Reports_VersionControl
33{
34
35    /**
36     * The name of the report we want in the output
37     *
38     * @var string
39     */
40    protected $reportName = 'SVN';
41
42
43    /**
44     * Extract the author from a blame line.
45     *
46     * @param string $line Line to parse.
47     *
48     * @return mixed string or false if impossible to recover.
49     */
50    protected function getAuthor($line)
51    {
52        $blameParts = array();
53        preg_match('|\s*([^\s]+)\s+([^\s]+)|', $line, $blameParts);
54
55        if (isset($blameParts[2]) === false) {
56            return false;
57        }
58
59        return $blameParts[2];
60
61    }//end getAuthor()
62
63
64    /**
65     * Gets the blame output.
66     *
67     * @param string $filename File to blame.
68     *
69     * @return array
70     */
71    protected function getBlameContent($filename)
72    {
73        $command = 'svn blame "'.$filename.'" 2>&1';
74        $handle  = popen($command, 'r');
75        if ($handle === false) {
76            echo 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
77            exit(2);
78        }
79
80        $rawContent = stream_get_contents($handle);
81        fclose($handle);
82
83        $blames = explode("\n", $rawContent);
84
85        return $blames;
86
87    }//end getBlameContent()
88
89
90}//end class
91