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,80 @@
<?php
/**
* \PHPCompatibility\Sniffs\ParameterValues\ForbiddenGetClassNullSniff.
*
* PHP version 7.2
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\ParameterValues\ForbiddenGetClassNullSniff.
*
* Detect: Passing `null` to get_class() is no longer allowed as of PHP 7.2.
* This will now result in an E_WARNING being thrown.
*
* PHP version 7.2
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class ForbiddenGetClassNullSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @var array
*/
protected $targetFunctions = array(
'get_class' => true,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsAbove('7.2') === false);
}
/**
* Process the parameters of a matched function.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack.
* @param string $functionName The token content (function name) which was matched.
* @param array $parameters Array with information about the parameters.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
{
if (isset($parameters[1]) === false) {
return;
}
if ($parameters[1]['raw'] !== 'null') {
return;
}
$phpcsFile->addError(
'Passing "null" as the $object to get_class() is not allowed since PHP 7.2.',
$parameters[1]['start'],
'Found'
);
}
}
@@ -0,0 +1,106 @@
<?php
/**
* \PHPCompatibility\Sniffs\ParameterValues\NewArrayReduceInitialTypeSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\ParameterValues\NewArrayReduceInitialTypeSniff.
*
* Detect: In PHP 5.2 and lower, the $initial parameter had to be an integer.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class NewArrayReduceInitialTypeSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @var array
*/
protected $targetFunctions = array(
'array_reduce' => true,
);
/**
* Tokens which, for the purposes of this sniff, indicate that there is
* a variable element to the value passed.
*
* @var array
*/
private $variableValueTokens = array(
T_VARIABLE,
T_STRING,
T_SELF,
T_PARENT,
T_STATIC,
T_DOUBLE_QUOTED_STRING,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsBelow('5.2') === false);
}
/**
* Process the parameters of a matched function.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack.
* @param string $functionName The token content (function name) which was matched.
* @param array $parameters Array with information about the parameters.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
{
if (isset($parameters[3]) === false) {
return;
}
$targetParam = $parameters[3];
if ($this->isNumber($phpcsFile, $targetParam['start'], $targetParam['end'], true) !== false) {
return;
}
if ($this->isNumericCalculation($phpcsFile, $targetParam['start'], $targetParam['end']) === true) {
return;
}
$error = 'Passing a non-integer as the value for $initial to array_reduce() is not supported in PHP 5.2 or lower.';
if ($phpcsFile->findNext($this->variableValueTokens, $targetParam['start'], ($targetParam['end'] + 1)) === false) {
$phpcsFile->addError(
$error . ' Found %s',
$targetParam['start'],
'InvalidTypeFound',
array($targetParam['raw'])
);
} else {
$phpcsFile->addWarning(
$error . ' Variable value found. Found %s',
$targetParam['start'],
'VariableFound',
array($targetParam['raw'])
);
}
}
}
@@ -0,0 +1,112 @@
<?php
/**
* \PHPCompatibility\Sniffs\ParameterValues\NewFopenModesSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\ParameterValues\NewFopenModesSniff.
*
* Detect: Changes in allowed values for the fopen() $mode parameter.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class NewFopenModesSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @var array
*/
protected $targetFunctions = array(
'fopen' => true,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @return bool
*/
protected function bowOutEarly()
{
// Version used here should be (above) the highest version from the `newModes` control,
// structure below, i.e. the last PHP version in which a new mode was introduced.
return ($this->supportsBelow('7.1') === false);
}
/**
* Process the parameters of a matched function.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack.
* @param string $functionName The token content (function name) which was matched.
* @param array $parameters Array with information about the parameters.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
{
if (isset($parameters[2]) === false) {
return;
}
$tokens = $phpcsFile->getTokens();
$targetParam = $parameters[2];
$errors = array();
for ($i = $targetParam['start']; $i <= $targetParam['end']; $i++) {
if ($tokens[$i]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
continue;
}
if (strpos($tokens[$i]['content'], 'c+') !== false && $this->supportsBelow('5.2.5')) {
$errors['cplusFound'] = array(
'c+',
'5.2.5',
$targetParam['raw'],
);
} elseif (strpos($tokens[$i]['content'], 'c') !== false && $this->supportsBelow('5.2.5')) {
$errors['cFound'] = array(
'c',
'5.2.5',
$targetParam['raw'],
);
}
if (strpos($tokens[$i]['content'], 'e') !== false && $this->supportsBelow('7.0.15')) {
$errors['eFound'] = array(
'e',
'7.0.15',
$targetParam['raw'],
);
}
}
if (empty($errors) === true) {
return;
}
foreach ($errors as $errorCode => $errorData) {
$phpcsFile->addError(
'Passing "%s" as the $mode to fopen() is not supported in PHP %s or lower. Found %s',
$targetParam['start'],
$errorCode,
$errorData
);
}
}
}
@@ -0,0 +1,167 @@
<?php
/**
* \PHPCompatibility\Sniffs\ParameterValues\NewHashAlgorithmsSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\ParameterValues;
use PHPCompatibility\AbstractNewFeatureSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\ParameterValues\NewHashAlgorithmsSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class NewHashAlgorithmsSniff extends AbstractNewFeatureSniff
{
/**
* A list of new hash algorithms, not present in older versions.
*
* The array lists : version number with false (not present) or true (present).
* If's sufficient to list the first version where the hash algorithm appears.
*
* @var array(string => array(string => bool))
*/
protected $newAlgorithms = array(
'md2' => array(
'5.2' => false,
'5.3' => true,
),
'ripemd256' => array(
'5.2' => false,
'5.3' => true,
),
'ripemd320' => array(
'5.2' => false,
'5.3' => true,
),
'salsa10' => array(
'5.2' => false,
'5.3' => true,
),
'salsa20' => array(
'5.2' => false,
'5.3' => true,
),
'snefru256' => array(
'5.2' => false,
'5.3' => true,
),
'sha224' => array(
'5.2' => false,
'5.3' => true,
),
'joaat' => array(
'5.3' => false,
'5.4' => true,
),
'fnv132' => array(
'5.3' => false,
'5.4' => true,
),
'fnv164' => array(
'5.3' => false,
'5.4' => true,
),
'gost-crypto' => array(
'5.5' => false,
'5.6' => true,
),
'sha512/224' => array(
'7.0' => false,
'7.1' => true,
),
'sha512/256' => array(
'7.0' => false,
'7.1' => true,
),
'sha3-224' => array(
'7.0' => false,
'7.1' => true,
),
'sha3-256' => array(
'7.0' => false,
'7.1' => true,
),
'sha3-384' => array(
'7.0' => false,
'7.1' => true,
),
'sha3-512' => array(
'7.0' => false,
'7.1' => 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)
{
$algo = $this->getHashAlgorithmParameter($phpcsFile, $stackPtr);
if (empty($algo) || is_string($algo) === false) {
return;
}
// Bow out if not one of the algorithms we're targetting.
if (isset($this->newAlgorithms[$algo]) === false) {
return;
}
// Check if the algorithm used is new.
$itemInfo = array(
'name' => $algo,
);
$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->newAlgorithms[$itemInfo['name']];
}
/**
* Get the error message template for this sniff.
*
* @return string
*/
protected function getErrorMsgTemplate()
{
return 'The %s hash algorithm is not present in PHP version %s or earlier';
}
}
@@ -0,0 +1,126 @@
<?php
/**
* \PHPCompatibility\Sniffs\ParameterValues\NewNegativeStringOffsetSniff.
*
* PHP version 7.1
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\ParameterValues\NewNegativeStringOffsetSniff.
*
* Detect: negative string offsets as parameters passed to functions where this
* was not allowed prior to PHP 7.1.
*
* PHP version 7.1
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class NewNegativeStringOffsetSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @var array Function name => 1-based parameter offset of the affected parameters => parameter name.
*/
protected $targetFunctions = array(
'file_get_contents' => array(
4 => 'offset',
),
'grapheme_extract' => array(
4 => 'start',
),
'grapheme_stripos' => array(
3 => 'offset',
),
'grapheme_strpos' => array(
3 => 'offset',
),
'iconv_strpos' => array(
3 => 'offset',
),
'mb_ereg_search_setpos' => array(
1 => 'position',
),
'mb_strimwidth' => array(
2 => 'start',
3 => 'width',
),
'mb_stripos' => array(
3 => 'offset',
),
'mb_strpos' => array(
3 => 'offset',
),
'stripos' => array(
3 => 'offset',
),
'strpos' => array(
3 => 'offset',
),
'substr_count' => array(
3 => 'offset',
4 => 'length',
),
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsBelow('7.0') === false);
}
/**
* Process the parameters of a matched function.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack.
* @param string $functionName The token content (function name) which was matched.
* @param array $parameters Array with information about the parameters.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
{
$functionLC = strtolower($functionName);
foreach ($this->targetFunctions[$functionLC] as $pos => $name) {
if (isset($parameters[$pos]) === false) {
continue;
}
$targetParam = $parameters[$pos];
if ($this->isNegativeNumber($phpcsFile, $targetParam['start'], $targetParam['end']) === false) {
continue;
}
$phpcsFile->addError(
'Negative string offsets were not supported for the $%s parameter in %s() in PHP 7.0 or lower. Found %s',
$targetParam['start'],
'Found',
array(
$name,
$functionName,
$targetParam['raw'],
)
);
}
}
}
@@ -0,0 +1,115 @@
<?php
/**
* \PHPCompatibility\Sniffs\ParameterValues\NewPCREModifiers.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\ParameterValues;
use PHPCompatibility\Sniffs\ParameterValues\RemovedPCREModifiersSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\ParameterValues\NewPCREModifiers.
*
* Check for usage of newly added regex modifiers for PCRE functions.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class NewPCREModifiersSniff extends RemovedPCREModifiersSniff
{
/**
* Functions to check for.
*
* @var array
*/
protected $targetFunctions = array(
'preg_replace' => true,
'preg_filter' => true,
'preg_grep' => true,
'preg_match_all' => true,
'preg_match' => true,
'preg_replace_callback_array' => true,
'preg_replace_callback' => true,
'preg_replace' => true,
'preg_split' => true,
);
/**
* Array listing newly introduced regex modifiers.
*
* The key should be the modifier (case-sensitive!).
* The value should be the PHP version in which the modifier was introduced.
*
* @var array
*/
protected $newModifiers = array(
'J' => array(
'7.1' => false,
'7.2' => true,
),
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @return bool
*/
protected function bowOutEarly()
{
// Version used here should be the highest version from the `$newModifiers` array,
// i.e. the last PHP version in which a new modifier was introduced.
return ($this->supportsBelow('7.2') === false);
}
/**
* Examine the regex modifier string.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
* @param string $functionName The function which contained the pattern.
* @param string $modifiers The regex modifiers found.
*
* @return void
*/
protected function examineModifiers(File $phpcsFile, $stackPtr, $functionName, $modifiers)
{
$error = 'The PCRE regex modifier "%s" is not present in PHP version %s or earlier';
foreach ($this->newModifiers as $modifier => $versionArray) {
if (strpos($modifiers, $modifier) === false) {
continue;
}
$notInVersion = '';
foreach ($versionArray as $version => $present) {
if ($notInVersion === '' && $present === false
&& $this->supportsBelow($version) === true
) {
$notInVersion = $version;
}
}
if ($notInVersion === '') {
continue;
}
$errorCode = $modifier . 'ModifierFound';
$data = array(
$modifier,
$notInVersion,
);
$phpcsFile->addError($error, $stackPtr, $errorCode, $data);
}
}
}
@@ -0,0 +1,123 @@
<?php
/**
* \PHPCompatibility\Sniffs\ParameterValues\NewPackFormatSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\ParameterValues\NewPackFormatSniff.
*
* Detect: Changes in the allowed values for $format passed to pack().
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class NewPackFormatSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @var array
*/
protected $targetFunctions = array(
'pack' => true,
);
/**
* List of new format character codes added to pack().
*
* @var array Regex pattern => Version array.
*/
protected $newFormats = array(
'`([Z])`' => array(
'5.4' => false,
'5.5' => true,
),
'`([qQJP])`' => array(
'5.6.2' => false,
'5.6.3' => true,
),
'`([eEgG])`' => array(
'7.0.14' => false,
'7.0.15' => true, // And 7.1.1.
),
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsBelow('7.1') === false);
}
/**
* Process the parameters of a matched function.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack.
* @param string $functionName The token content (function name) which was matched.
* @param array $parameters Array with information about the parameters.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
{
if (isset($parameters[1]) === false) {
return;
}
$tokens = $phpcsFile->getTokens();
$targetParam = $parameters[1];
for ($i = $targetParam['start']; $i <= $targetParam['end']; $i++) {
if ($tokens[$i]['code'] !== T_CONSTANT_ENCAPSED_STRING
&& $tokens[$i]['code'] !== T_DOUBLE_QUOTED_STRING
) {
continue;
}
$content = $tokens[$i]['content'];
if ($tokens[$i]['code'] === T_DOUBLE_QUOTED_STRING) {
$content = $this->stripVariables($content);
}
foreach ($this->newFormats as $pattern => $versionArray) {
if (preg_match($pattern, $content, $matches) !== 1) {
continue;
}
foreach ($versionArray as $version => $present) {
if ($present === false && $this->supportsBelow($version) === true) {
$phpcsFile->addError(
'Passing the $format(s) "%s" to pack() is not supported in PHP %s or lower. Found %s',
$targetParam['start'],
'NewFormatFound',
array(
$matches[1],
$version,
$targetParam['raw'],
)
);
continue 2;
}
}
}
}
}
}
@@ -0,0 +1,111 @@
<?php
/**
* \PHPCompatibility\Sniffs\ParameterValues\RemovedHashAlgorithmsSniff.
*
* PHP version 5.4
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim.godden@cu.be>
* @copyright 2012 Cu.be Solutions bvba
*/
namespace PHPCompatibility\Sniffs\ParameterValues;
use PHPCompatibility\AbstractRemovedFeatureSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\ParameterValues\RemovedHashAlgorithmsSniff.
*
* Discourages the use of deprecated and removed hash algorithms.
*
* PHP version 5.4
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim.godden@cu.be>
* @copyright 2012 Cu.be Solutions bvba
*/
class RemovedHashAlgorithmsSniff extends AbstractRemovedFeatureSniff
{
/**
* A list of removed hash algorithms, which were present in older versions.
*
* The array lists : version number with false (deprecated) and true (removed).
* If's sufficient to list the first version where the hash algorithm was deprecated/removed.
*
* @var array(string => array(string => bool))
*/
protected $removedAlgorithms = array(
'salsa10' => array(
'5.4' => true,
),
'salsa20' => array(
'5.4' => 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)
{
$algo = $this->getHashAlgorithmParameter($phpcsFile, $stackPtr);
if (empty($algo) || is_string($algo) === false) {
return;
}
// Bow out if not one of the algorithms we're targetting.
if (isset($this->removedAlgorithms[$algo]) === false) {
return;
}
$itemInfo = array(
'name' => $algo,
);
$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->removedAlgorithms[$itemInfo['name']];
}
/**
* Get the error message template for this sniff.
*
* @return string
*/
protected function getErrorMsgTemplate()
{
return 'The %s hash algorithm is ';
}
}
@@ -0,0 +1,80 @@
<?php
/**
* \PHPCompatibility\Sniffs\ParameterValues\RemovedIconvEncodingSniff.
*
* PHP version 5.6
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\ParameterValues\RemovedIconvEncodingSniff.
*
* Detect: "The iconv and mbstring configuration options related to encoding
* have been deprecated in favour of default_charset."
*
* {@internal It is unclear which mbstring functions should be targetted, so for now,
* only the iconv function is handled.}}
*
* PHP version 5.6
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class RemovedIconvEncodingSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @var array
*/
protected $targetFunctions = array(
'iconv_set_encoding' => true,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsAbove('5.6') === false);
}
/**
* Process the parameters of a matched function.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack.
* @param string $functionName The token content (function name) which was matched.
* @param array $parameters Array with information about the parameters.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
{
if (isset($parameters[1]) === false) {
return;
}
$phpcsFile->addWarning(
'All previously accepted values for the $type parameter of iconv_set_encoding() have been deprecated since PHP 5.6. Found %s',
$parameters[1]['start'],
'DeprecatedValueFound',
$parameters[1]['raw']
);
}
}
@@ -0,0 +1,122 @@
<?php
/**
* \PHPCompatibility\Sniffs\ParameterValues\RemovedMbstringModifiersSniff.
*
* PHP version 7.1
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* \PHPCompatibility\Sniffs\ParameterValues\RemovedMbstringModifiersSniff.
*
* PHP version 7.1
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class RemovedMbstringModifiersSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* Key is the function name, value the parameter position of the options parameter.
*
* @var array
*/
protected $targetFunctions = array(
'mb_ereg_replace' => 4,
'mb_eregi_replace' => 4,
'mb_regex_set_options' => 1,
'mbereg_replace' => 4, // Undocumented, but valid function alias.
'mberegi_replace' => 4, // Undocumented, but valid function alias.
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @return bool
*/
protected function bowOutEarly()
{
// Version used here should be the highest version from the `$newModifiers` array,
// i.e. the last PHP version in which a new modifier was introduced.
return ($this->supportsAbove('7.1') === false);
}
/**
* Process the parameters of a matched function.
*
* This method has to be made concrete in child classes.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack.
* @param string $functionName The token content (function name) which was matched.
* @param array $parameters Array with information about the parameters.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
{
$tokens = $phpcsFile->getTokens();
$functionNameLc = strtolower($functionName);
// Check whether the options parameter in the function call is passed.
if (isset($parameters[$this->targetFunctions[$functionNameLc]]) === false) {
return;
}
$optionsParam = $parameters[$this->targetFunctions[$functionNameLc]];
$stringToken = $phpcsFile->findNext(Tokens::$stringTokens, $optionsParam['start'], $optionsParam['end'] + 1);
if ($stringToken === false) {
// No string token found in the options parameter, so skip it (e.g. variable passed in).
return;
}
$options = '';
/*
* Get the content of any string tokens in the options parameter and remove the quotes and variables.
*/
for ($i = $stringToken; $i <= $optionsParam['end']; $i++) {
if (isset(Tokens::$stringTokens[$tokens[$i]['code']]) === false) {
continue;
}
$content = $this->stripQuotes($tokens[$i]['content']);
if ($tokens[$i]['code'] === T_DOUBLE_QUOTED_STRING) {
$content = $this->stripVariables($content);
}
$content = trim($content);
if (empty($content) === false) {
$options .= $content;
}
}
if (strpos($options, 'e') !== false) {
$error = 'The Mbstring regex "e" modifier is deprecated since PHP 7.1.';
// The alternative mb_ereg_replace_callback() function is only available since 5.4.1.
if ($this->supportsBelow('5.4.1') === false) {
$error .= ' Use mb_ereg_replace_callback() instead (PHP 5.4.1+).';
}
$phpcsFile->addWarning($error, $stackPtr, 'Deprecated');
}
}
}
@@ -0,0 +1,114 @@
<?php
/**
* \PHPCompatibility\Sniffs\ParameterValues\RemovedNonCryptoHashSniff.
*
* PHP version 7.2
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\ParameterValues\RemovedNonCryptoHashSniff.
*
* Detect: "The hash_hmac(), hash_hmac_file(), hash_pbkdf2(), and hash_init()
* (with HASH_HMAC) functions no longer accept non-cryptographic hashes."
*
* PHP version 7.2
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class RemovedNonCryptoHashSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @var array
*/
protected $targetFunctions = array(
'hash_hmac' => true,
'hash_hmac_file' => true,
'hash_init' => true,
'hash_pbkdf2' => true,
);
/**
* List of the non-cryptographic hashes.
*
* @var array
*/
protected $disabledCryptos = array(
'adler32' => true,
'crc32' => true,
'crc32b' => true,
'fnv132' => true,
'fnv1a32' => true,
'fnv164' => true,
'fnv1a64' => true,
'joaat' => true,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsAbove('7.2') === false);
}
/**
* Process the parameters of a matched function.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack.
* @param string $functionName The token content (function name) which was matched.
* @param array $parameters Array with information about the parameters.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
{
if (isset($parameters[1]) === false) {
return;
}
$targetParam = $parameters[1];
if (isset($this->disabledCryptos[$this->stripQuotes($targetParam['raw'])]) === false) {
return;
}
if (strtolower($functionName) === 'hash_init'
&& (isset($parameters[2]) === false
|| ($parameters[2]['raw'] !== 'HASH_HMAC'
&& $parameters[2]['raw'] !== (string) HASH_HMAC))
) {
// For hash_init(), these hashes are only disabled with HASH_HMAC set.
return;
}
$phpcsFile->addError(
'Non-cryptographic hashes are no longer accepted by function %s() since PHP 7.2. Found: %s',
$targetParam['start'],
$this->stringToErrorCode($functionName),
array(
$functionName,
$targetParam['raw'],
)
);
}
}
@@ -0,0 +1,219 @@
<?php
/**
* \PHPCompatibility\Sniffs\ParameterValues\RemovedPCREModifiersSniff.
*
* PHP version 5.5
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim.godden@cu.be>
* @copyright 2014 Cu.be Solutions bvba
*/
namespace PHPCompatibility\Sniffs\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* \PHPCompatibility\Sniffs\ParameterValues\RemovedPCREModifiersSniff.
*
* Check for usage of the `e` modifier with PCRE functions which is deprecated since PHP 5.5
* and removed as of PHP 7.0.
*
* PHP version 5.5
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim.godden@cu.be>
* @copyright 2014 Cu.be Solutions bvba
*/
class RemovedPCREModifiersSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @var array
*/
protected $targetFunctions = array(
'preg_replace' => true,
'preg_filter' => true,
);
/**
* Regex bracket delimiters.
*
* @var array
*/
protected $doublesSeparators = array(
'{' => '}',
'[' => ']',
'(' => ')',
'<' => '>',
);
/**
* Process the parameters of a matched function.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack.
* @param string $functionName The token content (function name) which was matched.
* @param array $parameters Array with information about the parameters.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
{
// Check the first parameter in the function call as that should contain the regex(es).
if (isset($parameters[1]) === false) {
return;
}
$tokens = $phpcsFile->getTokens();
$functionNameLc = strtolower($functionName);
$firstParam = $parameters[1];
// Differentiate between an array of patterns passed and a single pattern.
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $firstParam['start'], ($firstParam['end'] + 1), true);
if ($nextNonEmpty !== false && ($tokens[$nextNonEmpty]['code'] === T_ARRAY || $tokens[$nextNonEmpty]['code'] === T_OPEN_SHORT_ARRAY)) {
$arrayValues = $this->getFunctionCallParameters($phpcsFile, $nextNonEmpty);
if ($functionNameLc === 'preg_replace_callback_array') {
// For preg_replace_callback_array(), the patterns will be in the array keys.
foreach ($arrayValues as $value) {
$hasKey = $phpcsFile->findNext(T_DOUBLE_ARROW, $value['start'], ($value['end'] + 1));
if ($hasKey === false) {
continue;
}
$value['end'] = ($hasKey - 1);
$value['raw'] = trim($phpcsFile->getTokensAsString($value['start'], ($hasKey - $value['start'])));
$this->processRegexPattern($value, $phpcsFile, $value['end'], $functionName);
}
} else {
// Otherwise, the patterns will be in the array values.
foreach ($arrayValues as $value) {
$hasKey = $phpcsFile->findNext(T_DOUBLE_ARROW, $value['start'], ($value['end'] + 1));
if ($hasKey !== false) {
$value['start'] = ($hasKey + 1);
$value['raw'] = trim($phpcsFile->getTokensAsString($value['start'], (($value['end'] + 1) - $value['start'])));
}
$this->processRegexPattern($value, $phpcsFile, $value['end'], $functionName);
}
}
} else {
$this->processRegexPattern($firstParam, $phpcsFile, $stackPtr, $functionName);
}
}
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsAbove('5.5') === false);
}
/**
* Analyse a potential regex pattern for usage of the /e modifier.
*
* @param array $pattern Array containing the start and end token
* pointer of the potential regex pattern and
* the raw string value of the pattern.
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
* @param string $functionName The function which contained the pattern.
*
* @return void
*/
protected function processRegexPattern($pattern, File $phpcsFile, $stackPtr, $functionName)
{
$tokens = $phpcsFile->getTokens();
/*
* The pattern might be build up of a combination of strings, variables
* and function calls. We are only concerned with the strings.
*/
$regex = '';
for ($i = $pattern['start']; $i <= $pattern['end']; $i++) {
if (isset(Tokens::$stringTokens[$tokens[$i]['code']]) === true) {
$content = $this->stripQuotes($tokens[$i]['content']);
if ($tokens[$i]['code'] === T_DOUBLE_QUOTED_STRING) {
$content = $this->stripVariables($content);
}
$regex .= trim($content);
}
}
// Deal with multi-line regexes which were broken up in several string tokens.
if ($tokens[$pattern['start']]['line'] !== $tokens[$pattern['end']]['line']) {
$regex = $this->stripQuotes($regex);
}
if ($regex === '') {
// No string token found in the first parameter, so skip it (e.g. if variable passed in).
return;
}
$regexFirstChar = substr($regex, 0, 1);
// Make sure that the character identified as the delimiter is valid.
// Otherwise, it is a false positive caused by the string concatenation.
if (preg_match('`[a-z0-9\\\\ ]`i', $regexFirstChar) === 1) {
return;
}
if (isset($this->doublesSeparators[$regexFirstChar])) {
$regexEndPos = strrpos($regex, $this->doublesSeparators[$regexFirstChar]);
} else {
$regexEndPos = strrpos($regex, $regexFirstChar);
}
if ($regexEndPos !== false) {
$modifiers = substr($regex, $regexEndPos + 1);
$this->examineModifiers($phpcsFile, $stackPtr, $functionName, $modifiers);
}
}
/**
* Examine the regex modifier string.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
* @param string $functionName The function which contained the pattern.
* @param string $modifiers The regex modifiers found.
*
* @return void
*/
protected function examineModifiers(File $phpcsFile, $stackPtr, $functionName, $modifiers)
{
if (strpos($modifiers, 'e') !== false) {
$error = '%s() - /e modifier is deprecated since PHP 5.5';
$isError = false;
$errorCode = 'Deprecated';
$data = array($functionName);
if ($this->supportsAbove('7.0')) {
$error .= ' and removed since PHP 7.0';
$isError = true;
$errorCode = 'Removed';
}
$this->addMessage($phpcsFile, $error, $stackPtr, $isError, $errorCode, $data);
}
}
}
@@ -0,0 +1,99 @@
<?php
/**
* \PHPCompatibility\Sniffs\ParameterValues\RemovedSetlocaleStringSniff.
*
* PHP version 4.2
* PHP version 7.0
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\ParameterValues\RemovedSetlocaleStringSniff.
*
* Detect: Support for the category parameter passed as a string has been removed.
* Only LC_* constants can be used as of this version [7.0.0].
*
* PHP version 4.2
* PHP version 7.0
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class RemovedSetlocaleStringSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @var array
*/
protected $targetFunctions = array(
'setlocale' => true,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsAbove('4.2') === false);
}
/**
* Process the parameters of a matched function.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack.
* @param string $functionName The token content (function name) which was matched.
* @param array $parameters Array with information about the parameters.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
{
if (isset($parameters[1]) === false) {
return;
}
$tokens = $phpcsFile->getTokens();
$targetParam = $parameters[1];
for ($i = $targetParam['start']; $i <= $targetParam['end']; $i++) {
if ($tokens[$i]['code'] !== T_CONSTANT_ENCAPSED_STRING
&& $tokens[$i]['code'] !== T_DOUBLE_QUOTED_STRING
) {
continue;
}
$message = 'Passing the $category as a string to setlocale() has been deprecated since PHP 4.2';
$isError = false;
$errorCode = 'Deprecated';
$data = array($targetParam['raw']);
if ($this->supportsAbove('7.0') === true) {
$message .= ' and is removed since PHP 7.0';
$isError = true;
$errorCode = 'Removed';
}
$message .= '; Pass one of the LC_* constants instead. Found: %s';
$this->addMessage($phpcsFile, $message, $i, $isError, $errorCode, $data);
break;
}
}
}