Skip to content

Commit

Permalink
Merge pull request #1396 from PHPCompatibility/feature/1239-newpcremo…
Browse files Browse the repository at this point in the history
…difiers-support-named-params

NewPCREModifiers: add support for named parameters
  • Loading branch information
wimg committed Oct 31, 2022
2 parents 13296a6 + c240d1d commit 58d2225
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
53 changes: 41 additions & 12 deletions PHPCompatibility/Sniffs/ParameterValues/NewPCREModifiersSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHPCompatibility\Helpers\PCRERegexTrait;
use PHP_CodeSniffer\Files\File;
use PHPCSUtils\Utils\PassedParameters;

/**
* Check for the use of newly added regex modifiers for PCRE functions.
Expand All @@ -36,18 +37,44 @@ class NewPCREModifiersSniff extends AbstractFunctionCallParameterSniff
* Functions to check for.
*
* @since 8.2.0
* @since 10.0.0 Value changed from an irrelevant value to an array.
*
* @var array
* @var array Key is the function name, value an array containing the 1-based parameter position
* and the official name of the parameter.
*/
protected $targetFunctions = [
'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,
'preg_filter' => [
'position' => 1,
'name' => 'pattern',
],
'preg_grep' => [
'position' => 1,
'name' => 'pattern',
],
'preg_match_all' => [
'position' => 1,
'name' => 'pattern',
],
'preg_match' => [
'position' => 1,
'name' => 'pattern',
],
'preg_replace_callback_array' => [
'position' => 1,
'name' => 'pattern',
],
'preg_replace_callback' => [
'position' => 1,
'name' => 'pattern',
],
'preg_replace' => [
'position' => 1,
'name' => 'pattern',
],
'preg_split' => [
'position' => 1,
'name' => 'pattern',
],
];

/**
Expand Down Expand Up @@ -96,12 +123,14 @@ protected function bowOutEarly()
*/
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) {
$functionLC = \strtolower($functionName);
$paramInfo = $this->targetFunctions[$functionLC];
$targetParam = PassedParameters::getParameterFromStack($parameters, $paramInfo['position'], $paramInfo['name']);
if ($targetParam === false) {
return;
}

$patterns = $this->getRegexPatternsFromParameter($phpcsFile, $functionName, $parameters[1]);
$patterns = $this->getRegexPatternsFromParameter($phpcsFile, $functionName, $targetParam);
if (empty($patterns) === true) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ EOD
'[$1]',
$text
);

// Safeguard support for PHP 8 named parameters.
preg_grep(array: $input, pattern: '#some text#i'); // OK.
preg_grep(array: $input, pattern: '#some text#Ji', flags: $flags); // Error.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testPCRENewModifier($modifier, $lastVersionBefore, $lines, $okVe
public function dataPCRENewModifier()
{
return [
['J', '7.1', [3, 4, 10, 17, 19, 25, 43], '7.2'],
['J', '7.1', [3, 4, 10, 17, 19, 25, 43, 50], '7.2'],
];
}

Expand Down Expand Up @@ -97,6 +97,7 @@ public function dataNoFalsePositives()
return [
[18],
[28],
[49],
];
}

Expand Down

0 comments on commit 58d2225

Please sign in to comment.