Updated Rector to commit 16647397cc155594371858f316bd82e9dd436117

16647397cc [Php55] Handle crash on curly parentheses delimiter on PregReplaceEModifierRector (#3144)
This commit is contained in:
Tomas Votruba 2022-12-03 09:23:44 +00:00
parent 091bb20142
commit df7c74ec60
7 changed files with 68 additions and 25 deletions

View File

@ -7,8 +7,6 @@ use PhpParser\Node\ComplexType;
use PhpParser\Node\Name; use PhpParser\Node\Name;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\HasMethodType; use PHPStan\Type\Accessory\HasMethodType;
use PHPStan\Type\ConditionalType; use PHPStan\Type\ConditionalType;
use PHPStan\Type\Type; use PHPStan\Type\Type;

View File

@ -20,6 +20,11 @@ final class RegexMatcher
* @see https://regex101.com/r/2NWVwT/1 * @see https://regex101.com/r/2NWVwT/1
*/ */
private const LETTER_SUFFIX_REGEX = '#(?<modifiers>\\w+)$#'; private const LETTER_SUFFIX_REGEX = '#(?<modifiers>\\w+)$#';
/**
* @var string[]
* @see https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
*/
private const ALL_MODIFIERS_VALUES = ['i', 'm', 's', 'x', 'e', 'A', 'D', 'S', 'U', 'X', 'J', 'u'];
/** /**
* @readonly * @readonly
* @var \Rector\Core\PhpParser\Node\Value\ValueResolver * @var \Rector\Core\PhpParser\Node\Value\ValueResolver
@ -40,12 +45,26 @@ final class RegexMatcher
return null; return null;
} }
$delimiter = $pattern[0]; $delimiter = $pattern[0];
/** @var string $modifiers */ switch ($delimiter) {
$modifiers = Strings::after($pattern, $delimiter, -1); case '(':
if (\strpos($modifiers, 'e') === \false) { $delimiter = ')';
return null; break;
case '{':
$delimiter = '}';
break;
case '[':
$delimiter = ']';
break;
case '<':
$delimiter = '>';
break;
default:
$delimiter = $delimiter;
break;
} }
if (\in_array($pattern[\strlen($pattern) - 1], [')', '}', ']', '>'], \true)) { /** @var string $modifiers */
$modifiers = $this->resolveModifiers((string) Strings::after($pattern, $delimiter, -1));
if (\strpos($modifiers, 'e') === \false) {
return null; return null;
} }
$patternWithoutE = $this->createPatternWithoutE($pattern, $delimiter, $modifiers); $patternWithoutE = $this->createPatternWithoutE($pattern, $delimiter, $modifiers);
@ -56,6 +75,18 @@ final class RegexMatcher
} }
return null; return null;
} }
private function resolveModifiers(string $modifiersCandidate) : string
{
$modifiers = '';
for ($modifierIndex = 0; $modifierIndex < \strlen($modifiersCandidate); ++$modifierIndex) {
if (!\in_array($modifiersCandidate[$modifierIndex], self::ALL_MODIFIERS_VALUES, \true)) {
$modifiers = '';
continue;
}
$modifiers .= $modifiersCandidate[$modifierIndex];
}
return $modifiers;
}
private function createPatternWithoutE(string $pattern, string $delimiter, string $modifiers) : string private function createPatternWithoutE(string $pattern, string $delimiter, string $modifiers) : string
{ {
$modifiersWithoutE = Strings::replace($modifiers, '#e#', ''); $modifiersWithoutE = Strings::replace($modifiers, '#e#', '');

View File

@ -17,12 +17,12 @@ final class VersionResolver
* @api * @api
* @var string * @var string
*/ */
public const PACKAGE_VERSION = '139c19cbdf666ce3df7dea712faaf84772f19051'; public const PACKAGE_VERSION = '16647397cc155594371858f316bd82e9dd436117';
/** /**
* @api * @api
* @var string * @var string
*/ */
public const RELEASE_DATE = '2022-12-03 06:56:33'; public const RELEASE_DATE = '2022-12-03 10:18:24';
/** /**
* @var int * @var int
*/ */

View File

@ -11,6 +11,7 @@ use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\String_; use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt; use PhpParser\Node\Stmt;
use Rector\Core\Contract\PhpParser\NodePrinterInterface; use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\Core\Util\StringUtils; use Rector\Core\Util\StringUtils;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator; use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
final class InlineCodeParser final class InlineCodeParser
@ -60,11 +61,17 @@ final class InlineCodeParser
* @var \Rector\Core\PhpParser\Parser\SimplePhpParser * @var \Rector\Core\PhpParser\Parser\SimplePhpParser
*/ */
private $simplePhpParser; private $simplePhpParser;
public function __construct(NodePrinterInterface $nodePrinter, NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator, \Rector\Core\PhpParser\Parser\SimplePhpParser $simplePhpParser) /**
* @readonly
* @var \Rector\Core\PhpParser\Node\Value\ValueResolver
*/
private $valueResolver;
public function __construct(NodePrinterInterface $nodePrinter, NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator, \Rector\Core\PhpParser\Parser\SimplePhpParser $simplePhpParser, ValueResolver $valueResolver)
{ {
$this->nodePrinter = $nodePrinter; $this->nodePrinter = $nodePrinter;
$this->nodeScopeAndMetadataDecorator = $nodeScopeAndMetadataDecorator; $this->nodeScopeAndMetadataDecorator = $nodeScopeAndMetadataDecorator;
$this->simplePhpParser = $simplePhpParser; $this->simplePhpParser = $simplePhpParser;
$this->valueResolver = $valueResolver;
} }
/** /**
* @return Stmt[] * @return Stmt[]
@ -93,11 +100,18 @@ final class InlineCodeParser
} }
if ($expr instanceof Encapsed) { if ($expr instanceof Encapsed) {
// remove " // remove "
$expr = \trim($this->nodePrinter->print($expr), '""'); $printedExpr = \trim($this->nodePrinter->print($expr), '""');
/**
* Encapsed "$eval_links" is printed as {$eval_links} use its value when possible
*/
if (\strncmp($printedExpr, '{', \strlen('{')) === 0 && \substr_compare($printedExpr, '}', -\strlen('}')) === 0 && \count($expr->parts) === 1) {
$currentPart = \current($expr->parts);
$printedExpr = (string) $this->valueResolver->getValue($currentPart);
}
// use \$ → $ // use \$ → $
$expr = Strings::replace($expr, self::PRESLASHED_DOLLAR_REGEX, '$'); $printedExpr = Strings::replace($printedExpr, self::PRESLASHED_DOLLAR_REGEX, '$');
// use \'{$...}\' → $... // use \'{$...}\' → $...
return Strings::replace($expr, self::CURLY_BRACKET_WRAPPER_REGEX, '$1'); return Strings::replace($printedExpr, self::CURLY_BRACKET_WRAPPER_REGEX, '$1');
} }
if ($expr instanceof Concat) { if ($expr instanceof Concat) {
$string = $this->stringify($expr->left) . $this->stringify($expr->right); $string = $this->stringify($expr->left) . $this->stringify($expr->right);

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php'; require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit11fda1c87505eda717f408f0caeff073::getLoader(); return ComposerAutoloaderInitfb3cd2e4ffdaf03a65f03a570a71d9ba::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer // autoload_real.php @generated by Composer
class ComposerAutoloaderInit11fda1c87505eda717f408f0caeff073 class ComposerAutoloaderInitfb3cd2e4ffdaf03a65f03a570a71d9ba
{ {
private static $loader; private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit11fda1c87505eda717f408f0caeff073
return self::$loader; return self::$loader;
} }
spl_autoload_register(array('ComposerAutoloaderInit11fda1c87505eda717f408f0caeff073', 'loadClassLoader'), true, true); spl_autoload_register(array('ComposerAutoloaderInitfb3cd2e4ffdaf03a65f03a570a71d9ba', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit11fda1c87505eda717f408f0caeff073', 'loadClassLoader')); spl_autoload_unregister(array('ComposerAutoloaderInitfb3cd2e4ffdaf03a65f03a570a71d9ba', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php'; require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit11fda1c87505eda717f408f0caeff073::getInitializer($loader)); call_user_func(\Composer\Autoload\ComposerStaticInitfb3cd2e4ffdaf03a65f03a570a71d9ba::getInitializer($loader));
$loader->setClassMapAuthoritative(true); $loader->setClassMapAuthoritative(true);
$loader->register(true); $loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit11fda1c87505eda717f408f0caeff073::$files; $includeFiles = \Composer\Autoload\ComposerStaticInitfb3cd2e4ffdaf03a65f03a570a71d9ba::$files;
foreach ($includeFiles as $fileIdentifier => $file) { foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire11fda1c87505eda717f408f0caeff073($fileIdentifier, $file); composerRequirefb3cd2e4ffdaf03a65f03a570a71d9ba($fileIdentifier, $file);
} }
return $loader; return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit11fda1c87505eda717f408f0caeff073
* @param string $file * @param string $file
* @return void * @return void
*/ */
function composerRequire11fda1c87505eda717f408f0caeff073($fileIdentifier, $file) function composerRequirefb3cd2e4ffdaf03a65f03a570a71d9ba($fileIdentifier, $file)
{ {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload; namespace Composer\Autoload;
class ComposerStaticInit11fda1c87505eda717f408f0caeff073 class ComposerStaticInitfb3cd2e4ffdaf03a65f03a570a71d9ba
{ {
public static $files = array ( public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3023,9 +3023,9 @@ class ComposerStaticInit11fda1c87505eda717f408f0caeff073
public static function getInitializer(ClassLoader $loader) public static function getInitializer(ClassLoader $loader)
{ {
return \Closure::bind(function () use ($loader) { return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit11fda1c87505eda717f408f0caeff073::$prefixLengthsPsr4; $loader->prefixLengthsPsr4 = ComposerStaticInitfb3cd2e4ffdaf03a65f03a570a71d9ba::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit11fda1c87505eda717f408f0caeff073::$prefixDirsPsr4; $loader->prefixDirsPsr4 = ComposerStaticInitfb3cd2e4ffdaf03a65f03a570a71d9ba::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit11fda1c87505eda717f408f0caeff073::$classMap; $loader->classMap = ComposerStaticInitfb3cd2e4ffdaf03a65f03a570a71d9ba::$classMap;
}, null, ClassLoader::class); }, null, ClassLoader::class);
} }