Introducing coding standards validation.

This commit is contained in:
Marcos Schratzenstaller
2019-04-22 17:28:46 -03:00
committed by Nathan Rice
parent 6e40c921ab
commit fd504d41ac
1624 changed files with 190339 additions and 262 deletions
@@ -0,0 +1,439 @@
<?php
/**
* PHPCompatibility, an external standard for PHP_CodeSniffer.
*
* @package PHPCompatibility
* @copyright 2012-2018 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 value that was passed to a parameter, but will
* instead provide the current value (which might have been modified).
*
* PHP version 7.0
*
* @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.
*
* @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.
*
* @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.
*
* @var array
*/
private $noneFunctionCallIndicators = array(
T_DOUBLE_COLON => true,
T_OBJECT_OPERATOR => true,
);
/**
* The tokens for variable incrementing/decrementing.
*
* @var array
*/
private $plusPlusMinusMinus = array(
T_DEC => true,
T_INC => true,
);
/**
* Tokens to ignore when determining the start of a statement.
*
* @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.
*
* @return array
*/
public function register()
{
return array(
T_FUNCTION,
T_CLOSURE,
);
}
/**
* 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(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);
}
}
}
@@ -0,0 +1,164 @@
<?php
/**
* \PHPCompatibility\Sniffs\FunctionUse\ArgumentFunctionsUsageSniff.
*
* PHP version 5.3
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\FunctionUse;
use PHPCompatibility\Sniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* \PHPCompatibility\Sniffs\FunctionUse\ArgumentFunctionsUsageSniff.
*
* - 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
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class ArgumentFunctionsUsageSniff extends Sniff
{
/**
* The target functions for this sniff.
*
* @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.
*
* @return array
*/
public function register()
{
return array(T_STRING);
}
/**
* 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(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 usage 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 usage 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
);
}
}
@@ -0,0 +1,159 @@
<?php
/**
* \PHPCompatibility\Sniffs\FunctionUse\OptionalToRequiredFunctionParametersSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\FunctionUse;
use PHPCompatibility\Sniffs\FunctionUse\RequiredToOptionalFunctionParametersSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\FunctionUse\OptionalToRequiredFunctionParametersSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
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.
*
* @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.
*
* @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.
*
* @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.
*
* @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);
}
}
@@ -0,0 +1,219 @@
<?php
/**
* \PHPCompatibility\Sniffs\FunctionUse\RemovedFunctionParametersSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim.godden@cu.be>
*/
namespace PHPCompatibility\Sniffs\FunctionUse;
use PHPCompatibility\AbstractRemovedFeatureSniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* \PHPCompatibility\Sniffs\FunctionUse\RemovedFunctionParametersSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim.godden@cu.be>
*/
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.
*
* @var array
*/
protected $removedFunctionParameters = array(
'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.
*
* @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.
*
* @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;
}
$parameterCount = $this->getFunctionCallParameterCount($phpcsFile, $stackPtr);
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) {
$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.
*
* @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.
*
* @return array
*/
protected function getNonVersionArrayKeys()
{
return array('name');
}
/**
* Retrieve the relevant detail (version) information for use in an error message.
*
* @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.
*
* @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.
*
* @return string
*/
protected function getErrorMsgTemplate()
{
return 'The "%s" parameter for function %s() is ';
}
/**
* Filter the error data before it's passed to PHPCS.
*
* @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;
}
}
@@ -0,0 +1,970 @@
<?php
/**
* \PHPCompatibility\Sniffs\FunctionUse\RemovedFunctionsSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim.godden@cu.be>
*/
namespace PHPCompatibility\Sniffs\FunctionUse;
use PHPCompatibility\AbstractRemovedFeatureSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\FunctionUse\RemovedFunctionsSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim.godden@cu.be>
*/
class RemovedFunctionsSniff extends AbstractRemovedFeatureSniff
{
/**
* A list of deprecated and removed functions with their alternatives.
*
* The array lists : version number with false (deprecated) or true (removed) and an alternative function.
* If no alternative exists, it is NULL, i.e, the function should just not be used.
*
* @var array(string => array(string => bool|string|null))
*/
protected $removedFunctions = array(
'php_check_syntax' => array(
'5.0.5' => true,
'alternative' => null,
),
'call_user_method' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => 'call_user_func()',
),
'call_user_method_array' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => 'call_user_func_array()',
),
'define_syslog_variables' => array(
'5.3' => false,
'5.4' => true,
'5.5' => true,
'5.6' => true,
'alternative' => null,
),
'dl' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => null,
),
'ereg' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => 'preg_match()',
),
'ereg_replace' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => 'preg_replace()',
),
'eregi' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => 'preg_match()',
),
'eregi_replace' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => 'preg_replace()',
),
'imagepsbbox' => array(
'7.0' => true,
'alternative' => null,
),
'imagepsencodefont' => array(
'7.0' => true,
'alternative' => null,
),
'imagepsextendfont' => array(
'7.0' => true,
'alternative' => null,
),
'imagepsfreefont' => array(
'7.0' => true,
'alternative' => null,
),
'imagepsloadfont' => array(
'7.0' => true,
'alternative' => null,
),
'imagepsslantfont' => array(
'7.0' => true,
'alternative' => null,
),
'imagepstext' => array(
'7.0' => true,
'alternative' => null,
),
'import_request_variables' => array(
'5.3' => false,
'5.4' => true,
'alternative' => null,
),
'ldap_sort' => array(
'7.0' => false,
'alternative' => null,
),
'mcrypt_generic_end' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => 'mcrypt_generic_deinit()',
),
'mysql_db_query' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => 'mysqli::select_db() and mysqli::query()',
),
'mysql_escape_string' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => 'mysqli::real_escape_string()',
),
'mysql_list_dbs' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => null,
),
'mysqli_bind_param' => array(
'5.3' => false,
'5.4' => true,
'5.5' => true,
'5.6' => true,
'alternative' => 'mysqli_stmt::bind_param()',
),
'mysqli_bind_result' => array(
'5.3' => false,
'5.4' => true,
'5.5' => true,
'5.6' => true,
'alternative' => 'mysqli_stmt::bind_result()',
),
'mysqli_client_encoding' => array(
'5.3' => false,
'5.4' => true,
'5.5' => true,
'5.6' => true,
'alternative' => 'mysqli::character_set_name()',
),
'mysqli_fetch' => array(
'5.3' => false,
'5.4' => true,
'5.5' => true,
'5.6' => true,
'alternative' => 'mysqli_stmt::fetch()',
),
'mysqli_param_count' => array(
'5.3' => false,
'5.4' => true,
'5.5' => true,
'5.6' => true,
'alternative' => 'mysqli_stmt_param_count()',
),
'mysqli_get_metadata' => array(
'5.3' => false,
'5.4' => true,
'5.5' => true,
'5.6' => true,
'alternative' => 'mysqli_stmt::result_metadata()',
),
'mysqli_send_long_data' => array(
'5.3' => false,
'5.4' => true,
'5.5' => true,
'5.6' => true,
'alternative' => 'mysqli_stmt::send_long_data()',
),
'magic_quotes_runtime' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => null,
),
'session_register' => array(
'5.3' => false,
'5.4' => true,
'5.5' => true,
'5.6' => true,
'alternative' => '$_SESSION',
),
'session_unregister' => array(
'5.3' => false,
'5.4' => true,
'5.5' => true,
'5.6' => true,
'alternative' => '$_SESSION',
),
'session_is_registered' => array(
'5.3' => false,
'5.4' => true,
'5.5' => true,
'5.6' => true,
'alternative' => '$_SESSION',
),
'set_magic_quotes_runtime' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => null,
),
'set_socket_blocking' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => 'stream_set_blocking()',
),
'split' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => 'preg_split()',
),
'spliti' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => 'preg_split()',
),
'sql_regcase' => array(
'5.3' => false,
'5.4' => false,
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => null,
),
'php_logo_guid' => array(
'5.5' => true,
'5.6' => true,
'alternative' => null,
),
'php_egg_logo_guid' => array(
'5.5' => true,
'5.6' => true,
'alternative' => null,
),
'php_real_logo_guid' => array(
'5.5' => true,
'5.6' => true,
'alternative' => null,
),
'zend_logo_guid' => array(
'5.5' => true,
'5.6' => true,
'alternative' => null,
),
'datefmt_set_timezone_id' => array(
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => 'IntlDateFormatter::setTimeZone()',
),
'mcrypt_ecb' => array(
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => null,
),
'mcrypt_cbc' => array(
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => null,
),
'mcrypt_cfb' => array(
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => null,
),
'mcrypt_ofb' => array(
'5.5' => false,
'5.6' => false,
'7.0' => true,
'alternative' => null,
),
'ocibindbyname' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_bind_by_name()',
),
'ocicancel' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_cancel()',
),
'ocicloselob' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Lob::close()',
),
'ocicollappend' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Collection::append()',
),
'ocicollassign' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Collection::assign()',
),
'ocicollassignelem' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Collection::assignElem()',
),
'ocicollgetelem' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Collection::getElem()',
),
'ocicollmax' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Collection::max()',
),
'ocicollsize' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Collection::size()',
),
'ocicolltrim' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Collection::trim()',
),
'ocicolumnisnull' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_field_is_null()',
),
'ocicolumnname' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_field_name()',
),
'ocicolumnprecision' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_field_precision()',
),
'ocicolumnscale' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_field_scale()',
),
'ocicolumnsize' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_field_size()',
),
'ocicolumntype' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_field_type()',
),
'ocicolumntyperaw' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_field_type_raw()',
),
'ocicommit' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_commit()',
),
'ocidefinebyname' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_define_by_name()',
),
'ocierror' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_error()',
),
'ociexecute' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_execute()',
),
'ocifetch' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_fetch()',
),
'ocifetchinto' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => null,
),
'ocifetchstatement' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_fetch_all()',
),
'ocifreecollection' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Collection::free()',
),
'ocifreecursor' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_free_statement()',
),
'ocifreedesc' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Lob::free()',
),
'ocifreestatement' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_free_statement()',
),
'ociinternaldebug' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_internal_debug()',
),
'ociloadlob' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Lob::load()',
),
'ocilogoff' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_close()',
),
'ocilogon' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_connect()',
),
'ocinewcollection' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_new_collection()',
),
'ocinewcursor' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_new_cursor()',
),
'ocinewdescriptor' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_new_descriptor()',
),
'ocinlogon' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_new_connect()',
),
'ocinumcols' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_num_fields()',
),
'ociparse' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_parse()',
),
'ociplogon' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_pconnect()',
),
'ociresult' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_result()',
),
'ocirollback' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_rollback()',
),
'ocirowcount' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_num_rows()',
),
'ocisavelob' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Lob::save()',
),
'ocisavelobfile' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Lob::import()',
),
'ociserverversion' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_server_version()',
),
'ocisetprefetch' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_set_prefetch()',
),
'ocistatementtype' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'oci_statement_type()',
),
'ociwritelobtofile' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Lob::export()',
),
'ociwritetemporarylob' => array(
'5.4' => false,
'5.5' => false,
'5.6' => false,
'alternative' => 'OCI-Lob::writeTemporary()',
),
'mysqli_get_cache_stats' => array(
'5.4' => true,
'alternative' => null,
),
'mcrypt_create_iv' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'random_bytes() or OpenSSL',
),
'mcrypt_decrypt' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_enc_get_algorithms_name' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_enc_get_block_size' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_enc_get_iv_size' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_enc_get_key_size' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_enc_get_modes_name' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_enc_get_supported_key_sizes' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_enc_is_block_algorithm_mode' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_enc_is_block_algorithm' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_enc_is_block_mode' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_enc_self_test' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_encrypt' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_generic_deinit' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_generic_init' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_generic' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_get_block_size' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_get_cipher_name' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_get_iv_size' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_get_key_size' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_list_algorithms' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_list_modes' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_module_close' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_module_get_algo_block_size' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_module_get_algo_key_size' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_module_get_supported_key_sizes' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_module_is_block_algorithm_mode' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_module_is_block_algorithm' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_module_is_block_mode' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_module_open' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mcrypt_module_self_test' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'mdecrypt_generic' => array(
'7.1' => false,
'7.2' => true,
'alternative' => 'OpenSSL',
),
'jpeg2wbmp' => array(
'7.2' => false,
'alternative' => 'imagecreatefromjpeg() and imagewbmp()',
),
'png2wbmp' => array(
'7.2' => false,
'alternative' => 'imagecreatefrompng() or imagewbmp()',
),
'create_function' => array(
'7.2' => false,
'alternative' => 'an anonymous function',
),
'each' => array(
'7.2' => false,
'alternative' => 'a foreach loop',
),
'gmp_random' => array(
'7.2' => false,
'alternative' => 'gmp_random_bits() or gmp_random_range()',
),
'read_exif_data' => array(
'7.2' => false,
'alternative' => 'exif_read_data()',
),
'image2wbmp' => array(
'7.3' => false,
'alternative' => 'imagewbmp()',
),
'mbregex_encoding' => array(
'7.3' => false,
'alternative' => 'mb_regex_encoding()',
),
'mbereg' => array(
'7.3' => false,
'alternative' => 'mb_ereg()',
),
'mberegi' => array(
'7.3' => false,
'alternative' => 'mb_eregi()',
),
'mbereg_replace' => array(
'7.3' => false,
'alternative' => 'mb_ereg_replace()',
),
'mberegi_replace' => array(
'7.3' => false,
'alternative' => 'mb_eregi_replace()',
),
'mbsplit' => array(
'7.3' => false,
'alternative' => 'mb_split()',
),
'mbereg_match' => array(
'7.3' => false,
'alternative' => 'mb_ereg_match()',
),
'mbereg_search' => array(
'7.3' => false,
'alternative' => 'mb_ereg_search()',
),
'mbereg_search_pos' => array(
'7.3' => false,
'alternative' => 'mb_ereg_search_pos()',
),
'mbereg_search_regs' => array(
'7.3' => false,
'alternative' => 'mb_ereg_search_regs()',
),
'mbereg_search_init' => array(
'7.3' => false,
'alternative' => 'mb_ereg_search_init()',
),
'mbereg_search_getregs' => array(
'7.3' => false,
'alternative' => 'mb_ereg_search_getregs()',
),
'mbereg_search_getpos' => array(
'7.3' => false,
'alternative' => 'mb_ereg_search_getpos()',
),
'mbereg_search_setpos' => array(
'7.3' => false,
'alternative' => 'mb_ereg_search_setpos()',
),
'fgetss' => array(
'7.3' => false,
'alternative' => null,
),
'gzgetss' => array(
'7.3' => false,
'alternative' => null,
),
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
// Handle case-insensitivity of function names.
$this->removedFunctions = $this->arrayKeysToLowercase($this->removedFunctions);
return array(T_STRING);
}
/**
* 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(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$ignore = array(
T_DOUBLE_COLON => true,
T_OBJECT_OPERATOR => true,
T_FUNCTION => true,
T_CLASS => true,
T_CONST => true,
T_USE => true,
T_NS_SEPARATOR => 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->removedFunctions[$functionLc]) === false) {
return;
}
$itemInfo = array(
'name' => $function,
'nameLc' => $functionLc,
);
$this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
}
/**
* Get the relevant sub-array for a specific item from a multi-dimensional array.
*
* @param array $itemInfo Base information about the item.
*
* @return array Version and other information about the item.
*/
public function getItemArray(array $itemInfo)
{
return $this->removedFunctions[$itemInfo['nameLc']];
}
/**
* Get the error message template for this sniff.
*
* @return string
*/
protected function getErrorMsgTemplate()
{
return 'Function %s() is ';
}
}
@@ -0,0 +1,310 @@
<?php
/**
* \PHPCompatibility\Sniffs\FunctionUse\RequiredToOptionalFunctionParametersSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\FunctionUse;
use PHPCompatibility\AbstractComplexVersionSniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* \PHPCompatibility\Sniffs\FunctionUse\RequiredToOptionalFunctionParametersSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
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.
*
* @var array
*/
protected $functionParameters = array(
'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.
*
* @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.
*
* @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->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.
*
* @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.
*
* @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.
*
* @return array
*/
protected function getNonVersionArrayKeys()
{
return array('name');
}
/**
* Retrieve the relevant detail (version) information for use in an error message.
*
* @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.
*
* @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.
*
* @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);
}
}