• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 15253296250

26 May 2025 11:55AM UTC coverage: 78.632% (+0.3%) from 78.375%
15253296250

Pull #1105

github

web-flow
Merge d9441d98f into caf806050
Pull Request #1105: Skip tests when 'git' command is not available

19665 of 25009 relevant lines covered (78.63%)

88.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Reports/Gitblame.php
1
<?php
2
/**
3
 * Git blame report for PHP_CodeSniffer.
4
 *
5
 * @author    Ben Selby <benmatselby@gmail.com>
6
 * @author    Greg Sherwood <gsherwood@squiz.net>
7
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
8
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
9
 */
10

11
namespace PHP_CodeSniffer\Reports;
12

13
use PHP_CodeSniffer\Exceptions\DeepExitException;
14
use PHP_CodeSniffer\Util\ExitCode;
15

16
class Gitblame extends VersionControl
17
{
18

19
    /**
20
     * The name of the report we want in the output
21
     *
22
     * @var string
23
     */
24
    protected $reportName = 'GIT';
25

26

27
    /**
28
     * Extract the author from a blame line.
29
     *
30
     * @param string $line Line to parse.
31
     *
32
     * @return mixed string or false if impossible to recover.
33
     */
34
    protected function getAuthor($line)
×
35
    {
36
        $blameParts = [];
×
37
        $line       = preg_replace('|\s+|', ' ', $line);
×
38
        preg_match(
×
39
            '|\(.+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+[0-9]+\)|',
×
40
            $line,
×
41
            $blameParts
×
42
        );
×
43

44
        if (isset($blameParts[0]) === false) {
×
45
            return false;
×
46
        }
47

48
        $parts = explode(' ', $blameParts[0]);
×
49

50
        if (count($parts) < 2) {
×
51
            return false;
×
52
        }
53

54
        $parts  = array_slice($parts, 0, (count($parts) - 2));
×
55
        $author = preg_replace('|\(|', '', implode(' ', $parts));
×
56
        return $author;
×
57

58
    }//end getAuthor()
59

60

61
    /**
62
     * Gets the blame output.
63
     *
64
     * @param string $filename File to blame.
65
     *
66
     * @return array
67
     * @throws \PHP_CodeSniffer\Exceptions\DeepExitException
68
     */
69
    protected function getBlameContent($filename)
×
70
    {
71
        $cwd = getcwd();
×
72

73
        chdir(dirname($filename));
×
74
        $command = 'git blame --date=short "'.basename($filename).'" 2>&1';
×
75
        $handle  = popen($command, 'r');
×
76
        if ($handle === false) {
×
77
            $error = 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
×
78
            throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
79
        }
80

81
        $rawContent = stream_get_contents($handle);
×
82
        pclose($handle);
×
83

84
        $blames = explode("\n", $rawContent);
×
85
        chdir($cwd);
×
86

87
        return $blames;
×
88

89
    }//end getBlameContent()
90

91

92
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc