cvs: pear /PHP_CodeSniffer/CodeSniffer/Standards/Zend/Sniffs/Debug CodeAnalyzerSniff.php /PHP_CodeSniffer/CodeSniffer/Standards/Zend/Tests/Debug
CodeAnalyzerUnitTest.inc CodeAnalyzerUnitTest.php
| From: | Greg Sherwood | Date: | Mon, 17 Sep 2007 05:11:46 +0000 |
| Subject: | cvs: pear /PHP_CodeSniffer/CodeSniffer/Standards/Zend/Sniffs/Debug CodeAnalyzerSniff.php /PHP_CodeSniffer/CodeSniffer/Standards/Zend/Tests/Debug CodeAnalyzerUnitTest.inc CodeAnalyzerUnitTest.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-48615@lists.php.net to get a copy of this message | ||
squiz Mon Sep 17 05:11:46 2007 UTC
Added files:
/pear/PHP_CodeSniffer/CodeSniffer/Standards/Zend/Sniffs/Debug
CodeAnalyzerSniff.php
/pear/PHP_CodeSniffer/CodeSniffer/Standards/Zend/Tests/Debug
CodeAnalyzerUnitTest.inc
CodeAnalyzerUnitTest.php
Log:
New sniff to check files using the Zend Code Analyzer tool. Requires a config var zend_ca_path to
be set.
http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Zend/Sniffs/Debug/CodeAnalyzerSniff.php?view=markup&rev=1.1
Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Zend/Sniffs/Debug/CodeAnalyzerSniff.php
+++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Zend/Sniffs/Debug/CodeAnalyzerSniff.php
<?php
/**
* Zend_Sniffs_Debug_CodeAnalyzerSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Holger Kral <holger.kral@zend.com>
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence
BSD Licence
* @version CVS: $Id: CodeAnalyzerSniff.php,v 1.1 2007/09/17 05:11:46 squiz Exp $
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Zend_Sniffs_Debug_CodeAnalyzerSniff.
*
* Runs the Zend Code Analyzer (from Zend Studio) on the file.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Holger Kral <holger.kral@zend.com>
* @author Greg Sherwood <gsherwood@squiz.net>
* @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 Zend_Sniffs_Debug_CodeAnalyzerSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns the token types that this sniff is interested in.
*
* @return array(int)
*/
public function register()
{
return array(T_OPEN_TAG);
}//end register()
/**
* Processes the tokens that this sniff is interested in.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param int $stackPtr The position in the stack where
* the token was found.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
// Because we are analyzing the whole file in one step, execute this method
// only on first occurence of a T_OPEN_TAG.
$prevOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1));
if ($prevOpenTag !== false) {
return;
}
$fileName = $phpcsFile->getFilename();
$analyzerPath = PHP_CodeSniffer::getConfigData('zend_ca_path');
if (is_null($analyzerPath) === true) {
echo "\n--- WARNING ---\n";
echo "The Zend Code Analyzer path is not set.\n";
echo "Run \"phpcs --config-set zend_ca_path
/path/to/ZendCodeAnalyzer\".\n\n";
return;
}
// In the command, 2>&1 is important because the code analyzer sends its
// findings to stderr. $output normally contains only stdout, so using 2>&1
// will pipe even stderr to stdout.
$cmd = $analyzerPath.' '.$fileName.' 2>&1';
// There is the possibility to pass "--ide" as an option to the analyzer. This
// would result in an output format which would be easier to parse. The problem
// here is that no cleartext error messages are returnd; only error-code-labels.
// So for a start we go for cleartext output.
$exitCode = exec($cmd, $output, $retval);
// $exitCode is the last line of $output if no error occures, on error it is numeric.
// try to handle various errorcondition an provide useful error reporting
// || (is_numeric($retval) && $retval > 0)
if (is_numeric($exitCode) === true && $exitCode > 0) {
if (is_array($output) === true) {
$msg = join('\n', $output);
}
throw new PHP_CodeSniffer_Exception("Failed invoking ZendCodeAnalyzer, exitcode was
[$exitCode], retval was [$retval], output was [$msg]");
}
if (is_array($output) === true) {
$tokens = $phpcsFile->getTokens();
foreach ($output as $finding) {
// The first two lines of analyzer output contain something like this:
// > Zend Code Analyzer 1.2.2
// > Analyzing <filename>...
// So skip these...
$res = eregi("^.+\(line ([0-9]+)\):(.+)$", $finding, $regs);
if ($regs === null || $res === false) {
continue;
}
// Find the token at the start of the line.
$lineToken = null;
foreach ($tokens as $ptr => $info) {
if ($info['line'] == $regs[1]) {
$lineToken = $ptr;
break;
}
}
if ($lineToken !== null) {
$phpcsFile->addWarning(trim($regs[2]), $ptr);
}
}//end foreach
}//end if
}//end process()
}//end class
?>
http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Zend/Tests/Debug/CodeAnalyzerUnitTest.inc?view=markup&rev=1.1
Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Zend/Tests/Debug/CodeAnalyzerUnitTest.inc
+++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Zend/Tests/Debug/CodeAnalyzerUnitTest.inc
<?php
function myFunction($variable) {
// $variable is unused.
echo 'hi';
}
?>
http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Zend/Tests/Debug/CodeAnalyzerUnitTest.php?view=markup&rev=1.1
Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Zend/Tests/Debug/CodeAnalyzerUnitTest.php
+++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Zend/Tests/Debug/CodeAnalyzerUnitTest.php
<?php
/**
* Unit test class for the CodeAnalyzer sniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence
BSD Licence
* @version CVS: $Id: CodeAnalyzerUnitTest.php,v 1.1 2007/09/17 05:11:46 squiz Exp $
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Unit test class for the CodeAnalyzer sniff.
*
* A sniff unit test checks a .inc file for expected violations of a single
* coding standard. Expected errors and warnings are stored in this class.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @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 Zend_Tests_Debug_CodeAnalyzerUnitTest extends AbstractSniffUnitTest
{
/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @return array(int => int)
*/
public function getErrorList()
{
return array();
}//end getErrorList()
/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @return array(int => int)
*/
public function getWarningList()
{
return array(
2 => 1,
);
}//end getWarningList()
}//end class
?>