1<?php 2/** 3 * Gitblame 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 * Gitblame 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.2.2 28 * @link http://pear.php.net/package/PHP_CodeSniffer 29 */ 30class PHP_CodeSniffer_Reports_Gitblame 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 = 'GIT'; 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 preg_match( 53 '|\(.+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+[0-9]+\)|', 54 $line, 55 $blameParts 56 ); 57 58 if (isset($blameParts[0]) === false) { 59 return false; 60 } 61 62 $parts = explode(' ', $blameParts[0]); 63 64 if (count($parts) < 2) { 65 return false; 66 } 67 68 $parts = array_slice($parts, 0, (count($parts) - 2)); 69 $author = preg_replace('|\(|', '', implode($parts, ' ')); 70 return $author; 71 72 }//end getAuthor() 73 74 75 /** 76 * Gets the blame output. 77 * 78 * @param string $filename File to blame. 79 * 80 * @return array 81 */ 82 protected function getBlameContent($filename) 83 { 84 $cwd = getcwd(); 85 86 chdir(dirname($filename)); 87 $command = 'git blame --date=short "'.$filename.'" 2>&1'; 88 $handle = popen($command, 'r'); 89 if ($handle === false) { 90 echo 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL; 91 exit(2); 92 } 93 94 $rawContent = stream_get_contents($handle); 95 fclose($handle); 96 97 $blames = explode("\n", $rawContent); 98 chdir($cwd); 99 100 return $blames; 101 102 }//end getBlameContent() 103 104 105}//end class 106