基础代码
This commit is contained in:
+455
@@ -0,0 +1,455 @@
|
||||
<?php
|
||||
/**
|
||||
* PHPCompatibility, an external standard for PHP_CodeSniffer.
|
||||
*
|
||||
* @package PHPCompatibility
|
||||
* @copyright 2012-2019 PHPCompatibility Contributors
|
||||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
|
||||
* @link https://github.com/PHPCompatibility/PHPCompatibility
|
||||
*/
|
||||
|
||||
namespace PHPCompatibility\Sniffs\FunctionUse;
|
||||
|
||||
use PHPCompatibility\Sniff;
|
||||
use PHPCompatibility\PHPCSHelper;
|
||||
use PHP_CodeSniffer_File as File;
|
||||
use PHP_CodeSniffer_Tokens as Tokens;
|
||||
|
||||
/**
|
||||
* Functions inspecting function arguments report the current parameter value
|
||||
* instead of the original since PHP 7.0.
|
||||
*
|
||||
* `func_get_arg()`, `func_get_args()`, `debug_backtrace()` and exception backtraces
|
||||
* will no longer report the original parameter value as was passed to the function,
|
||||
* but will instead provide the current value (which might have been modified).
|
||||
*
|
||||
* PHP version 7.0
|
||||
*
|
||||
* @link https://www.php.net/manual/en/migration70.incompatible.php#migration70.incompatible.other.func-parameter-modified
|
||||
*
|
||||
* @since 9.1.0
|
||||
*/
|
||||
class ArgumentFunctionsReportCurrentValueSniff extends Sniff
|
||||
{
|
||||
|
||||
/**
|
||||
* A list of functions that, when called, can behave differently in PHP 7
|
||||
* when dealing with parameters of the function they're called in.
|
||||
*
|
||||
* @since 9.1.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $changedFunctions = array(
|
||||
'func_get_arg' => true,
|
||||
'func_get_args' => true,
|
||||
'debug_backtrace' => true,
|
||||
'debug_print_backtrace' => true,
|
||||
);
|
||||
|
||||
/**
|
||||
* Tokens to look out for to allow us to skip past nested scoped structures.
|
||||
*
|
||||
* @since 9.1.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $skipPastNested = array(
|
||||
'T_CLASS' => true,
|
||||
'T_ANON_CLASS' => true,
|
||||
'T_INTERFACE' => true,
|
||||
'T_TRAIT' => true,
|
||||
'T_FUNCTION' => true,
|
||||
'T_CLOSURE' => true,
|
||||
);
|
||||
|
||||
/**
|
||||
* List of tokens which when they preceed a T_STRING *within a function* indicate
|
||||
* this is not a call to a PHP native function.
|
||||
*
|
||||
* This list already takes into account that nested scoped structures are being
|
||||
* skipped over, so doesn't check for those again.
|
||||
* Similarly, as constants won't have parentheses, those don't need to be checked
|
||||
* for either.
|
||||
*
|
||||
* @since 9.1.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $noneFunctionCallIndicators = array(
|
||||
\T_DOUBLE_COLON => true,
|
||||
\T_OBJECT_OPERATOR => true,
|
||||
);
|
||||
|
||||
/**
|
||||
* The tokens for variable incrementing/decrementing.
|
||||
*
|
||||
* @since 9.1.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $plusPlusMinusMinus = array(
|
||||
\T_DEC => true,
|
||||
\T_INC => true,
|
||||
);
|
||||
|
||||
/**
|
||||
* Tokens to ignore when determining the start of a statement.
|
||||
*
|
||||
* @since 9.1.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $ignoreForStartOfStatement = array(
|
||||
\T_COMMA,
|
||||
\T_DOUBLE_ARROW,
|
||||
\T_OPEN_SQUARE_BRACKET,
|
||||
\T_OPEN_PARENTHESIS,
|
||||
);
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @since 9.1.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(
|
||||
\T_FUNCTION,
|
||||
\T_CLOSURE,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @since 9.1.0
|
||||
*
|
||||
* @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(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
if ($this->supportsAbove('7.0') === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) {
|
||||
// Abstract function, interface function, live coding or parse error.
|
||||
return;
|
||||
}
|
||||
|
||||
$scopeOpener = $tokens[$stackPtr]['scope_opener'];
|
||||
$scopeCloser = $tokens[$stackPtr]['scope_closer'];
|
||||
|
||||
// Does the function declaration have parameters ?
|
||||
$params = PHPCSHelper::getMethodParameters($phpcsFile, $stackPtr);
|
||||
if (empty($params)) {
|
||||
// No named arguments found, so no risk of them being changed.
|
||||
return;
|
||||
}
|
||||
|
||||
$paramNames = array();
|
||||
foreach ($params as $param) {
|
||||
$paramNames[] = $param['name'];
|
||||
}
|
||||
|
||||
for ($i = ($scopeOpener + 1); $i < $scopeCloser; $i++) {
|
||||
if (isset($this->skipPastNested[$tokens[$i]['type']]) && isset($tokens[$i]['scope_closer'])) {
|
||||
// Skip past nested structures.
|
||||
$i = $tokens[$i]['scope_closer'];
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($tokens[$i]['code'] !== \T_STRING) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$foundFunctionName = strtolower($tokens[$i]['content']);
|
||||
|
||||
if (isset($this->changedFunctions[$foundFunctionName]) === false) {
|
||||
// Not one of the target functions.
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
* Ok, so is this really a function call to one of the PHP native functions ?
|
||||
*/
|
||||
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
|
||||
if ($next === false || $tokens[$next]['code'] !== \T_OPEN_PARENTHESIS) {
|
||||
// Live coding, parse error or not a function call.
|
||||
continue;
|
||||
}
|
||||
|
||||
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true);
|
||||
if ($prev !== false) {
|
||||
if (isset($this->noneFunctionCallIndicators[$tokens[$prev]['code']])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for namespaced functions, ie: \foo\bar() not \bar().
|
||||
if ($tokens[ $prev ]['code'] === \T_NS_SEPARATOR) {
|
||||
$pprev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true);
|
||||
if ($pprev !== false && $tokens[ $pprev ]['code'] === \T_STRING) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Address some special cases.
|
||||
*/
|
||||
if ($foundFunctionName !== 'func_get_args') {
|
||||
$paramOne = $this->getFunctionCallParameter($phpcsFile, $i, 1);
|
||||
if ($paramOne !== false) {
|
||||
switch ($foundFunctionName) {
|
||||
/*
|
||||
* Check if `debug_(print_)backtrace()` is called with the
|
||||
* `DEBUG_BACKTRACE_IGNORE_ARGS` option.
|
||||
*/
|
||||
case 'debug_backtrace':
|
||||
case 'debug_print_backtrace':
|
||||
$hasIgnoreArgs = $phpcsFile->findNext(
|
||||
\T_STRING,
|
||||
$paramOne['start'],
|
||||
($paramOne['end'] + 1),
|
||||
false,
|
||||
'DEBUG_BACKTRACE_IGNORE_ARGS'
|
||||
);
|
||||
|
||||
if ($hasIgnoreArgs !== false) {
|
||||
// Debug_backtrace() called with ignore args option.
|
||||
continue 2;
|
||||
}
|
||||
break;
|
||||
|
||||
/*
|
||||
* Collect the necessary information to only throw a notice if the argument
|
||||
* touched/changed is in line with the passed $arg_num.
|
||||
*
|
||||
* Also, we can ignore `func_get_arg()` if the argument offset passed is
|
||||
* higher than the number of named parameters.
|
||||
*
|
||||
* {@internal Note: This does not take calculations into account!
|
||||
* Should be exceptionally rare and can - if needs be - be addressed at a later stage.}
|
||||
*/
|
||||
case 'func_get_arg':
|
||||
$number = $phpcsFile->findNext(\T_LNUMBER, $paramOne['start'], ($paramOne['end'] + 1));
|
||||
if ($number !== false) {
|
||||
$argNumber = $tokens[$number]['content'];
|
||||
|
||||
if (isset($paramNames[$argNumber]) === false) {
|
||||
// Requesting a non-named additional parameter. Ignore.
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* Check if the call to func_get_args() happens to be in an array_slice() or
|
||||
* array_splice() with an $offset higher than the number of named parameters.
|
||||
* In that case, we can ignore it.
|
||||
*
|
||||
* {@internal Note: This does not take offset calculations into account!
|
||||
* Should be exceptionally rare and can - if needs be - be addressed at a later stage.}
|
||||
*/
|
||||
if ($prev !== false && $tokens[$prev]['code'] === \T_OPEN_PARENTHESIS) {
|
||||
|
||||
$maybeFunctionCall = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true);
|
||||
if ($maybeFunctionCall !== false
|
||||
&& $tokens[$maybeFunctionCall]['code'] === \T_STRING
|
||||
&& ($tokens[$maybeFunctionCall]['content'] === 'array_slice'
|
||||
|| $tokens[$maybeFunctionCall]['content'] === 'array_splice')
|
||||
) {
|
||||
$parentFuncParamTwo = $this->getFunctionCallParameter($phpcsFile, $maybeFunctionCall, 2);
|
||||
$number = $phpcsFile->findNext(
|
||||
\T_LNUMBER,
|
||||
$parentFuncParamTwo['start'],
|
||||
($parentFuncParamTwo['end'] + 1)
|
||||
);
|
||||
|
||||
if ($number !== false && isset($paramNames[$tokens[$number]['content']]) === false) {
|
||||
// Requesting non-named additional parameters. Ignore.
|
||||
continue ;
|
||||
}
|
||||
|
||||
// Slice starts at a named argument, but we know which params are being accessed.
|
||||
$paramNamesSubset = \array_slice($paramNames, $tokens[$number]['content']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* For debug_backtrace(), check if the result is being dereferenced and if so,
|
||||
* whether the `args` index is used.
|
||||
* I.e. whether `$index` in `debug_backtrace()[$stackFrame][$index]` is a string
|
||||
* with the content `args`.
|
||||
*
|
||||
* Note: We already know that $next is the open parenthesis of the function call.
|
||||
*/
|
||||
if ($foundFunctionName === 'debug_backtrace' && isset($tokens[$next]['parenthesis_closer'])) {
|
||||
$afterParenthesis = $phpcsFile->findNext(
|
||||
Tokens::$emptyTokens,
|
||||
($tokens[$next]['parenthesis_closer'] + 1),
|
||||
null,
|
||||
true
|
||||
);
|
||||
|
||||
if ($tokens[$afterParenthesis]['code'] === \T_OPEN_SQUARE_BRACKET
|
||||
&& isset($tokens[$afterParenthesis]['bracket_closer'])
|
||||
) {
|
||||
$afterStackFrame = $phpcsFile->findNext(
|
||||
Tokens::$emptyTokens,
|
||||
($tokens[$afterParenthesis]['bracket_closer'] + 1),
|
||||
null,
|
||||
true
|
||||
);
|
||||
|
||||
if ($tokens[$afterStackFrame]['code'] === \T_OPEN_SQUARE_BRACKET
|
||||
&& isset($tokens[$afterStackFrame]['bracket_closer'])
|
||||
) {
|
||||
$arrayIndex = $phpcsFile->findNext(
|
||||
\T_CONSTANT_ENCAPSED_STRING,
|
||||
($afterStackFrame + 1),
|
||||
$tokens[$afterStackFrame]['bracket_closer']
|
||||
);
|
||||
|
||||
if ($arrayIndex !== false && $this->stripQuotes($tokens[$arrayIndex]['content']) !== 'args') {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Only check for variables before the start of the statement to
|
||||
* prevent false positives on the return value of the function call
|
||||
* being assigned to one of the parameters, i.e.:
|
||||
* `$param = func_get_args();`.
|
||||
*/
|
||||
$startOfStatement = PHPCSHelper::findStartOfStatement($phpcsFile, $i, $this->ignoreForStartOfStatement);
|
||||
|
||||
/*
|
||||
* Ok, so we've found one of the target functions in the right scope.
|
||||
* Now, let's check if any of the passed parameters were touched.
|
||||
*/
|
||||
$scanResult = 'clean';
|
||||
for ($j = ($scopeOpener + 1); $j < $startOfStatement; $j++) {
|
||||
if (isset($this->skipPastNested[$tokens[$j]['type']])
|
||||
&& isset($tokens[$j]['scope_closer'])
|
||||
) {
|
||||
// Skip past nested structures.
|
||||
$j = $tokens[$j]['scope_closer'];
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($tokens[$j]['code'] !== \T_VARIABLE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($foundFunctionName === 'func_get_arg' && isset($argNumber)) {
|
||||
if (isset($paramNames[$argNumber])
|
||||
&& $tokens[$j]['content'] !== $paramNames[$argNumber]
|
||||
) {
|
||||
// Different param than the one requested by func_get_arg().
|
||||
continue;
|
||||
}
|
||||
} elseif ($foundFunctionName === 'func_get_args' && isset($paramNamesSubset)) {
|
||||
if (\in_array($tokens[$j]['content'], $paramNamesSubset, true) === false) {
|
||||
// Different param than the ones requested by func_get_args().
|
||||
continue;
|
||||
}
|
||||
} elseif (\in_array($tokens[$j]['content'], $paramNames, true) === false) {
|
||||
// Variable is not one of the function parameters.
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
* Ok, so we've found a variable which was passed as one of the parameters.
|
||||
* Now, is this variable being changed, i.e. incremented, decremented or
|
||||
* assigned something ?
|
||||
*/
|
||||
$scanResult = 'warning';
|
||||
if (isset($variableToken) === false) {
|
||||
$variableToken = $j;
|
||||
}
|
||||
|
||||
$beforeVar = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($j - 1), null, true);
|
||||
if ($beforeVar !== false && isset($this->plusPlusMinusMinus[$tokens[$beforeVar]['code']])) {
|
||||
// Variable is being (pre-)incremented/decremented.
|
||||
$scanResult = 'error';
|
||||
$variableToken = $j;
|
||||
break;
|
||||
}
|
||||
|
||||
$afterVar = $phpcsFile->findNext(Tokens::$emptyTokens, ($j + 1), null, true);
|
||||
if ($afterVar === false) {
|
||||
// Shouldn't be possible, but just in case.
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($this->plusPlusMinusMinus[$tokens[$afterVar]['code']])) {
|
||||
// Variable is being (post-)incremented/decremented.
|
||||
$scanResult = 'error';
|
||||
$variableToken = $j;
|
||||
break;
|
||||
}
|
||||
|
||||
if ($tokens[$afterVar]['code'] === \T_OPEN_SQUARE_BRACKET
|
||||
&& isset($tokens[$afterVar]['bracket_closer'])
|
||||
) {
|
||||
// Skip past array access on the variable.
|
||||
while (($afterVar = $phpcsFile->findNext(Tokens::$emptyTokens, ($tokens[$afterVar]['bracket_closer'] + 1), null, true)) !== false) {
|
||||
if ($tokens[$afterVar]['code'] !== \T_OPEN_SQUARE_BRACKET
|
||||
|| isset($tokens[$afterVar]['bracket_closer']) === false
|
||||
) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($afterVar !== false
|
||||
&& isset(Tokens::$assignmentTokens[$tokens[$afterVar]['code']])
|
||||
) {
|
||||
// Variable is being assigned something.
|
||||
$scanResult = 'error';
|
||||
$variableToken = $j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unset($argNumber, $paramNamesSubset);
|
||||
|
||||
if ($scanResult === 'clean') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$error = 'Since PHP 7.0, functions inspecting arguments, like %1$s(), no longer report the original value as passed to a parameter, but will instead provide the current value. The parameter "%2$s" was %4$s on line %3$s.';
|
||||
$data = array(
|
||||
$foundFunctionName,
|
||||
$tokens[$variableToken]['content'],
|
||||
$tokens[$variableToken]['line'],
|
||||
);
|
||||
|
||||
if ($scanResult === 'error') {
|
||||
$data[] = 'changed';
|
||||
$phpcsFile->addError($error, $i, 'Changed', $data);
|
||||
|
||||
} elseif ($scanResult === 'warning') {
|
||||
$data[] = 'used, and possibly changed (by reference),';
|
||||
$phpcsFile->addWarning($error, $i, 'NeedsInspection', $data);
|
||||
}
|
||||
|
||||
unset($variableToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
/**
|
||||
* PHPCompatibility, an external standard for PHP_CodeSniffer.
|
||||
*
|
||||
* @package PHPCompatibility
|
||||
* @copyright 2012-2019 PHPCompatibility Contributors
|
||||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
|
||||
* @link https://github.com/PHPCompatibility/PHPCompatibility
|
||||
*/
|
||||
|
||||
namespace PHPCompatibility\Sniffs\FunctionUse;
|
||||
|
||||
use PHPCompatibility\Sniff;
|
||||
use PHP_CodeSniffer_File as File;
|
||||
use PHP_CodeSniffer_Tokens as Tokens;
|
||||
|
||||
/**
|
||||
* Detect usage of `func_get_args()`, `func_get_arg()` and `func_num_args()` in invalid context.
|
||||
*
|
||||
* Checks for:
|
||||
* - Prior to PHP 5.3, these functions could not be used as a function call parameter.
|
||||
* - Calling these functions from the outermost scope of a file which has been included by
|
||||
* calling `include` or `require` from within a function in the calling file, worked
|
||||
* prior to PHP 5.3. As of PHP 5.3, this will generate a warning and will always return false/-1.
|
||||
* If the file was called directly or included in the global scope, calls to these
|
||||
* functions would already generate a warning prior to PHP 5.3.
|
||||
*
|
||||
* PHP version 5.3
|
||||
*
|
||||
* @link https://www.php.net/manual/en/migration53.incompatible.php
|
||||
*
|
||||
* @since 8.2.0
|
||||
*/
|
||||
class ArgumentFunctionsUsageSniff extends Sniff
|
||||
{
|
||||
|
||||
/**
|
||||
* The target functions for this sniff.
|
||||
*
|
||||
* @since 8.2.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $targetFunctions = array(
|
||||
'func_get_args' => true,
|
||||
'func_get_arg' => true,
|
||||
'func_num_args' => true,
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @since 8.2.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(\T_STRING);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @since 8.2.0
|
||||
*
|
||||
* @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(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
$functionLc = strtolower($tokens[$stackPtr]['content']);
|
||||
if (isset($this->targetFunctions[$functionLc]) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Next non-empty token should be the open parenthesis.
|
||||
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true);
|
||||
if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) {
|
||||
return;
|
||||
}
|
||||
|
||||
$ignore = array(
|
||||
\T_DOUBLE_COLON => true,
|
||||
\T_OBJECT_OPERATOR => true,
|
||||
\T_FUNCTION => true,
|
||||
\T_NEW => true,
|
||||
);
|
||||
|
||||
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
|
||||
if (isset($ignore[$tokens[$prevNonEmpty]['code']]) === true) {
|
||||
// Not a call to a PHP function.
|
||||
return;
|
||||
} elseif ($tokens[$prevNonEmpty]['code'] === \T_NS_SEPARATOR && $tokens[$prevNonEmpty - 1]['code'] === \T_STRING) {
|
||||
// Namespaced function.
|
||||
return;
|
||||
}
|
||||
|
||||
$data = $tokens[$stackPtr]['content'];
|
||||
|
||||
/*
|
||||
* Check for use of the functions in the global scope.
|
||||
*
|
||||
* As PHPCS can not determine whether a file is included from within a function in
|
||||
* another file, so always throw a warning/error.
|
||||
*/
|
||||
if ($phpcsFile->hasCondition($stackPtr, array(\T_FUNCTION, \T_CLOSURE)) === false) {
|
||||
$isError = false;
|
||||
$message = 'Use of %s() outside of a user-defined function is only supported if the file is included from within a user-defined function in another file prior to PHP 5.3.';
|
||||
|
||||
if ($this->supportsAbove('5.3') === true) {
|
||||
$isError = true;
|
||||
$message .= ' As of PHP 5.3, it is no longer supported at all.';
|
||||
}
|
||||
|
||||
$this->addMessage($phpcsFile, $message, $stackPtr, $isError, 'OutsideFunctionScope', $data);
|
||||
}
|
||||
|
||||
/*
|
||||
* Check for use of the functions as a parameter in a function call.
|
||||
*/
|
||||
if ($this->supportsBelow('5.2') === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($tokens[$stackPtr]['nested_parenthesis']) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$throwError = false;
|
||||
|
||||
$closer = end($tokens[$stackPtr]['nested_parenthesis']);
|
||||
if (isset($tokens[$closer]['parenthesis_owner'])
|
||||
&& $tokens[$tokens[$closer]['parenthesis_owner']]['type'] === 'T_CLOSURE'
|
||||
) {
|
||||
$throwError = true;
|
||||
} else {
|
||||
$opener = key($tokens[$stackPtr]['nested_parenthesis']);
|
||||
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($opener - 1), null, true);
|
||||
if ($tokens[$prevNonEmpty]['code'] !== \T_STRING) {
|
||||
return;
|
||||
}
|
||||
|
||||
$prevPrevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevNonEmpty - 1), null, true);
|
||||
if ($tokens[$prevPrevNonEmpty]['code'] === \T_FUNCTION) {
|
||||
return;
|
||||
}
|
||||
|
||||
$throwError = true;
|
||||
}
|
||||
|
||||
if ($throwError === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$phpcsFile->addError(
|
||||
'%s() could not be used in parameter lists prior to PHP 5.3.',
|
||||
$stackPtr,
|
||||
'InParameterList',
|
||||
$data
|
||||
);
|
||||
}
|
||||
}
|
||||
+1109
File diff suppressed because it is too large
Load Diff
Vendored
+2008
File diff suppressed because it is too large
Load Diff
+173
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
/**
|
||||
* PHPCompatibility, an external standard for PHP_CodeSniffer.
|
||||
*
|
||||
* @package PHPCompatibility
|
||||
* @copyright 2012-2019 PHPCompatibility Contributors
|
||||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
|
||||
* @link https://github.com/PHPCompatibility/PHPCompatibility
|
||||
*/
|
||||
|
||||
namespace PHPCompatibility\Sniffs\FunctionUse;
|
||||
|
||||
use PHPCompatibility\Sniffs\FunctionUse\RequiredToOptionalFunctionParametersSniff;
|
||||
use PHP_CodeSniffer_File as File;
|
||||
|
||||
/**
|
||||
* Detect missing required function parameters in calls to native PHP functions.
|
||||
*
|
||||
* Specifically when those function parameters used to be optional in older PHP versions.
|
||||
*
|
||||
* PHP version All
|
||||
*
|
||||
* @link https://www.php.net/manual/en/doc.changelog.php
|
||||
*
|
||||
* @since 8.1.0
|
||||
* @since 9.0.0 Renamed from `OptionalRequiredFunctionParametersSniff` to `OptionalToRequiredFunctionParametersSniff`.
|
||||
*/
|
||||
class OptionalToRequiredFunctionParametersSniff extends RequiredToOptionalFunctionParametersSniff
|
||||
{
|
||||
|
||||
/**
|
||||
* A list of function parameters, which were optional in older versions and became required later on.
|
||||
*
|
||||
* The array lists : version number with true (required) and false (optional use deprecated).
|
||||
*
|
||||
* The index is the location of the parameter in the parameter list, starting at 0 !
|
||||
* If's sufficient to list the last version in which the parameter was not yet required.
|
||||
*
|
||||
* @since 8.1.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $functionParameters = array(
|
||||
// Special case, the optional nature is not deprecated, but usage is recommended
|
||||
// and leaving the parameter out will throw an E_NOTICE.
|
||||
'crypt' => array(
|
||||
1 => array(
|
||||
'name' => 'salt',
|
||||
'5.6' => 'recommended',
|
||||
),
|
||||
),
|
||||
'parse_str' => array(
|
||||
1 => array(
|
||||
'name' => 'result',
|
||||
'7.2' => false,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Determine whether an error/warning should be thrown for an item based on collected information.
|
||||
*
|
||||
* @since 8.1.0
|
||||
*
|
||||
* @param array $errorInfo Detail information about an item.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function shouldThrowError(array $errorInfo)
|
||||
{
|
||||
return ($errorInfo['optionalDeprecated'] !== ''
|
||||
|| $errorInfo['optionalRemoved'] !== ''
|
||||
|| $errorInfo['optionalRecommended'] !== '');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve the relevant detail (version) information for use in an error message.
|
||||
*
|
||||
* @since 8.1.0
|
||||
*
|
||||
* @param array $itemArray Version and other information about the item.
|
||||
* @param array $itemInfo Base information about the item.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getErrorInfo(array $itemArray, array $itemInfo)
|
||||
{
|
||||
$errorInfo = array(
|
||||
'paramName' => '',
|
||||
'optionalRecommended' => '',
|
||||
'optionalDeprecated' => '',
|
||||
'optionalRemoved' => '',
|
||||
'error' => false,
|
||||
);
|
||||
|
||||
$versionArray = $this->getVersionArray($itemArray);
|
||||
|
||||
if (empty($versionArray) === false) {
|
||||
foreach ($versionArray as $version => $required) {
|
||||
if ($this->supportsAbove($version) === true) {
|
||||
if ($required === true && $errorInfo['optionalRemoved'] === '') {
|
||||
$errorInfo['optionalRemoved'] = $version;
|
||||
$errorInfo['error'] = true;
|
||||
} elseif ($required === 'recommended' && $errorInfo['optionalRecommended'] === '') {
|
||||
$errorInfo['optionalRecommended'] = $version;
|
||||
} elseif ($errorInfo['optionalDeprecated'] === '') {
|
||||
$errorInfo['optionalDeprecated'] = $version;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$errorInfo['paramName'] = $itemArray['name'];
|
||||
|
||||
return $errorInfo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates the error or warning for this item.
|
||||
*
|
||||
* @since 8.1.0
|
||||
*
|
||||
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
||||
* @param int $stackPtr The position of the relevant token in
|
||||
* the stack.
|
||||
* @param array $itemInfo Base information about the item.
|
||||
* @param array $errorInfo Array with detail (version) information
|
||||
* relevant to the item.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addError(File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo)
|
||||
{
|
||||
$error = 'The "%s" parameter for function %s() is missing. Passing this parameter is ';
|
||||
if ($errorInfo['optionalRecommended'] === '') {
|
||||
$error .= 'no longer optional. The optional nature of the parameter is ';
|
||||
} else {
|
||||
$error .= 'strongly recommended ';
|
||||
}
|
||||
|
||||
$errorCode = $this->stringToErrorCode($itemInfo['name'] . '_' . $errorInfo['paramName']);
|
||||
$data = array(
|
||||
$errorInfo['paramName'],
|
||||
$itemInfo['name'],
|
||||
);
|
||||
|
||||
if ($errorInfo['optionalRecommended'] !== '') {
|
||||
$error .= 'since PHP %s ';
|
||||
$errorCode .= 'SoftRecommended';
|
||||
$data[] = $errorInfo['optionalRecommended'];
|
||||
} else {
|
||||
if ($errorInfo['optionalDeprecated'] !== '') {
|
||||
$error .= 'deprecated since PHP %s and ';
|
||||
$errorCode .= 'SoftRequired';
|
||||
$data[] = $errorInfo['optionalDeprecated'];
|
||||
}
|
||||
|
||||
if ($errorInfo['optionalRemoved'] !== '') {
|
||||
$error .= 'removed since PHP %s and ';
|
||||
$errorCode .= 'HardRequired';
|
||||
$data[] = $errorInfo['optionalRemoved'];
|
||||
}
|
||||
|
||||
// Remove the last 'and' from the message.
|
||||
$error = substr($error, 0, (\strlen($error) - 5));
|
||||
}
|
||||
|
||||
$this->addMessage($phpcsFile, $error, $stackPtr, $errorInfo['error'], $errorCode, $data);
|
||||
}
|
||||
}
|
||||
+292
@@ -0,0 +1,292 @@
|
||||
<?php
|
||||
/**
|
||||
* PHPCompatibility, an external standard for PHP_CodeSniffer.
|
||||
*
|
||||
* @package PHPCompatibility
|
||||
* @copyright 2012-2019 PHPCompatibility Contributors
|
||||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
|
||||
* @link https://github.com/PHPCompatibility/PHPCompatibility
|
||||
*/
|
||||
|
||||
namespace PHPCompatibility\Sniffs\FunctionUse;
|
||||
|
||||
use PHPCompatibility\AbstractRemovedFeatureSniff;
|
||||
use PHP_CodeSniffer_File as File;
|
||||
use PHP_CodeSniffer_Tokens as Tokens;
|
||||
|
||||
/**
|
||||
* Detect use of deprecated/removed function parameters in calls to native PHP functions.
|
||||
*
|
||||
* PHP version All
|
||||
*
|
||||
* @link https://www.php.net/manual/en/doc.changelog.php
|
||||
*
|
||||
* @since 7.0.0
|
||||
* @since 7.1.0 Now extends the `AbstractRemovedFeatureSniff` instead of the base `Sniff` class.
|
||||
*/
|
||||
class RemovedFunctionParametersSniff extends AbstractRemovedFeatureSniff
|
||||
{
|
||||
/**
|
||||
* A list of removed function parameters, which were present in older versions.
|
||||
*
|
||||
* The array lists : version number with false (deprecated) and true (removed).
|
||||
* The index is the location of the parameter in the parameter list, starting at 0 !
|
||||
* If's sufficient to list the first version where the function parameter was deprecated/removed.
|
||||
*
|
||||
* The optional `callback` key can be used to pass a method name which should be called for an
|
||||
* additional check. The method will be passed the parameter info and should return true
|
||||
* if the notice should be thrown or false otherwise.
|
||||
*
|
||||
* @since 7.0.0
|
||||
* @since 7.0.2 Visibility changed from `public` to `protected`.
|
||||
* @since 9.3.0 Optional `callback` key.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $removedFunctionParameters = array(
|
||||
'curl_version' => array(
|
||||
0 => array(
|
||||
'name' => 'age',
|
||||
'7.4' => false,
|
||||
'callback' => 'curlVersionInvalidValue',
|
||||
),
|
||||
),
|
||||
'define' => array(
|
||||
2 => array(
|
||||
'name' => 'case_insensitive',
|
||||
'7.3' => false, // Slated for removal in PHP 8.0.0.
|
||||
),
|
||||
),
|
||||
'gmmktime' => array(
|
||||
6 => array(
|
||||
'name' => 'is_dst',
|
||||
'5.1' => false,
|
||||
'7.0' => true,
|
||||
),
|
||||
),
|
||||
'ldap_first_attribute' => array(
|
||||
2 => array(
|
||||
'name' => 'ber_identifier',
|
||||
'5.2.4' => true,
|
||||
),
|
||||
),
|
||||
'ldap_next_attribute' => array(
|
||||
2 => array(
|
||||
'name' => 'ber_identifier',
|
||||
'5.2.4' => true,
|
||||
),
|
||||
),
|
||||
'mktime' => array(
|
||||
6 => array(
|
||||
'name' => 'is_dst',
|
||||
'5.1' => false,
|
||||
'7.0' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @since 7.0.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
// Handle case-insensitivity of function names.
|
||||
$this->removedFunctionParameters = $this->arrayKeysToLowercase($this->removedFunctionParameters);
|
||||
|
||||
return array(\T_STRING);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @since 7.0.0
|
||||
*
|
||||
* @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(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
$ignore = array(
|
||||
\T_DOUBLE_COLON => true,
|
||||
\T_OBJECT_OPERATOR => true,
|
||||
\T_FUNCTION => true,
|
||||
\T_CONST => true,
|
||||
);
|
||||
|
||||
$prevToken = $phpcsFile->findPrevious(\T_WHITESPACE, ($stackPtr - 1), null, true);
|
||||
if (isset($ignore[$tokens[$prevToken]['code']]) === true) {
|
||||
// Not a call to a PHP function.
|
||||
return;
|
||||
}
|
||||
|
||||
$function = $tokens[$stackPtr]['content'];
|
||||
$functionLc = strtolower($function);
|
||||
|
||||
if (isset($this->removedFunctionParameters[$functionLc]) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr);
|
||||
$parameterCount = \count($parameters);
|
||||
if ($parameterCount === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the parameter count returned > 0, we know there will be valid open parenthesis.
|
||||
$openParenthesis = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true);
|
||||
$parameterOffsetFound = $parameterCount - 1;
|
||||
|
||||
foreach ($this->removedFunctionParameters[$functionLc] as $offset => $parameterDetails) {
|
||||
if ($offset <= $parameterOffsetFound) {
|
||||
if (isset($parameterDetails['callback']) && method_exists($this, $parameterDetails['callback'])) {
|
||||
if ($this->{$parameterDetails['callback']}($phpcsFile, $parameters[($offset + 1)]) === false) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$itemInfo = array(
|
||||
'name' => $function,
|
||||
'nameLc' => $functionLc,
|
||||
'offset' => $offset,
|
||||
);
|
||||
$this->handleFeature($phpcsFile, $openParenthesis, $itemInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the relevant sub-array for a specific item from a multi-dimensional array.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @param array $itemInfo Base information about the item.
|
||||
*
|
||||
* @return array Version and other information about the item.
|
||||
*/
|
||||
public function getItemArray(array $itemInfo)
|
||||
{
|
||||
return $this->removedFunctionParameters[$itemInfo['nameLc']][$itemInfo['offset']];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an array of the non-PHP-version array keys used in a sub-array.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getNonVersionArrayKeys()
|
||||
{
|
||||
return array('name', 'callback');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve the relevant detail (version) information for use in an error message.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @param array $itemArray Version and other information about the item.
|
||||
* @param array $itemInfo Base information about the item.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getErrorInfo(array $itemArray, array $itemInfo)
|
||||
{
|
||||
$errorInfo = parent::getErrorInfo($itemArray, $itemInfo);
|
||||
$errorInfo['paramName'] = $itemArray['name'];
|
||||
|
||||
return $errorInfo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the item name to be used for the creation of the error code.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @param array $itemInfo Base information about the item.
|
||||
* @param array $errorInfo Detail information about an item.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getItemName(array $itemInfo, array $errorInfo)
|
||||
{
|
||||
return $itemInfo['name'] . '_' . $errorInfo['paramName'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the error message template for this sniff.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getErrorMsgTemplate()
|
||||
{
|
||||
return 'The "%s" parameter for function %s() is ';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Filter the error data before it's passed to PHPCS.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @param array $data The error data array which was created.
|
||||
* @param array $itemInfo Base information about the item this error message applies to.
|
||||
* @param array $errorInfo Detail information about an item this error message applies to.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function filterErrorData(array $data, array $itemInfo, array $errorInfo)
|
||||
{
|
||||
array_shift($data);
|
||||
array_unshift($data, $errorInfo['paramName'], $itemInfo['name']);
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether curl_version() was passed the default CURLVERSION_NOW.
|
||||
*
|
||||
* @since 9.3.0
|
||||
*
|
||||
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
||||
* @param array $parameter Parameter info array.
|
||||
*
|
||||
* @return bool True if the value was not CURLVERSION_NOW, false otherwise.
|
||||
*/
|
||||
protected function curlVersionInvalidValue(File $phpcsFile, array $parameter)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
$raw = '';
|
||||
for ($i = $parameter['start']; $i <= $parameter['end']; $i++) {
|
||||
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$raw .= $tokens[$i]['content'];
|
||||
}
|
||||
|
||||
if ($raw !== 'CURLVERSION_NOW'
|
||||
&& $raw !== (string) \CURLVERSION_NOW
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+1104
File diff suppressed because it is too large
Load Diff
+350
@@ -0,0 +1,350 @@
|
||||
<?php
|
||||
/**
|
||||
* PHPCompatibility, an external standard for PHP_CodeSniffer.
|
||||
*
|
||||
* @package PHPCompatibility
|
||||
* @copyright 2012-2019 PHPCompatibility Contributors
|
||||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
|
||||
* @link https://github.com/PHPCompatibility/PHPCompatibility
|
||||
*/
|
||||
|
||||
namespace PHPCompatibility\Sniffs\FunctionUse;
|
||||
|
||||
use PHPCompatibility\AbstractComplexVersionSniff;
|
||||
use PHP_CodeSniffer_File as File;
|
||||
use PHP_CodeSniffer_Tokens as Tokens;
|
||||
|
||||
/**
|
||||
* Detect missing required function parameters in calls to native PHP functions.
|
||||
*
|
||||
* Specifically when those function parameters are no longer required in more recent PHP versions.
|
||||
*
|
||||
* PHP version All
|
||||
*
|
||||
* @link https://www.php.net/manual/en/doc.changelog.php
|
||||
*
|
||||
* @since 7.0.3
|
||||
* @since 7.1.0 Now extends the `AbstractComplexVersionSniff` instead of the base `Sniff` class.
|
||||
* @since 9.0.0 Renamed from `RequiredOptionalFunctionParametersSniff` to `RequiredToOptionalFunctionParametersSniff`.
|
||||
*/
|
||||
class RequiredToOptionalFunctionParametersSniff extends AbstractComplexVersionSniff
|
||||
{
|
||||
|
||||
/**
|
||||
* A list of function parameters, which were required in older versions and became optional later on.
|
||||
*
|
||||
* The array lists : version number with true (required) and false (optional).
|
||||
*
|
||||
* The index is the location of the parameter in the parameter list, starting at 0 !
|
||||
* If's sufficient to list the last version in which the parameter was still required.
|
||||
*
|
||||
* @since 7.0.3
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $functionParameters = array(
|
||||
'array_merge' => array(
|
||||
0 => array(
|
||||
'name' => 'array(s) to merge',
|
||||
'7.3' => true,
|
||||
'7.4' => false,
|
||||
),
|
||||
),
|
||||
'array_merge_recursive' => array(
|
||||
0 => array(
|
||||
'name' => 'array(s) to merge',
|
||||
'7.3' => true,
|
||||
'7.4' => false,
|
||||
),
|
||||
),
|
||||
'array_push' => array(
|
||||
1 => array(
|
||||
'name' => 'element to push',
|
||||
'7.2' => true,
|
||||
'7.3' => false,
|
||||
),
|
||||
),
|
||||
'array_unshift' => array(
|
||||
1 => array(
|
||||
'name' => 'element to prepend',
|
||||
'7.2' => true,
|
||||
'7.3' => false,
|
||||
),
|
||||
),
|
||||
'bcscale' => array(
|
||||
0 => array(
|
||||
'name' => 'scale',
|
||||
'7.2' => true,
|
||||
'7.3' => false,
|
||||
),
|
||||
),
|
||||
'ftp_fget' => array(
|
||||
3 => array(
|
||||
'name' => 'mode',
|
||||
'7.2' => true,
|
||||
'7.3' => false,
|
||||
),
|
||||
),
|
||||
'ftp_fput' => array(
|
||||
3 => array(
|
||||
'name' => 'mode',
|
||||
'7.2' => true,
|
||||
'7.3' => false,
|
||||
),
|
||||
),
|
||||
'ftp_get' => array(
|
||||
3 => array(
|
||||
'name' => 'mode',
|
||||
'7.2' => true,
|
||||
'7.3' => false,
|
||||
),
|
||||
),
|
||||
'ftp_nb_fget' => array(
|
||||
3 => array(
|
||||
'name' => 'mode',
|
||||
'7.2' => true,
|
||||
'7.3' => false,
|
||||
),
|
||||
),
|
||||
'ftp_nb_fput' => array(
|
||||
3 => array(
|
||||
'name' => 'mode',
|
||||
'7.2' => true,
|
||||
'7.3' => false,
|
||||
),
|
||||
),
|
||||
'ftp_nb_get' => array(
|
||||
3 => array(
|
||||
'name' => 'mode',
|
||||
'7.2' => true,
|
||||
'7.3' => false,
|
||||
),
|
||||
),
|
||||
'ftp_nb_put' => array(
|
||||
3 => array(
|
||||
'name' => 'mode',
|
||||
'7.2' => true,
|
||||
'7.3' => false,
|
||||
),
|
||||
),
|
||||
'ftp_put' => array(
|
||||
3 => array(
|
||||
'name' => 'mode',
|
||||
'7.2' => true,
|
||||
'7.3' => false,
|
||||
),
|
||||
),
|
||||
'getenv' => array(
|
||||
0 => array(
|
||||
'name' => 'varname',
|
||||
'7.0' => true,
|
||||
'7.1' => false,
|
||||
),
|
||||
),
|
||||
'preg_match_all' => array(
|
||||
2 => array(
|
||||
'name' => 'matches',
|
||||
'5.3' => true,
|
||||
'5.4' => false,
|
||||
),
|
||||
),
|
||||
'stream_socket_enable_crypto' => array(
|
||||
2 => array(
|
||||
'name' => 'crypto_type',
|
||||
'5.5' => true,
|
||||
'5.6' => false,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @since 7.0.3
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
// Handle case-insensitivity of function names.
|
||||
$this->functionParameters = $this->arrayKeysToLowercase($this->functionParameters);
|
||||
|
||||
return array(\T_STRING);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @since 7.0.3
|
||||
*
|
||||
* @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(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
$ignore = array(
|
||||
\T_DOUBLE_COLON => true,
|
||||
\T_OBJECT_OPERATOR => true,
|
||||
\T_FUNCTION => true,
|
||||
\T_CONST => true,
|
||||
\T_NEW => true,
|
||||
);
|
||||
|
||||
$prevToken = $phpcsFile->findPrevious(\T_WHITESPACE, ($stackPtr - 1), null, true);
|
||||
if (isset($ignore[$tokens[$prevToken]['code']]) === true) {
|
||||
// Not a call to a PHP function.
|
||||
return;
|
||||
}
|
||||
|
||||
$function = $tokens[$stackPtr]['content'];
|
||||
$functionLc = strtolower($function);
|
||||
|
||||
if (isset($this->functionParameters[$functionLc]) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$parameterCount = $this->getFunctionCallParameterCount($phpcsFile, $stackPtr);
|
||||
$openParenthesis = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true);
|
||||
|
||||
// If the parameter count returned > 0, we know there will be valid open parenthesis.
|
||||
if ($parameterCount === 0 && $tokens[$openParenthesis]['code'] !== \T_OPEN_PARENTHESIS) {
|
||||
return;
|
||||
}
|
||||
|
||||
$parameterOffsetFound = $parameterCount - 1;
|
||||
|
||||
foreach ($this->functionParameters[$functionLc] as $offset => $parameterDetails) {
|
||||
if ($offset > $parameterOffsetFound) {
|
||||
$itemInfo = array(
|
||||
'name' => $function,
|
||||
'nameLc' => $functionLc,
|
||||
'offset' => $offset,
|
||||
);
|
||||
$this->handleFeature($phpcsFile, $openParenthesis, $itemInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine whether an error/warning should be thrown for an item based on collected information.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @param array $errorInfo Detail information about an item.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function shouldThrowError(array $errorInfo)
|
||||
{
|
||||
return ($errorInfo['requiredVersion'] !== '');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the relevant sub-array for a specific item from a multi-dimensional array.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @param array $itemInfo Base information about the item.
|
||||
*
|
||||
* @return array Version and other information about the item.
|
||||
*/
|
||||
public function getItemArray(array $itemInfo)
|
||||
{
|
||||
return $this->functionParameters[$itemInfo['nameLc']][$itemInfo['offset']];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an array of the non-PHP-version array keys used in a sub-array.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getNonVersionArrayKeys()
|
||||
{
|
||||
return array('name');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve the relevant detail (version) information for use in an error message.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @param array $itemArray Version and other information about the item.
|
||||
* @param array $itemInfo Base information about the item.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getErrorInfo(array $itemArray, array $itemInfo)
|
||||
{
|
||||
$errorInfo = array(
|
||||
'paramName' => '',
|
||||
'requiredVersion' => '',
|
||||
);
|
||||
|
||||
$versionArray = $this->getVersionArray($itemArray);
|
||||
|
||||
if (empty($versionArray) === false) {
|
||||
foreach ($versionArray as $version => $required) {
|
||||
if ($required === true && $this->supportsBelow($version) === true) {
|
||||
$errorInfo['requiredVersion'] = $version;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$errorInfo['paramName'] = $itemArray['name'];
|
||||
|
||||
return $errorInfo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the error message template for this sniff.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getErrorMsgTemplate()
|
||||
{
|
||||
return 'The "%s" parameter for function %s() is missing, but was required for PHP version %s and lower';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates the error or warning for this item.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
||||
* @param int $stackPtr The position of the relevant token in
|
||||
* the stack.
|
||||
* @param array $itemInfo Base information about the item.
|
||||
* @param array $errorInfo Array with detail (version) information
|
||||
* relevant to the item.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addError(File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo)
|
||||
{
|
||||
$error = $this->getErrorMsgTemplate();
|
||||
$errorCode = $this->stringToErrorCode($itemInfo['name'] . '_' . $errorInfo['paramName']) . 'Missing';
|
||||
$data = array(
|
||||
$errorInfo['paramName'],
|
||||
$itemInfo['name'],
|
||||
$errorInfo['requiredVersion'],
|
||||
);
|
||||
|
||||
$phpcsFile->addError($error, $stackPtr, $errorCode, $data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user