Initial Commit
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
<?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-2011 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
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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-2011 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
||||
* @version Release: 1.3.3
|
||||
* @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) {
|
||||
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 returnwd; 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 error conditions and provide useful
|
||||
// error reporting.
|
||||
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 = preg_match("/^.+\(line ([0-9]+)\):(.+)$/", $finding, $regs);
|
||||
if (empty($regs) === true || $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, 'ExternalTool');
|
||||
}
|
||||
}//end foreach
|
||||
}//end if
|
||||
|
||||
}//end process()
|
||||
|
||||
}//end class
|
||||
?>
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend_Sniffs_Files_ClosingTagsSniff.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @author Marc McIntyre <mmcintyre@squiz.net>
|
||||
* @copyright 2006-2011 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
|
||||
*/
|
||||
|
||||
/**
|
||||
* Zend_Sniffs_Files_LineEndingsSniff.
|
||||
*
|
||||
* Checks that the file does not end with a closing tag.
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @author Marc McIntyre <mmcintyre@squiz.net>
|
||||
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
||||
* @version Release: 1.3.3
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
class Zend_Sniffs_Files_ClosingTagSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(T_CLOSE_TAG);
|
||||
|
||||
}//end register()
|
||||
|
||||
|
||||
/**
|
||||
* Processes this sniff, when one of its tokens is encountered.
|
||||
*
|
||||
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
||||
* @param int $stackPtr The position of the current token in
|
||||
* the stack passed in $tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
|
||||
if ($next === false) {
|
||||
$error = 'A closing tag is not permitted at the end of a PHP file';
|
||||
$phpcsFile->addError($error, $stackPtr, 'NotAllowed');
|
||||
}
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,252 @@
|
||||
<?php
|
||||
/**
|
||||
* Squiz_Sniffs_NamingConventions_ValidVariableNameSniff.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @author Marc McIntyre <mmcintyre@squiz.net>
|
||||
* @copyright 2006-2011 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
|
||||
*/
|
||||
|
||||
if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
|
||||
throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found');
|
||||
}
|
||||
|
||||
/**
|
||||
* Squiz_Sniffs_NamingConventions_ValidVariableNameSniff.
|
||||
*
|
||||
* Checks the naming of variables and member variables.
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @author Marc McIntyre <mmcintyre@squiz.net>
|
||||
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
||||
* @version Release: 1.3.3
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
class Zend_Sniffs_NamingConventions_ValidVariableNameSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
|
||||
{
|
||||
|
||||
/**
|
||||
* Tokens to ignore so that we can find a DOUBLE_COLON.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_ignore = array(
|
||||
T_WHITESPACE,
|
||||
T_COMMENT,
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
||||
* @param int $stackPtr The position of the current token in the
|
||||
* stack passed in $tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
$varName = ltrim($tokens[$stackPtr]['content'], '$');
|
||||
|
||||
$phpReservedVars = array(
|
||||
'_SERVER',
|
||||
'_GET',
|
||||
'_POST',
|
||||
'_REQUEST',
|
||||
'_SESSION',
|
||||
'_ENV',
|
||||
'_COOKIE',
|
||||
'_FILES',
|
||||
'GLOBALS',
|
||||
);
|
||||
|
||||
// If it's a php reserved var, then its ok.
|
||||
if (in_array($varName, $phpReservedVars) === true) {
|
||||
return;
|
||||
}
|
||||
|
||||
$objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
|
||||
if ($tokens[$objOperator]['code'] === T_OBJECT_OPERATOR) {
|
||||
// Check to see if we are using a variable from an object.
|
||||
$var = $phpcsFile->findNext(array(T_WHITESPACE), ($objOperator + 1), null, true);
|
||||
if ($tokens[$var]['code'] === T_STRING) {
|
||||
// Either a var name or a function call, so check for bracket.
|
||||
$bracket = $phpcsFile->findNext(array(T_WHITESPACE), ($var + 1), null, true);
|
||||
|
||||
if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
|
||||
$objVarName = $tokens[$var]['content'];
|
||||
|
||||
// There is no way for us to know if the var is public or private,
|
||||
// so we have to ignore a leading underscore if there is one and just
|
||||
// check the main part of the variable name.
|
||||
$originalVarName = $objVarName;
|
||||
if (substr($objVarName, 0, 1) === '_') {
|
||||
$objVarName = substr($objVarName, 1);
|
||||
}
|
||||
|
||||
if (PHP_CodeSniffer::isCamelCaps($objVarName, false, true, false) === false) {
|
||||
$error = 'Variable "%s" is not in valid camel caps format';
|
||||
$data = array($originalVarName);
|
||||
$phpcsFile->addError($error, $var, 'NotCamelCaps', $data);
|
||||
} else if (preg_match('|\d|', $objVarName)) {
|
||||
$warning = 'Variable "%s" contains numbers but this is discouraged';
|
||||
$data = array($originalVarName);
|
||||
$phpcsFile->addWarning($warning, $stackPtr, 'ContainsNumbers', $data);
|
||||
}
|
||||
}//end if
|
||||
}//end if
|
||||
}//end if
|
||||
|
||||
// There is no way for us to know if the var is public or private,
|
||||
// so we have to ignore a leading underscore if there is one and just
|
||||
// check the main part of the variable name.
|
||||
$originalVarName = $varName;
|
||||
if (substr($varName, 0, 1) === '_') {
|
||||
$objOperator = $phpcsFile->findPrevious(array(T_WHITESPACE), ($stackPtr - 1), null, true);
|
||||
if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
|
||||
// The variable lives within a class, and is referenced like
|
||||
// this: MyClass::$_variable, so we don't know its scope.
|
||||
$inClass = true;
|
||||
} else {
|
||||
$inClass = $phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE));
|
||||
}
|
||||
|
||||
if ($inClass === true) {
|
||||
$varName = substr($varName, 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
|
||||
$error = 'Variable "%s" is not in valid camel caps format';
|
||||
$data = array($originalVarName);
|
||||
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
|
||||
} else if (preg_match('|\d|', $varName)) {
|
||||
$warning = 'Variable "%s" contains numbers but this is discouraged';
|
||||
$data = array($originalVarName);
|
||||
$phpcsFile->addWarning($warning, $stackPtr, 'ContainsNumbers', $data);
|
||||
}
|
||||
|
||||
}//end processVariable()
|
||||
|
||||
|
||||
/**
|
||||
* Processes class member variables.
|
||||
*
|
||||
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
||||
* @param int $stackPtr The position of the current token in the
|
||||
* stack passed in $tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
$varName = ltrim($tokens[$stackPtr]['content'], '$');
|
||||
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
|
||||
$public = ($memberProps['scope'] === 'public');
|
||||
|
||||
if ($public === true) {
|
||||
if (substr($varName, 0, 1) === '_') {
|
||||
$error = 'Public member variable "%s" must not contain a leading underscore';
|
||||
$data = array($varName);
|
||||
$phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (substr($varName, 0, 1) !== '_') {
|
||||
$scope = ucfirst($memberProps['scope']);
|
||||
$error = '%s member variable "%s" must contain a leading underscore';
|
||||
$data = array(
|
||||
$scope,
|
||||
$varName,
|
||||
);
|
||||
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (PHP_CodeSniffer::isCamelCaps($varName, false, $public, false) === false) {
|
||||
$error = 'Variable "%s" is not in valid camel caps format';
|
||||
$data = array($varName);
|
||||
$phpcsFile->addError($error, $stackPtr, 'MemberVarNotCamelCaps', $data);
|
||||
} else if (preg_match('|\d|', $varName)) {
|
||||
$warning = 'Variable "%s" contains numbers but this is discouraged';
|
||||
$data = array($varName);
|
||||
$phpcsFile->addWarning($warning, $stackPtr, 'MemberVarContainsNumbers', $data);
|
||||
}
|
||||
|
||||
}//end processMemberVar()
|
||||
|
||||
|
||||
/**
|
||||
* Processes the variable found within a double quoted string.
|
||||
*
|
||||
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
||||
* @param int $stackPtr The position of the double quoted
|
||||
* string.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
$phpReservedVars = array(
|
||||
'_SERVER',
|
||||
'_GET',
|
||||
'_POST',
|
||||
'_REQUEST',
|
||||
'_SESSION',
|
||||
'_ENV',
|
||||
'_COOKIE',
|
||||
'_FILES',
|
||||
'GLOBALS',
|
||||
);
|
||||
|
||||
if (preg_match_all('|[^\\\]\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
|
||||
foreach ($matches[1] as $varName) {
|
||||
// If it's a php reserved var, then its ok.
|
||||
if (in_array($varName, $phpReservedVars) === true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// There is no way for us to know if the var is public or private,
|
||||
// so we have to ignore a leading underscore if there is one and just
|
||||
// check the main part of the variable name.
|
||||
$originalVarName = $varName;
|
||||
if (substr($varName, 0, 1) === '_') {
|
||||
if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE)) === true) {
|
||||
$varName = substr($varName, 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
|
||||
$varName = $matches[0];
|
||||
$error = 'Variable "%s" is not in valid camel caps format';
|
||||
$data = array($originalVarName);
|
||||
$phpcsFile->addError($error, $stackPtr, 'StringVarNotCamelCaps', $data);
|
||||
} else if (preg_match('|\d|', $varName)) {
|
||||
$warning = 'Variable "%s" contains numbers but this is discouraged';
|
||||
$data = array($originalVarName);
|
||||
$phpcsFile->addWarning($warning, $stackPtr, 'StringVarContainsNumbers', $data);
|
||||
}
|
||||
}
|
||||
}//end if
|
||||
|
||||
}//end processVariableInString()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user