> Key is the function name, value an array containing * the 1-based parameter position and the official name of the parameter. */ protected $targetFunctions = [ 'preg_replace' => [ 'position' => 1, 'name' => 'pattern', ], 'preg_filter' => [ 'position' => 1, 'name' => 'pattern', ], ]; /** * Do a version check to determine if this sniff needs to run at all. * * @since 8.2.0 * * @return bool */ protected function bowOutEarly() { return (ScannedCode::shouldRunOnOrAbove('5.5') === false); } /** * 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. * @since 10.0.0 Most logic has been moved to the new `PCRERegexTrait`. * * @param \PHP_CodeSniffer\Files\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); $paramInfo = $this->targetFunctions[$functionLC]; $targetParam = PassedParameters::getParameterFromStack($parameters, $paramInfo['position'], $paramInfo['name']); if ($targetParam === false) { return; } $patterns = $this->getRegexPatternsFromParameter($phpcsFile, $functionName, $targetParam); if (empty($patterns) === true) { return; } foreach ($patterns as $pattern) { $modifiers = $this->getRegexModifiers($phpcsFile, $pattern); if ($modifiers === '') { continue; } $this->examineModifiers($phpcsFile, $pattern['end'], $functionName, $modifiers); } } /** * Examine the regex modifier string. * * @since 8.2.0 Split off from the `processRegexPattern()` method. * * @param \PHP_CodeSniffer\Files\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 = [$functionName]; if (ScannedCode::shouldRunOnOrAbove('7.0') === true) { $error .= ' and removed since PHP 7.0'; $isError = true; $errorCode = 'Removed'; } MessageHelper::addMessage($phpcsFile, $error, $stackPtr, $isError, $errorCode, $data); } } }