Initial Commit
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
/**
|
||||
* Generic_Sniffs_Functions_CallTimePassByReferenceSniff.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Florian Grandel <jerico.dev@gmail.com>
|
||||
* @copyright 2009 Florian Grandel
|
||||
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* Generic_Sniffs_Functions_CallTimePassByReferenceSniff.
|
||||
*
|
||||
* Ensures that variables are not passed by reference when calling a function.
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Florian Grandel <jerico.dev@gmail.com>
|
||||
* @copyright 2009 Florian Grandel
|
||||
* @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 Generic_Sniffs_Functions_CallTimePassByReferenceSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(T_STRING);
|
||||
|
||||
}//end register()
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
// Skip tokens that are the names of functions or classes
|
||||
// within their definitions. For example: function myFunction...
|
||||
// "myFunction" is T_STRING but we should skip because it is not a
|
||||
// function or method *call*.
|
||||
$functionName = $stackPtr;
|
||||
$findTokens = array_merge(
|
||||
PHP_CodeSniffer_Tokens::$emptyTokens,
|
||||
array(T_BITWISE_AND)
|
||||
);
|
||||
|
||||
$functionKeyword = $phpcsFile->findPrevious(
|
||||
$findTokens,
|
||||
($stackPtr - 1),
|
||||
null,
|
||||
true
|
||||
);
|
||||
|
||||
if ($tokens[$functionKeyword]['code'] === T_FUNCTION
|
||||
|| $tokens[$functionKeyword]['code'] === T_CLASS
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the next non-whitespace token after the function or method call
|
||||
// is not an opening parenthesis then it cant really be a *call*.
|
||||
$openBracket = $phpcsFile->findNext(
|
||||
PHP_CodeSniffer_Tokens::$emptyTokens,
|
||||
($functionName + 1),
|
||||
null,
|
||||
true
|
||||
);
|
||||
|
||||
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
|
||||
return;
|
||||
}
|
||||
|
||||
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
|
||||
|
||||
$nextSeparator = $openBracket;
|
||||
while (($nextSeparator = $phpcsFile->findNext(T_VARIABLE, ($nextSeparator + 1), $closeBracket)) !== false) {
|
||||
// Make sure the variable belongs directly to this function call
|
||||
// and is not inside a nested function call or array.
|
||||
$brackets = $tokens[$nextSeparator]['nested_parenthesis'];
|
||||
$lastBracket = array_pop($brackets);
|
||||
if ($lastBracket !== $closeBracket) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Checking this: $value = my_function(...[*]$arg...).
|
||||
$tokenBefore = $phpcsFile->findPrevious(
|
||||
PHP_CodeSniffer_Tokens::$emptyTokens,
|
||||
($nextSeparator - 1),
|
||||
null,
|
||||
true
|
||||
);
|
||||
|
||||
if ($tokens[$tokenBefore]['code'] === T_BITWISE_AND) {
|
||||
// Checking this: $value = my_function(...[*]&$arg...).
|
||||
$tokenBefore = $phpcsFile->findPrevious(
|
||||
PHP_CodeSniffer_Tokens::$emptyTokens,
|
||||
($tokenBefore - 1),
|
||||
null,
|
||||
true
|
||||
);
|
||||
|
||||
// We have to exclude all uses of T_BITWISE_AND that are not
|
||||
// references. We use a blacklist approach as we prefer false
|
||||
// positives to not identifiying a pass-by-reference call at all.
|
||||
// The blacklist may not yet be complete.
|
||||
switch ($tokens[$tokenBefore]['code']) {
|
||||
case T_VARIABLE:
|
||||
case T_CLOSE_PARENTHESIS:
|
||||
// In these cases T_BITWISE_AND represents
|
||||
// the bitwise and operator.
|
||||
continue;
|
||||
break;
|
||||
|
||||
default:
|
||||
// T_BITWISE_AND represents a pass-by-reference.
|
||||
$error = 'Call-time pass-by-reference calls are prohibited';
|
||||
$phpcsFile->addError($error, $tokenBefore, 'NotAllowed');
|
||||
break;
|
||||
}
|
||||
}//end if
|
||||
}//end while
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
/**
|
||||
* Generic_Sniffs_Functions_FunctionCallArgumentSpacingSniff.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/**
|
||||
* Generic_Sniffs_Functions_FunctionCallArgumentSpacingSniff.
|
||||
*
|
||||
* Checks that calls to methods and functions are spaced correctly.
|
||||
*
|
||||
* @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 Generic_Sniffs_Functions_FunctionCallArgumentSpacingSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(T_STRING);
|
||||
|
||||
}//end register()
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
// Skip tokens that are the names of functions or classes
|
||||
// within their definitions. For example:
|
||||
// function myFunction...
|
||||
// "myFunction" is T_STRING but we should skip because it is not a
|
||||
// function or method *call*.
|
||||
$functionName = $stackPtr;
|
||||
$ignoreTokens = PHP_CodeSniffer_Tokens::$emptyTokens;
|
||||
$ignoreTokens[] = T_BITWISE_AND;
|
||||
$functionKeyword = $phpcsFile->findPrevious($ignoreTokens, ($stackPtr - 1), null, true);
|
||||
if ($tokens[$functionKeyword]['code'] === T_FUNCTION || $tokens[$functionKeyword]['code'] === T_CLASS) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the next non-whitespace token after the function or method call
|
||||
// is not an opening parenthesis then it cant really be a *call*.
|
||||
$openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($functionName + 1), null, true);
|
||||
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
|
||||
return;
|
||||
}
|
||||
|
||||
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
|
||||
|
||||
$nextSeperator = $openBracket;
|
||||
while (($nextSeperator = $phpcsFile->findNext(array(T_COMMA, T_VARIABLE), ($nextSeperator + 1), $closeBracket)) !== false) {
|
||||
// Make sure the comma or variable belongs directly to this function call,
|
||||
// and is not inside a nested function call or array.
|
||||
$brackets = $tokens[$nextSeperator]['nested_parenthesis'];
|
||||
$lastBracket = array_pop($brackets);
|
||||
if ($lastBracket !== $closeBracket) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($tokens[$nextSeperator]['code'] === T_COMMA) {
|
||||
if ($tokens[($nextSeperator - 1)]['code'] === T_WHITESPACE) {
|
||||
$error = 'Space found before comma in function call';
|
||||
$phpcsFile->addError($error, $stackPtr, 'SpaceBeforeComma');
|
||||
}
|
||||
|
||||
if ($tokens[($nextSeperator + 1)]['code'] !== T_WHITESPACE) {
|
||||
$error = 'No space found after comma in function call';
|
||||
$phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterComma');
|
||||
} else {
|
||||
// If there is a newline in the space, then the must be formatting
|
||||
// each argument on a newline, which is valid, so ignore it.
|
||||
if (strpos($tokens[($nextSeperator + 1)]['content'], $phpcsFile->eolChar) === false) {
|
||||
$space = strlen($tokens[($nextSeperator + 1)]['content']);
|
||||
if ($space > 1) {
|
||||
$error = 'Expected 1 space after comma in function call; %s found';
|
||||
$data = array($space);
|
||||
$phpcsFile->addError($error, $stackPtr, 'TooMuchSpaceAfterComma', $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Token is a variable.
|
||||
$nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($nextSeperator + 1), $closeBracket, true);
|
||||
if ($nextToken !== false) {
|
||||
if ($tokens[$nextToken]['code'] === T_EQUAL) {
|
||||
if (($tokens[($nextToken - 1)]['code']) !== T_WHITESPACE) {
|
||||
$error = 'Expected 1 space before = sign of default value';
|
||||
$phpcsFile->addError($error, $stackPtr, 'NoSpaceBeforeEquals');
|
||||
}
|
||||
|
||||
if ($tokens[($nextToken + 1)]['code'] !== T_WHITESPACE) {
|
||||
$error = 'Expected 1 space after = sign of default value';
|
||||
$phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterEquals');
|
||||
}
|
||||
}
|
||||
}
|
||||
}//end if
|
||||
}//end while
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
/**
|
||||
* Generic_Sniffs_Methods_OpeningMethodBraceBsdAllmanSniff.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/**
|
||||
* Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff.
|
||||
*
|
||||
* Checks that the opening brace of a function is on the line after the
|
||||
* function declaration.
|
||||
*
|
||||
* @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 Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Registers the tokens that this sniff wants to listen for.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(T_FUNCTION);
|
||||
|
||||
}//end register()
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$openingBrace = $tokens[$stackPtr]['scope_opener'];
|
||||
|
||||
// The end of the function occurs at the end of the argument list. Its
|
||||
// like this because some people like to break long function declarations
|
||||
// over multiple lines.
|
||||
$functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line'];
|
||||
$braceLine = $tokens[$openingBrace]['line'];
|
||||
|
||||
$lineDifference = ($braceLine - $functionLine);
|
||||
|
||||
if ($lineDifference === 0) {
|
||||
$error = 'Opening brace should be on a new line';
|
||||
$phpcsFile->addError($error, $openingBrace, 'BraceOnSameLine');
|
||||
return;
|
||||
}
|
||||
|
||||
if ($lineDifference > 1) {
|
||||
$error = 'Opening brace should be on the line after the declaration; found %s blank line(s)';
|
||||
$data = array(($lineDifference - 1));
|
||||
$phpcsFile->addError($error, $openingBrace, 'BraceSpacing', $data);
|
||||
return;
|
||||
}
|
||||
|
||||
// We need to actually find the first piece of content on this line,
|
||||
// as if this is a method with tokens before it (public, static etc)
|
||||
// or an if with an else before it, then we need to start the scope
|
||||
// checking from there, rather than the current token.
|
||||
$lineStart = $stackPtr;
|
||||
while (($lineStart = $phpcsFile->findPrevious(array(T_WHITESPACE), ($lineStart - 1), null, false)) !== false) {
|
||||
if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// We found a new line, now go forward and find the first non-whitespace
|
||||
// token.
|
||||
$lineStart = $phpcsFile->findNext(array(T_WHITESPACE), $lineStart, null, true);
|
||||
|
||||
// The opening brace is on the correct line, now it needs to be
|
||||
// checked to be correctly indented.
|
||||
$startColumn = $tokens[$lineStart]['column'];
|
||||
$braceIndent = $tokens[$openingBrace]['column'];
|
||||
|
||||
if ($braceIndent !== $startColumn) {
|
||||
$error = 'Opening brace indented incorrectly; expected %s spaces, found %s';
|
||||
$data = array(
|
||||
($startColumn - 1),
|
||||
($braceIndent - 1),
|
||||
);
|
||||
$phpcsFile->addError($error, $openingBrace, 'BraceIndent', $data);
|
||||
}
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
/**
|
||||
* Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/**
|
||||
* Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff.
|
||||
*
|
||||
* Checks that the opening brace of a function is on the same line
|
||||
* as the function declaration.
|
||||
*
|
||||
* @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 Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Registers the tokens that this sniff wants to listen for.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(T_FUNCTION);
|
||||
|
||||
}//end register()
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$openingBrace = $tokens[$stackPtr]['scope_opener'];
|
||||
|
||||
// The end of the function occurs at the end of the argument list. Its
|
||||
// like this because some people like to break long function declarations
|
||||
// over multiple lines.
|
||||
$functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line'];
|
||||
$braceLine = $tokens[$openingBrace]['line'];
|
||||
|
||||
$lineDifference = ($braceLine - $functionLine);
|
||||
|
||||
if ($lineDifference > 0) {
|
||||
$error = 'Opening brace should be on the same line as the declaration';
|
||||
$phpcsFile->addError($error, $openingBrace, 'BraceOnNewLine');
|
||||
return;
|
||||
}
|
||||
|
||||
// Checks that the closing parenthesis and the opening brace are
|
||||
// separated by a whitespace character.
|
||||
$closerColumn = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['column'];
|
||||
$braceColumn = $tokens[$openingBrace]['column'];
|
||||
|
||||
$columnDifference = ($braceColumn - $closerColumn);
|
||||
|
||||
if ($columnDifference !== 2) {
|
||||
$error = 'Expected 1 space between the closing parenthesis and the opening brace; found %s';
|
||||
$data = array(($columnDifference - 1));
|
||||
$phpcsFile->addError($error, $openingBrace, 'SpaceBeforeBrace', $data);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check that a tab was not used instead of a space.
|
||||
$spaceTokenPtr = ($tokens[$stackPtr]['parenthesis_closer'] + 1);
|
||||
$spaceContent = $tokens[$spaceTokenPtr]['content'];
|
||||
if ($spaceContent !== ' ') {
|
||||
$error = 'Expected a single space character between closing parenthesis and opening brace; found %s';
|
||||
$data = array($spaceContent);
|
||||
$phpcsFile->addError($error, $openingBrace, 'SpaceBeforeBrace', $data);
|
||||
return;
|
||||
}
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user