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: @package_version@ 28 * @link http://pear.php.net/package/PHP_CodeSniffer 29 */ 30class 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 $fileParts = explode(DIRECTORY_SEPARATOR, $filename); 88 $found = false; 89 $location = ''; 90 while (empty($fileParts) === false) { 91 array_pop($fileParts); 92 $location = implode($fileParts, DIRECTORY_SEPARATOR); 93 if (is_dir($location.DIRECTORY_SEPARATOR.'.hg') === true) { 94 $found = true; 95 break; 96 } 97 } 98 99 if ($found === true) { 100 chdir($location); 101 } else { 102 echo 'ERROR: Could not locate .hg directory '.PHP_EOL.PHP_EOL; 103 exit(2); 104 } 105 106 $command = 'hg blame -u -d -v "'.$filename.'" 2>&1'; 107 $handle = popen($command, 'r'); 108 if ($handle === false) { 109 echo 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL; 110 exit(2); 111 } 112 113 $rawContent = stream_get_contents($handle); 114 fclose($handle); 115 116 $blames = explode("\n", $rawContent); 117 chdir($cwd); 118 119 return $blames; 120 121 }//end getBlameContent() 122 123 124}//end class 125