annotate vendor/squizlabs/php_codesniffer/src/Reports/Gitblame.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@17 1 <?php
Chris@17 2 /**
Chris@17 3 * Git blame report for PHP_CodeSniffer.
Chris@17 4 *
Chris@17 5 * @author Ben Selby <benmatselby@gmail.com>
Chris@17 6 * @author Greg Sherwood <gsherwood@squiz.net>
Chris@17 7 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
Chris@17 8 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
Chris@17 9 */
Chris@17 10
Chris@17 11 namespace PHP_CodeSniffer\Reports;
Chris@17 12
Chris@17 13 use PHP_CodeSniffer\Exceptions\DeepExitException;
Chris@17 14
Chris@17 15 class Gitblame extends VersionControl
Chris@17 16 {
Chris@17 17
Chris@17 18 /**
Chris@17 19 * The name of the report we want in the output
Chris@17 20 *
Chris@17 21 * @var string
Chris@17 22 */
Chris@17 23 protected $reportName = 'GIT';
Chris@17 24
Chris@17 25
Chris@17 26 /**
Chris@17 27 * Extract the author from a blame line.
Chris@17 28 *
Chris@17 29 * @param string $line Line to parse.
Chris@17 30 *
Chris@17 31 * @return mixed string or false if impossible to recover.
Chris@17 32 */
Chris@17 33 protected function getAuthor($line)
Chris@17 34 {
Chris@17 35 $blameParts = [];
Chris@17 36 $line = preg_replace('|\s+|', ' ', $line);
Chris@17 37 preg_match(
Chris@17 38 '|\(.+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+[0-9]+\)|',
Chris@17 39 $line,
Chris@17 40 $blameParts
Chris@17 41 );
Chris@17 42
Chris@17 43 if (isset($blameParts[0]) === false) {
Chris@17 44 return false;
Chris@17 45 }
Chris@17 46
Chris@17 47 $parts = explode(' ', $blameParts[0]);
Chris@17 48
Chris@17 49 if (count($parts) < 2) {
Chris@17 50 return false;
Chris@17 51 }
Chris@17 52
Chris@17 53 $parts = array_slice($parts, 0, (count($parts) - 2));
Chris@17 54 $author = preg_replace('|\(|', '', implode($parts, ' '));
Chris@17 55 return $author;
Chris@17 56
Chris@17 57 }//end getAuthor()
Chris@17 58
Chris@17 59
Chris@17 60 /**
Chris@17 61 * Gets the blame output.
Chris@17 62 *
Chris@17 63 * @param string $filename File to blame.
Chris@17 64 *
Chris@17 65 * @return array
Chris@17 66 */
Chris@17 67 protected function getBlameContent($filename)
Chris@17 68 {
Chris@17 69 $cwd = getcwd();
Chris@17 70
Chris@17 71 chdir(dirname($filename));
Chris@17 72 $command = 'git blame --date=short "'.$filename.'" 2>&1';
Chris@17 73 $handle = popen($command, 'r');
Chris@17 74 if ($handle === false) {
Chris@17 75 $error = 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
Chris@17 76 throw new DeepExitException($error, 3);
Chris@17 77 }
Chris@17 78
Chris@17 79 $rawContent = stream_get_contents($handle);
Chris@17 80 fclose($handle);
Chris@17 81
Chris@17 82 $blames = explode("\n", $rawContent);
Chris@17 83 chdir($cwd);
Chris@17 84
Chris@17 85 return $blames;
Chris@17 86
Chris@17 87 }//end getBlameContent()
Chris@17 88
Chris@17 89
Chris@17 90 }//end class