comparison vendor/squizlabs/php_codesniffer/src/Reports/Gitblame.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
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/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
9 */
10
11 namespace PHP_CodeSniffer\Reports;
12
13 use PHP_CodeSniffer\Exceptions\DeepExitException;
14
15 class Gitblame extends VersionControl
16 {
17
18 /**
19 * The name of the report we want in the output
20 *
21 * @var string
22 */
23 protected $reportName = 'GIT';
24
25
26 /**
27 * Extract the author from a blame line.
28 *
29 * @param string $line Line to parse.
30 *
31 * @return mixed string or false if impossible to recover.
32 */
33 protected function getAuthor($line)
34 {
35 $blameParts = [];
36 $line = preg_replace('|\s+|', ' ', $line);
37 preg_match(
38 '|\(.+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+[0-9]+\)|',
39 $line,
40 $blameParts
41 );
42
43 if (isset($blameParts[0]) === false) {
44 return false;
45 }
46
47 $parts = explode(' ', $blameParts[0]);
48
49 if (count($parts) < 2) {
50 return false;
51 }
52
53 $parts = array_slice($parts, 0, (count($parts) - 2));
54 $author = preg_replace('|\(|', '', implode($parts, ' '));
55 return $author;
56
57 }//end getAuthor()
58
59
60 /**
61 * Gets the blame output.
62 *
63 * @param string $filename File to blame.
64 *
65 * @return array
66 */
67 protected function getBlameContent($filename)
68 {
69 $cwd = getcwd();
70
71 chdir(dirname($filename));
72 $command = 'git blame --date=short "'.$filename.'" 2>&1';
73 $handle = popen($command, 'r');
74 if ($handle === false) {
75 $error = 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
76 throw new DeepExitException($error, 3);
77 }
78
79 $rawContent = stream_get_contents($handle);
80 fclose($handle);
81
82 $blames = explode("\n", $rawContent);
83 chdir($cwd);
84
85 return $blames;
86
87 }//end getBlameContent()
88
89
90 }//end class