forked from squizlabs/PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added report type --report=hgblame to show number of errors/warnings …
…committed by authors in a Mercurial repository git-svn-id: http://svn.php.net/repository/pear/packages/PHP_CodeSniffer/trunk@313191 c90b9560-bf6c-de11-be94-00142212c4b1
- Loading branch information
Showing
7 changed files
with
401 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
/** | ||
* Mercurial report for PHP_CodeSniffer. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Ben Selby <[email protected]> | ||
* @copyright 2009 SQLI <www.sqli.com> | ||
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
/** | ||
* Mercurial report for PHP_CodeSniffer. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Ben Selby <[email protected]> | ||
* @copyright 2009 SQLI <www.sqli.com> | ||
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence | ||
* @version Release: @package_version@ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
class PHP_CodeSniffer_Reports_Hgblame extends PHP_CodeSniffer_Reports_VersionControl | ||
{ | ||
|
||
/** | ||
* The name of the report we want in the output | ||
* | ||
* @var string | ||
*/ | ||
protected $reportName = 'MERCURIAL'; | ||
|
||
|
||
/** | ||
* Extract the author from a blame line. | ||
* | ||
* @param string $line Line to parse. | ||
* | ||
* @return mixed string or false if impossible to recover. | ||
*/ | ||
protected function getAuthor($line) | ||
{ | ||
$blameParts = array(); | ||
$line = preg_replace('|\s+|', ' ', $line); | ||
|
||
preg_match( | ||
'|(.+[0-9]{2}:[0-9]{2}:[0-9]{2}\s[0-9]{4}\s.[0-9]{4}:)|', | ||
$line, | ||
$blameParts | ||
); | ||
|
||
if (isset($blameParts[0]) === false) { | ||
return false; | ||
} | ||
|
||
$parts = explode(' ', $blameParts[0]); | ||
|
||
if (count($parts) < 6) { | ||
return false; | ||
} | ||
|
||
$parts = array_slice($parts, 0, (count($parts) - 6)); | ||
|
||
return trim(preg_replace('|<.+>|', '', implode($parts, ' '))); | ||
|
||
}//end getAuthor() | ||
|
||
|
||
/** | ||
* Gets the blame output. | ||
* | ||
* @param string $filename File to blame. | ||
* | ||
* @return array | ||
*/ | ||
protected function getBlameContent($filename) | ||
{ | ||
$cwd = getcwd(); | ||
|
||
if (PHP_CODESNIFFER_VERBOSITY > 0) { | ||
echo 'Getting MERCURIAL blame info for '.basename($filename).'... '; | ||
} | ||
|
||
$fileParts = explode('/', $filename); | ||
$found = false; | ||
$location = ''; | ||
while (empty($fileParts) === false) { | ||
array_pop($fileParts); | ||
$location = implode($fileParts, '/'); | ||
if (is_dir($location.'/.hg') === true) { | ||
$found = true; | ||
break; | ||
} | ||
} | ||
|
||
if ($found === true) { | ||
chdir($location); | ||
} else { | ||
echo 'ERROR: Could not locate .hg directory '.PHP_EOL.PHP_EOL; | ||
exit(2); | ||
} | ||
|
||
$command = 'hg blame -u -d -v '.$filename; | ||
$handle = popen($command, 'r'); | ||
if ($handle === false) { | ||
echo 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL; | ||
exit(2); | ||
} | ||
|
||
$rawContent = stream_get_contents($handle); | ||
fclose($handle); | ||
|
||
if (PHP_CODESNIFFER_VERBOSITY > 0) { | ||
echo 'DONE'.PHP_EOL; | ||
} | ||
|
||
$blames = explode("\n", $rawContent); | ||
chdir($cwd); | ||
|
||
return $blames; | ||
|
||
}//end getBlameContent() | ||
|
||
|
||
}//end class | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/** | ||
* Tests for the Hgblame report of PHP_CodeSniffer. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Ben Selby <[email protected]> | ||
* @copyright 2009 SQLI <www.sqli.com> | ||
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
require_once 'PHPUnit/Framework/TestCase.php'; | ||
require_once dirname(__FILE__).'/AbstractTestCase.php'; | ||
require_once dirname(__FILE__).'/Mock/Hgblame.php'; | ||
|
||
if (is_file(dirname(__FILE__).'/../../../CodeSniffer.php') === true) { | ||
// We are not installed. | ||
include_once dirname(__FILE__).'/../../../CodeSniffer/Reports/VersionControl.php'; | ||
} else { | ||
include_once 'PHP/CodeSniffer/Reports/VersionControl.php'; | ||
} | ||
|
||
/** | ||
* Tests for the Hgblame report of PHP_CodeSniffer. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Ben Selby <[email protected]> | ||
* @copyright 2009 SQLI <www.sqli.com> | ||
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence | ||
* @version Release: @package_version@ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
class Core_Reports_HgblameTest extends Core_Reports_AbstractTestCase | ||
{ | ||
|
||
|
||
/** | ||
* Test standard generation | ||
* | ||
* @return void | ||
*/ | ||
public function testGenerate() | ||
{ | ||
$fullReport = new PHP_CodeSniffer_Reports_Mock_Hgblame(); | ||
$generated = $this->getFixtureReport($fullReport); | ||
$generatedLines = explode(PHP_EOL, $generated); | ||
$this->assertGreaterThan(10, count($generatedLines)); | ||
|
||
}//end testGenerate() | ||
|
||
|
||
/** | ||
* Test author recovering from a hg blame line | ||
* | ||
* @param string $line The hg blame output | ||
* @param string $expected The author name | ||
* | ||
* @dataProvider provideDataForGetHgAuthor | ||
* | ||
* @return void | ||
*/ | ||
public function testGetHgAuthor($line, $expected) | ||
{ | ||
$fullReport = new PHP_CodeSniffer_Reports_Mock_Hgblame(); | ||
$author = $fullReport->testGetHgAuthor($line); | ||
$this->assertEquals($expected, $author); | ||
|
||
}//end testGetHgAuthor() | ||
|
||
|
||
/** | ||
* Data provider for testGetHgAuthor | ||
* | ||
* @return array | ||
*/ | ||
public static function provideDataForGetHgAuthor() | ||
{ | ||
return array( | ||
array('Ben Selby <[email protected]> Sun May 29 00:05:15 2011 +0300: /**', 'Ben Selby'), | ||
array(' benmatselby@somewhere Sun May 29 00:05:15 2011 +0300: /**', 'benmatselby@somewhere'), | ||
array('Ben Selby <[email protected]> Tue Apr 26 00:36:36 2011 +0300: * // Some random text with dates (e.g. 2011-05-01 12:30:00, Y-m-d H:i:s', 'Ben Selby'), | ||
); | ||
|
||
}//end provideDataForGetHgAuthor() | ||
|
||
|
||
}//end class | ||
|
||
?> |
Oops, something went wrong.