~ubuntu-branches/ubuntu/vivid/php-codesniffer/vivid

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.5/CodeSniffer/Reports/Hgblame.php

  • Committer: Package Import Robot
  • Author(s): David Prévot, Greg Sherwood, Alexey, Emily, David Prévot
  • Date: 2014-09-26 13:44:35 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140926134435-wvjq16miqq4d60y0
Tags: 1.5.5-1
[ Greg Sherwood ]
* Improved closure support in Generic ScopeIndentSniff
* Improved indented PHP tag support in Generic ScopeIndentSniff
* Standards can now be located within hidden directories
 (further fix for bug #20323)
* Fixed bug #20373 : Inline comment sniff tab handling way
* Fixed bug #20378 : Report appended to existing file if no errors
  found in run
* Fixed bug #20381 : Invalid "Comment closer must be on a new line"
* PHP tokenizer no longer converts class/function names to special
  tokens types
* Fixed bug #20386 : Squiz.Commenting.ClassComment.SpacingBefore
  thrown if first block comment
* Squiz and PEAR FunctionCommentSnif now support _()
* PEAR ValidFunctionNameSniff no longer throws an error for _()
* Fixed bug #248 : FunctionCommentSniff expects ampersand on param name
* Fixed bug #248 in Squiz sniff as well
* Fixed bug #265 : False positives with type hints in ForbiddenFunctionsSniff
* Prepare for 1.5.5 release

[ Alexey ]
* Allowed single undersored methods and functions

[ Emily ]
* Added var_dump to discouraged functions sniff

[ David Prévot ]
* Revert "Add XS-Testsuite still needed for ci.d.n"
* Add self to uploaders
* Bump standards version to 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Mercurial report for PHP_CodeSniffer.
 
4
 *
 
5
 * PHP version 5
 
6
 *
 
7
 * @category  PHP
 
8
 * @package   PHP_CodeSniffer
 
9
 * @author    Ben Selby <benmatselby@gmail.com>
 
10
 * @copyright 2009-2014 SQLI <www.sqli.com>
 
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
 * Mercurial report for PHP_CodeSniffer.
 
18
 *
 
19
 * PHP version 5
 
20
 *
 
21
 * @category  PHP
 
22
 * @package   PHP_CodeSniffer
 
23
 * @author    Ben Selby <benmatselby@gmail.com>
 
24
 * @copyright 2009-2014 SQLI <www.sqli.com>
 
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: 1.5.5
 
28
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
29
 */
 
30
class PHP_CodeSniffer_Reports_Hgblame extends PHP_CodeSniffer_Reports_VersionControl
 
31
{
 
32
 
 
33
    /**
 
34
     * The name of the report we want in the output
 
35
     *
 
36
     * @var string
 
37
     */
 
38
    protected $reportName = 'MERCURIAL';
 
39
 
 
40
 
 
41
    /**
 
42
     * Extract the author from a blame line.
 
43
     *
 
44
     * @param string $line Line to parse.
 
45
     *
 
46
     * @return mixed string or false if impossible to recover.
 
47
     */
 
48
    protected function getAuthor($line)
 
49
    {
 
50
        $blameParts = array();
 
51
        $line       = preg_replace('|\s+|', ' ', $line);
 
52
 
 
53
        preg_match(
 
54
            '|(.+[0-9]{2}:[0-9]{2}:[0-9]{2}\s[0-9]{4}\s.[0-9]{4}:)|',
 
55
            $line,
 
56
            $blameParts
 
57
        );
 
58
 
 
59
        if (isset($blameParts[0]) === false) {
 
60
            return false;
 
61
        }
 
62
 
 
63
        $parts = explode(' ', $blameParts[0]);
 
64
 
 
65
        if (count($parts) < 6) {
 
66
            return false;
 
67
        }
 
68
 
 
69
        $parts = array_slice($parts, 0, (count($parts) - 6));
 
70
 
 
71
        return trim(preg_replace('|<.+>|', '', implode($parts, ' ')));
 
72
 
 
73
    }//end getAuthor()
 
74
 
 
75
 
 
76
    /**
 
77
     * Gets the blame output.
 
78
     *
 
79
     * @param string $filename File to blame.
 
80
     *
 
81
     * @return array
 
82
     */
 
83
    protected function getBlameContent($filename)
 
84
    {
 
85
        $cwd = getcwd();
 
86
 
 
87
        if (PHP_CODESNIFFER_VERBOSITY > 0) {
 
88
            echo 'Getting MERCURIAL blame info for '.basename($filename).'... ';
 
89
        }
 
90
 
 
91
        $fileParts = explode(DIRECTORY_SEPARATOR, $filename);
 
92
        $found     = false;
 
93
        $location  = '';
 
94
        while (empty($fileParts) === false) {
 
95
            array_pop($fileParts);
 
96
            $location = implode($fileParts, DIRECTORY_SEPARATOR);
 
97
            if (is_dir($location.DIRECTORY_SEPARATOR.'.hg') === true) {
 
98
                $found = true;
 
99
                break;
 
100
            }
 
101
        }
 
102
 
 
103
        if ($found === true) {
 
104
            chdir($location);
 
105
        } else {
 
106
            echo 'ERROR: Could not locate .hg directory '.PHP_EOL.PHP_EOL;
 
107
            exit(2);
 
108
        }
 
109
 
 
110
        $command = 'hg blame -u -d -v "'.$filename.'"';
 
111
        $handle  = popen($command, 'r');
 
112
        if ($handle === false) {
 
113
            echo 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
 
114
            exit(2);
 
115
        }
 
116
 
 
117
        $rawContent = stream_get_contents($handle);
 
118
        fclose($handle);
 
119
 
 
120
        if (PHP_CODESNIFFER_VERBOSITY > 0) {
 
121
            echo 'DONE'.PHP_EOL;
 
122
        }
 
123
 
 
124
        $blames = explode("\n", $rawContent);
 
125
        chdir($cwd);
 
126
 
 
127
        return $blames;
 
128
 
 
129
    }//end getBlameContent()
 
130
 
 
131
 
 
132
}//end class
 
133
 
 
134
?>