基础代码

This commit is contained in:
2021-02-26 22:23:13 +08:00
parent 7884df52f0
commit a719feebba
2585 changed files with 328263 additions and 0 deletions
@@ -0,0 +1,84 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* 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
*
* @link https://wiki.php.net/rfc/get_class_disallow_null_parameter
* @link https://www.php.net/manual/en/function.get-class.php#refsect1-function.get-class-changelog
*
* @since 9.0.0
*/
class ForbiddenGetClassNullSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 9.0.0
*
* @var array
*/
protected $targetFunctions = array(
'get_class' => true,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @since 9.0.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsAbove('7.2') === false);
}
/**
* Process the parameters of a matched function.
*
* @since 9.0.0
*
* @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,113 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* Since PHP 5.3.4, `strip_tags()` ignores self-closing XHTML tags in allowable_tags
*
* PHP version 5.3.4
*
* @link https://www.php.net/manual/en/function.strip-tags.php#refsect1-function.strip-tags-changelog
*
* @since 9.3.0
*/
class ForbiddenStripTagsSelfClosingXHTMLSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 9.3.0
*
* @var array
*/
protected $targetFunctions = array(
'strip_tags' => true,
);
/**
* Text string tokens to examine.
*
* @since 9.3.0
*
* @var array
*/
private $textStringTokens = array(
\T_CONSTANT_ENCAPSED_STRING => true,
\T_DOUBLE_QUOTED_STRING => true,
\T_INLINE_HTML => true,
\T_HEREDOC => true,
\T_NOWDOC => true,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @since 9.3.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsAbove('5.4') === false);
}
/**
* Process the parameters of a matched function.
*
* @since 9.3.0
*
* @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];
for ($i = $targetParam['start']; $i <= $targetParam['end']; $i++) {
if ($tokens[$i]['code'] === \T_STRING
|| $tokens[$i]['code'] === \T_VARIABLE
) {
// Variable, constant, function call. Ignore as undetermined.
return;
}
if (isset($this->textStringTokens[$tokens[$i]['code']]) === true
&& strpos($tokens[$i]['content'], '/>') !== false
) {
$phpcsFile->addError(
'Self-closing XHTML tags are ignored. Only non-self-closing tags should be used in the strip_tags() $allowable_tags parameter since PHP 5.3.4. Found: %s',
$i,
'Found',
array($targetParam['raw'])
);
// Only throw one error per function call.
return;
}
}
}
}
@@ -0,0 +1,116 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* In PHP 5.2 and lower, the `$initial` parameter for `array_reduce()` had to be an integer.
*
* PHP version 5.3
*
* @link https://www.php.net/manual/en/migration53.other.php#migration53.other
* @link https://www.php.net/manual/en/function.array-reduce.php#refsect1-function.array-reduce-changelog
*
* @since 9.0.0
*/
class NewArrayReduceInitialTypeSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 9.0.0
*
* @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.
*
* @since 9.0.0
*
* @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.
*
* @since 9.0.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsBelow('5.2') === false);
}
/**
* Process the parameters of a matched function.
*
* @since 9.0.0
*
* @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,119 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* Check for valid values for the `fopen()` `$mode` parameter.
*
* PHP version 5.2+
*
* @link https://www.php.net/manual/en/function.fopen.php#refsect1-function.fopen-changelog
*
* @since 9.0.0
*/
class NewFopenModesSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 9.0.0
*
* @var array
*/
protected $targetFunctions = array(
'fopen' => true,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @since 9.0.0
*
* @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.
*
* @since 9.0.0
*
* @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,92 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* As of PHP 5.4, the default character set for `htmlspecialchars()`, `htmlentities()`
* and `html_entity_decode()` is now `UTF-8`, instead of `ISO-8859-1`.
*
* PHP version 5.4
*
* @link https://www.php.net/manual/en/migration54.other.php
* @link https://www.php.net/manual/en/function.html-entity-decode.php#refsect1-function.html-entity-decode-changelog
* @link https://www.php.net/manual/en/function.htmlentities.php#refsect1-function.htmlentities-changelog
* @link https://www.php.net/manual/en/function.htmlspecialchars.php#refsect1-function.htmlspecialchars-changelog
*
* @since 9.3.0
*/
class NewHTMLEntitiesEncodingDefaultSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* Key is the function name, value the 1-based parameter position of
* the $encoding parameter.
*
* @since 9.3.0
*
* @var array
*/
protected $targetFunctions = array(
'html_entity_decode' => 3,
'htmlentities' => 3,
'htmlspecialchars' => 3,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* Note: This sniff should only trigger errors when both PHP 5.3 or lower,
* as well as PHP 5.4 or higher need to be supported within the application.
*
* @since 9.3.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsBelow('5.3') === false || $this->supportsAbove('5.4') === false);
}
/**
* Process the parameters of a matched function.
*
* @since 9.3.0
*
* @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);
if (isset($parameters[$this->targetFunctions[$functionLC]]) === true) {
return;
}
$phpcsFile->addError(
'The default value of the $encoding parameter for %s() was changed from ISO-8859-1 to UTF-8 in PHP 5.4. For cross-version compatibility, the $encoding parameter should be explicitly set.',
$stackPtr,
'NotSet',
array($functionName)
);
}
}
@@ -0,0 +1,185 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractNewFeatureSniff;
use PHP_CodeSniffer_File as File;
/**
* Detect the use of newly introduced hash algorithms.
*
* PHP version 5.2+
*
* @link https://www.php.net/manual/en/function.hash-algos.php#refsect1-function.hash-algos-changelog
*
* @since 7.0.7
* @since 7.1.0 Now extends the `AbstractNewFeatureSniff` instead of the base `Sniff` class..
*/
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.
*
* @since 7.0.7
*
* @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,
),
'crc32c' => array(
'7.3' => false,
'7.4' => true,
),
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @since 7.0.7
*
* @return array
*/
public function register()
{
return array(\T_STRING);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @since 7.0.7
*
* @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.
*
* @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->newAlgorithms[$itemInfo['name']];
}
/**
* Get the error message template for this sniff.
*
* @since 7.1.0
*
* @return string
*/
protected function getErrorMsgTemplate()
{
return 'The %s hash algorithm is not present in PHP version %s or earlier';
}
}
@@ -0,0 +1,91 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* The default value for the `$variant` parameter has changed from `INTL_IDNA_VARIANT_2003`
* to `INTL_IDNA_VARIANT_UTS46` in PHP 7.4.
*
* PHP version 7.4
*
* @link https://www.php.net/manual/en/migration74.incompatible.php#migration74.incompatible.intl
* @link https://wiki.php.net/rfc/deprecate-and-remove-intl_idna_variant_2003
* @link https://www.php.net/manual/en/function.idn-to-ascii.php
* @link https://www.php.net/manual/en/function.idn-to-utf8.php
*
* @since 9.3.0
*/
class NewIDNVariantDefaultSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* Key is the function name, value the 1-based parameter position of
* the $variant parameter.
*
* @since 9.3.0
*
* @var array
*/
protected $targetFunctions = array(
'idn_to_ascii' => 3,
'idn_to_utf8' => 3,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* Note: This sniff should only trigger errors when both PHP 7.3 or lower,
* as well as PHP 7.4 or higher need to be supported within the application.
*
* @since 9.3.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsBelow('7.3') === false || $this->supportsAbove('7.4') === false);
}
/**
* Process the parameters of a matched function.
*
* @since 9.3.0
*
* @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);
if (isset($parameters[$this->targetFunctions[$functionLC]]) === true) {
return;
}
$error = 'The default value of the %s() $variant parameter has changed from INTL_IDNA_VARIANT_2003 to INTL_IDNA_VARIANT_UTS46 in PHP 7.4. For optimal cross-version compatibility, the $variant parameter should be explicitly set.';
$phpcsFile->addError(
$error,
$stackPtr,
'NotSet',
array($functionName)
);
}
}
@@ -0,0 +1,231 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* Detect calls to Iconv and Mbstring functions with the optional `$charset`/`$encoding` parameter not set.
*
* The default value for the iconv `$charset` and the MbString $encoding` parameters was changed
* in PHP 5.6 to the value of `default_charset`, which defaults to `UTF-8`.
*
* Previously, the iconv functions would default to the value of `iconv.internal_encoding`;
* The Mbstring functions would default to the return value of `mb_internal_encoding()`.
* In both case, this would normally come down to `ISO-8859-1`.
*
* PHP version 5.6
*
* @link https://www.php.net/manual/en/migration56.new-features.php#migration56.new-features.default-encoding
* @link https://www.php.net/manual/en/migration56.deprecated.php#migration56.deprecated.iconv-mbstring-encoding
* @link https://wiki.php.net/rfc/default_encoding
*
* @since 9.3.0
*/
class NewIconvMbstringCharsetDefaultSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* Only those functions where the charset/encoding parameter is optional need to be listed.
*
* Key is the function name, value the 1-based parameter position of
* the $charset/$encoding parameter.
*
* @since 9.3.0
*
* @var array
*/
protected $targetFunctions = array(
'iconv_mime_decode_headers' => 3,
'iconv_mime_decode' => 3,
'iconv_mime_encode' => 3, // Special case.
'iconv_strlen' => 2,
'iconv_strpos' => 4,
'iconv_strrpos' => 3,
'iconv_substr' => 4,
'mb_check_encoding' => 2,
'mb_chr' => 2,
'mb_convert_case' => 3,
'mb_convert_encoding' => 3,
'mb_convert_kana' => 3,
'mb_decode_numericentity' => 3,
'mb_encode_numericentity' => 3,
'mb_ord' => 2,
'mb_scrub' => 2,
'mb_strcut' => 4,
'mb_stripos' => 4,
'mb_stristr' => 4,
'mb_strlen' => 2,
'mb_strpos' => 4,
'mb_strrchr' => 4,
'mb_strrichr' => 4,
'mb_strripos' => 4,
'mb_strrpos' => 4,
'mb_strstr' => 4,
'mb_strtolower' => 2,
'mb_strtoupper' => 2,
'mb_strwidth' => 2,
'mb_substr_count' => 3,
'mb_substr' => 4,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* Note: This sniff should only trigger errors when both PHP 5.5 or lower,
* as well as PHP 5.6 or higher need to be supported within the application.
*
* @since 9.3.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsBelow('5.5') === false || $this->supportsAbove('5.6') === false);
}
/**
* Process the parameters of a matched function.
*
* @since 9.3.0
*
* @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);
if ($functionLC === 'iconv_mime_encode') {
// Special case the iconv_mime_encode() function.
return $this->processIconvMimeEncode($phpcsFile, $stackPtr, $functionName, $parameters);
}
if (isset($parameters[$this->targetFunctions[$functionLC]]) === true) {
return;
}
$paramName = '$encoding';
if (strpos($functionLC, 'iconv_') === 0) {
$paramName = '$charset';
} elseif ($functionLC === 'mb_convert_encoding') {
$paramName = '$from_encoding';
}
$error = 'The default value of the %1$s parameter for %2$s() was changed from ISO-8859-1 to UTF-8 in PHP 5.6. For cross-version compatibility, the %1$s parameter should be explicitly set.';
$data = array(
$paramName,
$functionName,
);
$phpcsFile->addError($error, $stackPtr, 'NotSet', $data);
}
/**
* Process the parameters of a matched call to the iconv_mime_encode() function.
*
* @since 9.3.0
*
* @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 processIconvMimeEncode(File $phpcsFile, $stackPtr, $functionName, $parameters)
{
$error = 'The default value of the %s parameter index for iconv_mime_encode() was changed from ISO-8859-1 to UTF-8 in PHP 5.6. For cross-version compatibility, the %s should be explicitly set.';
$functionLC = strtolower($functionName);
if (isset($parameters[$this->targetFunctions[$functionLC]]) === false) {
$phpcsFile->addError(
$error,
$stackPtr,
'PreferencesNotSet',
array(
'$preferences[\'input/output-charset\']',
'$preferences[\'input-charset\'] and $preferences[\'output-charset\'] indexes',
)
);
return;
}
$tokens = $phpcsFile->getTokens();
$targetParam = $parameters[$this->targetFunctions[$functionLC]];
$firstNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $targetParam['start'], ($targetParam['end'] + 1), true);
if ($firstNonEmpty === false) {
// Parse error or live coding.
return;
}
if ($tokens[$firstNonEmpty]['code'] === \T_ARRAY
|| $tokens[$firstNonEmpty]['code'] === \T_OPEN_SHORT_ARRAY
) {
$hasInputCharset = preg_match('`([\'"])input-charset\1\s*=>`', $targetParam['raw']);
$hasOutputCharset = preg_match('`([\'"])output-charset\1\s*=>`', $targetParam['raw']);
if ($hasInputCharset === 1 && $hasOutputCharset === 1) {
// Both input as well as output charset are set.
return;
}
if ($hasInputCharset !== 1) {
$phpcsFile->addError(
$error,
$firstNonEmpty,
'InputPreferenceNotSet',
array(
'$preferences[\'input-charset\']',
'$preferences[\'input-charset\'] index',
)
);
}
if ($hasOutputCharset !== 1) {
$phpcsFile->addError(
$error,
$firstNonEmpty,
'OutputPreferenceNotSet',
array(
'$preferences[\'output-charset\']',
'$preferences[\'output-charset\'] index',
)
);
}
return;
}
// The $preferences parameter was passed, but it was a variable/constant/output of a function call.
$phpcsFile->addWarning(
$error,
$firstNonEmpty,
'Undetermined',
array(
'$preferences[\'input/output-charset\']',
'$preferences[\'input-charset\'] and $preferences[\'output-charset\'] indexes',
)
);
}
}
@@ -0,0 +1,129 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* Detect negative string offsets as parameters passed to functions where this
* was not allowed prior to PHP 7.1.
*
* PHP version 7.1
*
* @link https://wiki.php.net/rfc/negative-string-offsets
*
* @since 9.0.0
*/
class NewNegativeStringOffsetSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 9.0.0
*
* @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.
*
* @since 9.0.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsBelow('7.0') === false);
}
/**
* Process the parameters of a matched function.
*
* @since 9.0.0
*
* @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,127 @@
<?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\ParameterValues;
use PHPCompatibility\Sniffs\ParameterValues\RemovedPCREModifiersSniff;
use PHP_CodeSniffer_File as File;
/**
* Check for the use of newly added regex modifiers for PCRE functions.
*
* Initially just checks for the PHP 7.2 new `J` modifier.
*
* PHP version 7.2+
*
* @link https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
* @link https://www.php.net/manual/en/migration72.new-features.php#migration72.new-features.pcre
*
* @since 8.2.0
* @since 9.0.0 Renamed from `PCRENewModifiersSniff` to `NewPCREModifiersSniff`.
*/
class NewPCREModifiersSniff extends RemovedPCREModifiersSniff
{
/**
* Functions to check for.
*
* @since 8.2.0
*
* @var array
*/
protected $targetFunctions = array(
'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.
*
* @since 8.2.0
*
* @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.
*
* @since 8.2.0
*
* @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.
*
* @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.
* @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,132 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* Check for valid values for the `$format` passed to `pack()`.
*
* PHP version 5.4+
*
* @link https://www.php.net/manual/en/function.pack.php#refsect1-function.pack-changelog
*
* @since 9.0.0
*/
class NewPackFormatSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 9.0.0
*
* @var array
*/
protected $targetFunctions = array(
'pack' => true,
);
/**
* List of new format character codes added to pack().
*
* @since 9.0.0
*
* @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.
*
* @since 9.0.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsBelow('7.1') === false);
}
/**
* Process the parameters of a matched function.
*
* @since 9.0.0
*
* @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,125 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* The constant value of the password hash algorithm constants has changed in PHP 7.4.
*
* Applications using the constants `PASSWORD_DEFAULT`, `PASSWORD_BCRYPT`,
* `PASSWORD_ARGON2I`, and `PASSWORD_ARGON2ID` will continue to function correctly.
* Using an integer will still work, but will produce a deprecation warning.
*
* PHP version 7.4
*
* @link https://www.php.net/manual/en/migration74.incompatible.php#migration74.incompatible.core.password-algorithm-constants
* @link https://wiki.php.net/rfc/password_registry
*
* @since 9.3.0
*/
class NewPasswordAlgoConstantValuesSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* Key is the function name, value the 1-based parameter position of
* the $algo parameter.
*
* @since 9.3.0
*
* @var array
*/
protected $targetFunctions = array(
'password_hash' => 2,
'password_needs_rehash' => 2,
);
/**
* Tokens types which indicate that the parameter passed is not the PHP native constant.
*
* @since 9.3.0
*
* @var array
*/
private $invalidTokenTypes = array(
\T_NULL => true,
\T_TRUE => true,
\T_FALSE => true,
\T_LNUMBER => true,
\T_DNUMBER => true,
\T_CONSTANT_ENCAPSED_STRING => true,
\T_DOUBLE_QUOTED_STRING => true,
\T_HEREDOC => true,
\T_NOWDOC => true,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @since 9.3.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsAbove('7.4') === false);
}
/**
* Process the parameters of a matched function.
*
* @since 9.3.0
*
* @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);
if (isset($parameters[$this->targetFunctions[$functionLC]]) === false) {
return;
}
$targetParam = $parameters[$this->targetFunctions[$functionLC]];
$tokens = $phpcsFile->getTokens();
for ($i = $targetParam['start']; $i <= $targetParam['end']; $i++) {
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
continue;
}
if (isset($this->invalidTokenTypes[$tokens[$i]['code']]) === true) {
$phpcsFile->addWarning(
'The value of the password hash algorithm constants has changed in PHP 7.4. Pass a PHP native constant to the %s() function instead of using the value of the constant. Found: %s',
$stackPtr,
'NotAlgoConstant',
array(
$functionName,
$targetParam['raw'],
)
);
break;
}
}
}
}
@@ -0,0 +1,136 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* As of PHP 7.4, `proc_open()` now also accepts an array instead of a string for the command.
*
* In that case, the process will be opened directly (without going through a shell) and
* PHP will take care of any necessary argument escaping.
*
* PHP version 7.4
*
* @link https://www.php.net/manual/en/migration74.new-features.php#migration74.new-features.standard.proc-open
* @link https://www.php.net/manual/en/function.proc-open.php
*
* @since 9.3.0
*/
class NewProcOpenCmdArraySniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 9.3.0
*
* @var array
*/
protected $targetFunctions = array(
'proc_open' => true,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @since 9.3.0
*
* @return bool
*/
protected function bowOutEarly()
{
return false;
}
/**
* Process the parameters of a matched function.
*
* @since 9.3.0
*
* @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];
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $targetParam['start'], $targetParam['end'], true);
if ($nextNonEmpty === false) {
// Shouldn't be possible.
return;
}
if ($tokens[$nextNonEmpty]['code'] !== \T_ARRAY
&& $tokens[$nextNonEmpty]['code'] !== \T_OPEN_SHORT_ARRAY
) {
// Not passed as an array.
return;
}
if ($this->supportsBelow('7.3') === true) {
$phpcsFile->addError(
'The proc_open() function did not accept $cmd to be passed in array format in PHP 7.3 and earlier.',
$nextNonEmpty,
'Found'
);
}
if ($this->supportsAbove('7.4') === true) {
if (strpos($targetParam['raw'], 'escapeshellarg(') === false) {
// Efficiency: prevent needlessly walking the array.
return;
}
$items = $this->getFunctionCallParameters($phpcsFile, $nextNonEmpty);
if (empty($items)) {
return;
}
foreach ($items as $item) {
for ($i = $item['start']; $i <= $item['end']; $i++) {
if ($tokens[$i]['code'] !== \T_STRING
|| $tokens[$i]['content'] !== 'escapeshellarg'
) {
continue;
}
// @todo Potential future enhancement: check if it's a call to the PHP native function.
$phpcsFile->addWarning(
'When passing proc_open() the $cmd parameter as an array, PHP will take care of any necessary argument escaping. Found: %s',
$i,
'Invalid',
array($item['raw'])
);
// Only throw one error per array item.
break;
}
}
}
}
}
@@ -0,0 +1,152 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* As of PHP 7.4, `strip_tags()` now also accepts an array of `$allowable_tags`.
*
* PHP version 7.4
*
* @link https://www.php.net/manual/en/migration74.new-features.php#migration74.new-features.standard.strip-tags
* @link https://www.php.net/manual/en/function.strip-tags.php
*
* @since 9.3.0
*/
class NewStripTagsAllowableTagsArraySniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 9.3.0
*
* @var array
*/
protected $targetFunctions = array(
'strip_tags' => true,
);
/**
* Text string tokens to examine.
*
* @since 9.3.0
*
* @var array
*/
private $textStringTokens = array(
\T_CONSTANT_ENCAPSED_STRING => true,
\T_DOUBLE_QUOTED_STRING => true,
\T_INLINE_HTML => true,
\T_HEREDOC => true,
\T_NOWDOC => true,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @since 9.3.0
*
* @return bool
*/
protected function bowOutEarly()
{
return false;
}
/**
* Process the parameters of a matched function.
*
* @since 9.3.0
*
* @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];
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $targetParam['start'], $targetParam['end'], true);
if ($nextNonEmpty === false) {
// Shouldn't be possible.
return;
}
if ($tokens[$nextNonEmpty]['code'] !== \T_ARRAY
&& $tokens[$nextNonEmpty]['code'] !== \T_OPEN_SHORT_ARRAY
) {
// Not passed as a hard-coded array.
return;
}
if ($this->supportsBelow('7.3') === true) {
$phpcsFile->addError(
'The strip_tags() function did not accept $allowable_tags to be passed in array format in PHP 7.3 and earlier.',
$nextNonEmpty,
'Found'
);
}
if ($this->supportsAbove('7.4') === true) {
if (strpos($targetParam['raw'], '>') === false) {
// Efficiency: prevent needlessly walking the array.
return;
}
$items = $this->getFunctionCallParameters($phpcsFile, $nextNonEmpty);
if (empty($items)) {
return;
}
foreach ($items as $item) {
for ($i = $item['start']; $i <= $item['end']; $i++) {
if ($tokens[$i]['code'] === \T_STRING
|| $tokens[$i]['code'] === \T_VARIABLE
) {
// Variable, constant, function call. Ignore complete item as undetermined.
break;
}
if (isset($this->textStringTokens[$tokens[$i]['code']]) === true
&& strpos($tokens[$i]['content'], '>') !== false
) {
$phpcsFile->addWarning(
'When passing strip_tags() the $allowable_tags parameter as an array, the tags should not be enclosed in <> brackets. Found: %s',
$i,
'Invalid',
array($item['raw'])
);
// Only throw one error per array item.
break;
}
}
}
}
}
}
@@ -0,0 +1,117 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractRemovedFeatureSniff;
use PHP_CodeSniffer_File as File;
/**
* Detect the use of deprecated and removed hash algorithms.
*
* PHP version 5.4
*
* @link https://www.php.net/manual/en/function.hash-algos.php#refsect1-function.hash-algos-changelog
*
* @since 5.5
* @since 7.1.0 Now extends the `AbstractRemovedFeatureSniff` instead of the base `Sniff` class.
*/
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.
*
* @since 7.0.7
*
* @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.
*
* @since 5.5
*
* @return array
*/
public function register()
{
return array(\T_STRING);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @since 5.5
*
* @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.
*
* @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->removedAlgorithms[$itemInfo['name']];
}
/**
* Get the error message template for this sniff.
*
* @since 7.1.0
*
* @return string
*/
protected function getErrorMsgTemplate()
{
return 'The %s hash algorithm is ';
}
}
@@ -0,0 +1,86 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* Detect passing deprecated `$type` values to `iconv_get_encoding()`.
*
* "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
*
* @link https://www.php.net/manual/en/migration56.deprecated.php#migration56.deprecated.iconv-mbstring-encoding
* @link https://wiki.php.net/rfc/default_encoding
*
* @since 9.0.0
*/
class RemovedIconvEncodingSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 9.0.0
*
* @var array
*/
protected $targetFunctions = array(
'iconv_set_encoding' => true,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @since 9.0.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsAbove('5.6') === false);
}
/**
* Process the parameters of a matched function.
*
* @since 9.0.0
*
* @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,323 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* Passing the `$glue` and `$pieces` parameters to `implode()` in reverse order has
* been deprecated in PHP 7.4.
*
* PHP version 7.4
*
* @link https://www.php.net/manual/en/migration74.deprecated.php#migration74.deprecated.core.implode-reverse-parameters
* @link https://wiki.php.net/rfc/deprecations_php_7_4#implode_parameter_order_mix
* @link https://php.net/manual/en/function.implode.php
*
* @since 9.3.0
*/
class RemovedImplodeFlexibleParamOrderSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 9.3.0
*
* @var array
*/
protected $targetFunctions = array(
'implode' => true,
'join' => true,
);
/**
* List of PHP native constants which should be recognized as text strings.
*
* @since 9.3.0
*
* @var array
*/
private $constantStrings = array(
'DIRECTORY_SEPARATOR' => true,
'PHP_EOL' => true,
);
/**
* List of PHP native functions which should be recognized as returning an array.
*
* Note: The array_*() functions will always be taken into account.
*
* @since 9.3.0
*
* @var array
*/
private $arrayFunctions = array(
'compact' => true,
'explode' => true,
'range' => true,
);
/**
* List of PHP native array functions which should *not* be recognized as returning an array.
*
* @since 9.3.0
*
* @var array
*/
private $arrayFunctionExceptions = array(
'array_key_exists' => true,
'array_key_first' => true,
'array_key_last' => true,
'array_multisort' => true,
'array_pop' => true,
'array_product' => true,
'array_push' => true,
'array_search' => true,
'array_shift' => true,
'array_sum' => true,
'array_unshift' => true,
'array_walk_recursive' => true,
'array_walk' => true,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @since 9.3.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsAbove('7.4') === false);
}
/**
* Process the parameters of a matched function.
*
* @since 9.3.0
*
* @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) {
// Only one parameter, this must be $pieces. Bow out.
return;
}
$tokens = $phpcsFile->getTokens();
/*
* Examine the first parameter.
* If there is any indication that this is an array declaration, we have an error.
*/
$targetParam = $parameters[1];
$start = $targetParam['start'];
$end = ($targetParam['end'] + 1);
$isOnlyText = true;
$firstNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $start, $end, true);
if ($firstNonEmpty === false) {
// Parse error. Shouldn't be possible.
return;
}
if ($tokens[$firstNonEmpty]['code'] === \T_OPEN_PARENTHESIS) {
$start = ($firstNonEmpty + 1);
$end = $tokens[$firstNonEmpty]['parenthesis_closer'];
}
$hasTernary = $phpcsFile->findNext(\T_INLINE_THEN, $start, $end);
if ($hasTernary !== false
&& isset($tokens[$start]['nested_parenthesis'], $tokens[$hasTernary]['nested_parenthesis'])
&& count($tokens[$start]['nested_parenthesis']) === count($tokens[$hasTernary]['nested_parenthesis'])
) {
$start = ($hasTernary + 1);
}
for ($i = $start; $i < $end; $i++) {
$tokenCode = $tokens[$i]['code'];
if (isset(Tokens::$emptyTokens[$tokenCode])) {
continue;
}
if ($tokenCode === \T_STRING && isset($this->constantStrings[$tokens[$i]['content']])) {
continue;
}
if ($hasTernary !== false && $tokenCode === \T_INLINE_ELSE) {
continue;
}
if (isset(Tokens::$stringTokens[$tokenCode]) === false) {
$isOnlyText = false;
}
if ($tokenCode === \T_ARRAY || $tokenCode === \T_OPEN_SHORT_ARRAY || $tokenCode === \T_ARRAY_CAST) {
$this->throwNotice($phpcsFile, $stackPtr, $functionName);
return;
}
if ($tokenCode === \T_STRING) {
/*
* Check for specific functions which return an array (i.e. $pieces).
*/
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), $end, true);
if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) {
continue;
}
$nameLc = strtolower($tokens[$i]['content']);
if (isset($this->arrayFunctions[$nameLc]) === false
&& (strpos($nameLc, 'array_') !== 0
|| isset($this->arrayFunctionExceptions[$nameLc]) === true)
) {
continue;
}
// Now make sure it's the PHP native function being called.
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), $start, true);
if ($tokens[$prevNonEmpty]['code'] === \T_DOUBLE_COLON
|| $tokens[$prevNonEmpty]['code'] === \T_OBJECT_OPERATOR
) {
// Method call, not a call to the PHP native function.
continue;
}
if ($tokens[$prevNonEmpty]['code'] === \T_NS_SEPARATOR
&& $tokens[$prevNonEmpty - 1]['code'] === \T_STRING
) {
// Namespaced function.
continue;
}
// Ok, so we know that there is an array function in the first param.
// 99.9% chance that this is $pieces, not $glue.
$this->throwNotice($phpcsFile, $stackPtr, $functionName);
return;
}
}
if ($isOnlyText === true) {
// First parameter only contained text string tokens, i.e. glue.
return;
}
/*
* Examine the second parameter.
*/
$targetParam = $parameters[2];
$start = $targetParam['start'];
$end = ($targetParam['end'] + 1);
$firstNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $start, $end, true);
if ($firstNonEmpty === false) {
// Parse error. Shouldn't be possible.
return;
}
if ($tokens[$firstNonEmpty]['code'] === \T_OPEN_PARENTHESIS) {
$start = ($firstNonEmpty + 1);
$end = $tokens[$firstNonEmpty]['parenthesis_closer'];
}
$hasTernary = $phpcsFile->findNext(\T_INLINE_THEN, $start, $end);
if ($hasTernary !== false
&& isset($tokens[$start]['nested_parenthesis'], $tokens[$hasTernary]['nested_parenthesis'])
&& count($tokens[$start]['nested_parenthesis']) === count($tokens[$hasTernary]['nested_parenthesis'])
) {
$start = ($hasTernary + 1);
}
for ($i = $start; $i < $end; $i++) {
$tokenCode = $tokens[$i]['code'];
if (isset(Tokens::$emptyTokens[$tokenCode])) {
continue;
}
if ($tokenCode === \T_ARRAY || $tokenCode === \T_OPEN_SHORT_ARRAY || $tokenCode === \T_ARRAY_CAST) {
// Found an array, $pieces is second.
return;
}
if ($tokenCode === \T_STRING && isset($this->constantStrings[$tokens[$i]['content']])) {
// One of the special cased, PHP native string constants found.
$this->throwNotice($phpcsFile, $stackPtr, $functionName);
return;
}
if ($tokenCode === \T_STRING || $tokenCode === \T_VARIABLE) {
// Function call, constant or variable encountered.
// No matter what this is combined with, we won't be able to reliably determine the value.
return;
}
if ($tokenCode === \T_CONSTANT_ENCAPSED_STRING
|| $tokenCode === \T_DOUBLE_QUOTED_STRING
|| $tokenCode === \T_HEREDOC
|| $tokenCode === \T_NOWDOC
) {
$this->throwNotice($phpcsFile, $stackPtr, $functionName);
return;
}
}
}
/**
* Throw the error/warning.
*
* @since 9.3.0
*
* @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.
*
* @return void
*/
protected function throwNotice(File $phpcsFile, $stackPtr, $functionName)
{
$message = 'Passing the $glue and $pieces parameters in reverse order to %s has been deprecated since PHP 7.4';
$isError = false;
$errorCode = 'Deprecated';
$data = array($functionName);
/*
Support for the deprecated behaviour is expected to be removed in PHP 8.0.
Once this has been implemented, this section should be uncommented.
if ($this->supportsAbove('8.0') === true) {
$message .= ' and is removed since PHP 8.0';
$isError = true;
$errorCode = 'Removed';
}
*/
$message .= '; $glue should be the first parameter and $pieces the second';
$this->addMessage($phpcsFile, $message, $stackPtr, $isError, $errorCode, $data);
}
}
@@ -0,0 +1,149 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* Detect passing `$encoding` to `mb_strrpos()` as 3rd argument.
*
* The `$encoding` parameter was moved from the third position to the fourth in PHP 5.2.0.
* For backward compatibility, `$encoding` could be specified as the third parameter, but doing
* so is deprecated and will be removed in the future.
*
* Between PHP 5.2 and PHP 7.3, this was a deprecation in documentation only.
* As of PHP 7.4, a deprecation warning will be thrown if an encoding is passed as the 3rd
* argument.
* As of PHP 8, the argument is expected to change to accept an integer only.
*
* PHP version 5.2
* PHP version 7.4
*
* @link https://www.php.net/manual/en/migration74.deprecated.php#migration74.deprecated.mbstring
* @link https://wiki.php.net/rfc/deprecations_php_7_4#mb_strrpos_with_encoding_as_3rd_argument
* @link https://www.php.net/manual/en/function.mb-strrpos.php
*
* @since 9.3.0
*/
class RemovedMbStrrposEncodingThirdParamSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 9.3.0
*
* @var array
*/
protected $targetFunctions = array(
'mb_strrpos' => true,
);
/**
* Tokens which should be recognized as text.
*
* @since 9.3.0
*
* @var array
*/
private $textStringTokens = array(
\T_CONSTANT_ENCAPSED_STRING,
\T_DOUBLE_QUOTED_STRING,
\T_HEREDOC,
\T_NOWDOC,
);
/**
* Tokens which should be recognized as numbers.
*
* @since 9.3.0
*
* @var array
*/
private $numberTokens = array(
\T_LNUMBER => \T_LNUMBER,
\T_DNUMBER => \T_DNUMBER,
\T_MINUS => \T_MINUS,
\T_PLUS => \T_PLUS,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @since 9.3.0
*
* @return bool
*/
protected function bowOutEarly()
{
return $this->supportsAbove('5.2') === false;
}
/**
* Process the parameters of a matched function.
*
* @since 9.3.0
*
* @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) {
// Optional third parameter not set.
return;
}
if (isset($parameters[4]) === true) {
// Encoding set as fourth parameter.
return;
}
$targetParam = $parameters[3];
$targets = $this->numberTokens + Tokens::$emptyTokens;
$nonNumber = $phpcsFile->findNext($targets, $targetParam['start'], ($targetParam['end'] + 1), true);
if ($nonNumber === false) {
return;
}
if ($this->isNumericCalculation($phpcsFile, $targetParam['start'], $targetParam['end']) === true) {
return;
}
$hasString = $phpcsFile->findNext($this->textStringTokens, $targetParam['start'], ($targetParam['end'] + 1));
if ($hasString === false) {
// No text strings found. Undetermined.
return;
}
$error = 'Passing the encoding to mb_strrpos() as third parameter is soft deprecated since PHP 5.2';
if ($this->supportsAbove('7.4') === true) {
$error .= ' and hard deprecated since PHP 7.4';
}
$error .= '. Use an explicit 0 as the offset in the third parameter.';
$phpcsFile->addWarning(
$error,
$targetParam['start'],
'Deprecated'
);
}
}
@@ -0,0 +1,135 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* Check for use of deprecated and removed regex modifiers for MbString regex functions.
*
* Initially just checks for the PHP 7.1 deprecated `e` modifier.
*
* PHP version 7.1
*
* @link https://wiki.php.net/rfc/deprecate_mb_ereg_replace_eval_option
* @link https://www.php.net/manual/en/function.mb-regex-set-options.php
*
* @since 7.0.5
* @since 7.0.8 This sniff now throws a warning instead of an error as the functionality is
* only deprecated (for now).
* @since 8.2.0 Now extends the `AbstractFunctionCallParameterSniff` instead of the base `Sniff` class.
* @since 9.0.0 Renamed from `MbstringReplaceEModifierSniff` to `RemovedMbstringModifiersSniff`.
*/
class RemovedMbstringModifiersSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* Key is the function name, value the parameter position of the options parameter.
*
* @since 7.0.5
* @since 8.2.0 Renamed from `$functions` to `$targetFunctions`.
*
* @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.
*
* @since 8.2.0
*
* @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.
*
* @since 7.0.5
* @since 8.2.0 Renamed from `process()` to `processParameters()` and removed
* logic superfluous now the sniff extends the abstract.
*
* @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,121 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* Detect usage of non-cryptographic hashes.
*
* "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
*
* @link https://www.php.net/manual/en/migration72.incompatible.php#migration72.incompatible.hash-functions
*
* @since 9.0.0
*/
class RemovedNonCryptoHashSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 9.0.0
*
* @var array
*/
protected $targetFunctions = array(
'hash_hmac' => true,
'hash_hmac_file' => true,
'hash_init' => true,
'hash_pbkdf2' => true,
);
/**
* List of the non-cryptographic hashes.
*
* @since 9.0.0
*
* @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.
*
* @since 9.0.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsAbove('7.2') === false);
}
/**
* Process the parameters of a matched function.
*
* @since 9.0.0
*
* @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,241 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* Check for the use of deprecated and removed regex modifiers for PCRE regex functions.
*
* Initially just checks for the `e` modifier, which was deprecated since PHP 5.5
* and removed as of PHP 7.0.
*
* {@internal If and when this sniff would need to start checking for other modifiers, a minor
* refactor will be needed as all references to the `e` modifier are currently hard-coded.}
*
* PHP version 5.5
* PHP version 7.0
*
* @link https://wiki.php.net/rfc/remove_preg_replace_eval_modifier
* @link https://wiki.php.net/rfc/remove_deprecated_functionality_in_php7
* @link https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
*
* @since 5.6
* @since 7.0.8 This sniff now throws a warning (deprecated) or an error (removed) depending
* on the `testVersion` set. Previously it would always throw an error.
* @since 8.2.0 Now extends the `AbstractFunctionCallParameterSniff` instead of the base `Sniff` class.
* @since 9.0.0 Renamed from `PregReplaceEModifierSniff` to `RemovedPCREModifiersSniff`.
*/
class RemovedPCREModifiersSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 7.0.1
* @since 8.2.0 Renamed from `$functions` to `$targetFunctions`.
*
* @var array
*/
protected $targetFunctions = array(
'preg_replace' => true,
'preg_filter' => true,
);
/**
* Regex bracket delimiters.
*
* @since 7.0.5 This array was originally contained within the `process()` method.
*
* @var array
*/
protected $doublesSeparators = array(
'{' => '}',
'[' => ']',
'(' => ')',
'<' => '>',
);
/**
* Process the parameters of a matched function.
*
* @since 5.6
* @since 8.2.0 Renamed from `process()` to `processParameters()` and removed
* logic superfluous now the sniff extends the abstract.
*
* @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.
*
* @since 8.2.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsAbove('5.5') === false);
}
/**
* Analyse a potential regex pattern for use of the /e modifier.
*
* @since 7.1.2 This logic was originally contained within the `process()` method.
*
* @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.
*
* @since 8.2.0 Split off from the `processRegexPattern()` method.
*
* @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,104 @@
<?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\ParameterValues;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHP_CodeSniffer_File as File;
/**
* Detect passing a string literal as `$category` to `setlocale()`.
*
* Support for the category parameter passed as a string has been removed.
* Only `LC_*` constants can be used as of PHP 7.0.0.
*
* PHP version 4.2
* PHP version 7.0
*
* @link https://wiki.php.net/rfc/remove_deprecated_functionality_in_php7
* @link https://www.php.net/manual/en/function.setlocale.php#refsect1-function.setlocale-changelog
*
* @since 9.0.0
*/
class RemovedSetlocaleStringSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 9.0.0
*
* @var array
*/
protected $targetFunctions = array(
'setlocale' => true,
);
/**
* Do a version check to determine if this sniff needs to run at all.
*
* @since 9.0.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsAbove('4.2') === false);
}
/**
* Process the parameters of a matched function.
*
* @since 9.0.0
*
* @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;
}
}
}