Initial Commit

This commit is contained in:
Riley Schneider
2025-12-03 16:38:10 +01:00
parent c5e26bf594
commit b732d8d4b5
17680 changed files with 5977495 additions and 2 deletions

View File

@@ -0,0 +1,259 @@
<?php
/**
* PEAR_Sniffs_Functions_FunctionCallSignatureSniff.
*
* 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
*/
/**
* PEAR_Sniffs_Functions_FunctionCallSignatureSniff.
*
* @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 PEAR_Sniffs_Functions_FunctionCallSignatureSniff 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();
// Find the next non-empty token.
$openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
// Not a function call.
return;
}
if (isset($tokens[$openBracket]['parenthesis_closer']) === false) {
// Not a function call.
return;
}
// Find the previous non-empty token.
$search = PHP_CodeSniffer_Tokens::$emptyTokens;
$search[] = T_BITWISE_AND;
$previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true);
if ($tokens[$previous]['code'] === T_FUNCTION) {
// It's a function definition, not a function call.
return;
}
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
if (($stackPtr + 1) !== $openBracket) {
// Checking this: $value = my_function[*](...).
$error = 'Space before opening parenthesis of function call prohibited';
$phpcsFile->addError($error, $stackPtr, 'SpaceBeforeOpenBracket');
}
$next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true);
if ($tokens[$next]['code'] === T_SEMICOLON) {
if (in_array($tokens[($closeBracket + 1)]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
$error = 'Space after closing parenthesis of function call prohibited';
$phpcsFile->addError($error, $closeBracket, 'SpaceAfterCloseBracket');
}
}
// Check if this is a single line or multi-line function call.
if ($tokens[$openBracket]['line'] === $tokens[$closeBracket]['line']) {
$this->processSingleLineCall($phpcsFile, $stackPtr, $openBracket, $tokens);
} else {
$this->processMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens);
}
}//end process()
/**
* Processes single-line calls.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $openBracket The position of the openning bracket
* in the stack passed in $tokens.
* @param array $tokens The stack of tokens that make up
* the file.
*
* @return void
*/
public function processSingleLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens)
{
if ($tokens[($openBracket + 1)]['code'] === T_WHITESPACE) {
// Checking this: $value = my_function([*]...).
$error = 'Space after opening parenthesis of function call prohibited';
$phpcsFile->addError($error, $stackPtr, 'SpaceAfterOpenBracket');
}
$closer = $tokens[$openBracket]['parenthesis_closer'];
if ($tokens[($closer - 1)]['code'] === T_WHITESPACE) {
// Checking this: $value = my_function(...[*]).
$between = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true);
// Only throw an error if there is some content between the parenthesis.
// i.e., Checking for this: $value = my_function().
// If there is no content, then we would have thrown an error in the
// previous IF statement because it would look like this:
// $value = my_function( ).
if ($between !== $closer) {
$error = 'Space before closing parenthesis of function call prohibited';
$phpcsFile->addError($error, $closer, 'SpaceBeforeCloseBracket');
}
}
}//end processSingleLineCall()
/**
* Processes multi-line calls.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $openBracket The position of the openning bracket
* in the stack passed in $tokens.
* @param array $tokens The stack of tokens that make up
* the file.
*
* @return void
*/
public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens)
{
// We need to work out how far indented the function
// call itself is, so we can work out how far to
// indent the arguments.
$functionIndent = 0;
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
$i++;
break;
}
}
if ($tokens[$i]['code'] === T_WHITESPACE) {
$functionIndent = strlen($tokens[$i]['content']);
}
// Each line between the parenthesis should be indented 4 spaces.
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
$lastLine = $tokens[$openBracket]['line'];
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
// Skip nested function calls.
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
$i = $tokens[$i]['parenthesis_closer'];
$lastLine = $tokens[$i]['line'];
continue;
}
if ($tokens[$i]['line'] !== $lastLine) {
$lastLine = $tokens[$i]['line'];
// Ignore heredoc indentation.
if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$heredocTokens) === true) {
continue;
}
// Ignore multi-line string indentation.
if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$stringTokens) === true) {
if ($tokens[$i]['code'] === $tokens[($i - 1)]['code']) {
continue;
}
}
// We changed lines, so this should be a whitespace indent token, but first make
// sure it isn't a blank line because we don't need to check indent unless there
// is actually some code to indent.
$nextCode = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), ($closeBracket + 1), true);
if ($tokens[$nextCode]['line'] !== $lastLine) {
$error = 'Empty lines are not allowed in multi-line function calls';
$phpcsFile->addError($error, $i, 'EmptyLine');
continue;
}
if ($nextCode === $closeBracket) {
// Closing brace needs to be indented to the same level
// as the function call.
$expectedIndent = $functionIndent;
} else {
$expectedIndent = ($functionIndent + 4);
}
if ($tokens[$i]['code'] !== T_WHITESPACE) {
$foundIndent = 0;
} else {
$foundIndent = strlen($tokens[$i]['content']);
}
if ($expectedIndent !== $foundIndent) {
$error = 'Multi-line function call not indented correctly; expected %s spaces but found %s';
$data = array(
$expectedIndent,
$foundIndent,
);
$phpcsFile->addError($error, $i, 'Indent', $data);
}
}//end if
// Skip the rest of a closure.
if ($tokens[$i]['code'] === T_CLOSURE) {
$i = $tokens[$i]['scope_closer'];
$lastLine = $tokens[$i]['line'];
continue;
}
}//end for
if ($tokens[($openBracket + 1)]['content'] !== $phpcsFile->eolChar) {
$error = 'Opening parenthesis of a multi-line function call must be the last content on the line';
$phpcsFile->addError($error, $stackPtr, 'ContentAfterOpenBracket');
}
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true);
if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
$error = 'Closing parenthesis of a multi-line function call must be on a line by itself';
$phpcsFile->addError($error, $closeBracket, 'CloseBracketLine');
}
}//end processMultiLineCall()
}//end class
?>

View File

@@ -0,0 +1,224 @@
<?php
/**
* PEAR_Sniffs_Functions_FunctionDeclarationSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @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
*/
/**
* PEAR_Sniffs_Functions_FunctionDeclarationSniff.
*
* Ensure single and multi-line function declarations are defined correctly.
*
* @category PHP
* @package PHP_CodeSniffer
* @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 PEAR_Sniffs_Functions_FunctionDeclarationSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
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();
// Check if this is a single line or multi-line declaration.
$openBracket = $tokens[$stackPtr]['parenthesis_opener'];
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
if ($tokens[$openBracket]['line'] === $tokens[$closeBracket]['line']) {
$this->processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens);
} else {
$this->processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens);
}
}//end process()
/**
* Processes single-line declarations.
*
* Just uses the Generic BSD-Allman brace sniff.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param array $tokens The stack of tokens that make up
* the file.
*
* @return void
*/
public function processSingleLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens)
{
if (class_exists('Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff', true) === false) {
throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff not found');
}
$sniff = new Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff();
$sniff->process($phpcsFile, $stackPtr);
}//end processSingleLineDeclaration()
/**
* Processes mutli-line declarations.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param array $tokens The stack of tokens that make up
* the file.
*
* @return void
*/
public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens)
{
// We need to work out how far indented the function
// declaration itself is, so we can work out how far to
// indent parameters.
$functionIndent = 0;
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
$i++;
break;
}
}
if ($tokens[$i]['code'] === T_WHITESPACE) {
$functionIndent = strlen($tokens[$i]['content']);
}
// Each line between the parenthesis should be indented 4 spaces.
$openBracket = $tokens[$stackPtr]['parenthesis_opener'];
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
$lastLine = $tokens[$openBracket]['line'];
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
if ($tokens[$i]['line'] !== $lastLine) {
if ($tokens[$i]['line'] === $tokens[$closeBracket]['line']) {
// Closing brace needs to be indented to the same level
// as the function.
$expectedIndent = $functionIndent;
} else {
$expectedIndent = ($functionIndent + 4);
}
// We changed lines, so this should be a whitespace indent token.
if ($tokens[$i]['code'] !== T_WHITESPACE) {
$foundIndent = 0;
} else {
$foundIndent = strlen($tokens[$i]['content']);
}
if ($expectedIndent !== $foundIndent) {
$error = 'Multi-line function declaration not indented correctly; expected %s spaces but found %s';
$data = array(
$expectedIndent,
$foundIndent,
);
$phpcsFile->addError($error, $i, 'Indent', $data);
}
$lastLine = $tokens[$i]['line'];
}//end if
if ($tokens[$i]['code'] === T_ARRAY) {
// Skip arrays as they have their own indentation rules.
$i = $tokens[$i]['parenthesis_closer'];
$lastLine = $tokens[$i]['line'];
continue;
}
}//end for
if (isset($tokens[$stackPtr]['scope_opener']) === true) {
// The openning brace needs to be one space away
// from the closing parenthesis.
$next = $tokens[($closeBracket + 1)];
if ($next['code'] !== T_WHITESPACE) {
$length = 0;
} else if ($next['content'] === $phpcsFile->eolChar) {
$length = -1;
} else {
$length = strlen($next['content']);
}
if ($length !== 1) {
$data = array($length);
$code = 'SpaceBeforeOpenBrace';
$error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found ';
if ($length === -1) {
$error .= 'newline';
$code = 'NewlineBeforeOpenBrace';
} else {
$error .= '%s spaces';
}
$phpcsFile->addError($error, ($closeBracket + 1), $code, $data);
return;
}
// And just in case they do something funny before the brace...
$next = $phpcsFile->findNext(
T_WHITESPACE,
($closeBracket + 1),
null,
true
);
if ($next !== false && $tokens[$next]['code'] !== T_OPEN_CURLY_BRACKET) {
$error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration';
$phpcsFile->addError($error, $next, 'NoSpaceBeforeOpenBrace');
}
}//end if
// The closing parenthesis must be on a new line, even
// when checking abstract function definitions.
$prev = $phpcsFile->findPrevious(
T_WHITESPACE,
($closeBracket - 1),
null,
true
);
if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
$error = 'The closing parenthesis of a multi-line function declaration must be on a new line';
$phpcsFile->addError($error, $closeBracket, 'CloseBracketLine');
}
}//end processMultiLineDeclaration()
}//end class
?>

View File

@@ -0,0 +1,108 @@
<?php
/**
* PEAR_Sniffs_Functions_ValidDefaultValueSniff.
*
* 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
*/
/**
* PEAR_Sniffs_Functions_ValidDefaultValueSniff.
*
* A Sniff to ensure that parameters defined for a function that have a default
* value come at the end of the function signature.
*
* @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 PEAR_Sniffs_Functions_ValidDefaultValueSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
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();
$argStart = $tokens[$stackPtr]['parenthesis_opener'];
$argEnd = $tokens[$stackPtr]['parenthesis_closer'];
// Flag for when we have found a default in our arg list.
// If there is a value without a default after this, it is an error.
$defaultFound = false;
$nextArg = $argStart;
while (($nextArg = $phpcsFile->findNext(T_VARIABLE, ($nextArg + 1), $argEnd)) !== false) {
$argHasDefault = self::_argHasDefault($phpcsFile, $nextArg);
if (($argHasDefault === false) && ($defaultFound === true)) {
$error = 'Arguments with default values must be at the end of the argument list';
$phpcsFile->addError($error, $nextArg, 'NotAtEnd');
return;
}
if ($argHasDefault === true) {
$defaultFound = true;
}
}
}//end process()
/**
* Returns true if the passed argument has a default value.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $argPtr The position of the argument
* in the stack.
*
* @return bool
*/
private static function _argHasDefault(PHP_CodeSniffer_File $phpcsFile, $argPtr)
{
$tokens = $phpcsFile->getTokens();
$nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($argPtr + 1), null, true);
if ($tokens[$nextToken]['code'] !== T_EQUAL) {
return false;
}
return true;
}//end _argHasDefault()
}//end class
?>