Updated Rector to commit d8e8a34a606b1e0b0a1a674a4e008822bbedf624

d8e8a34a60 Refactor PARENT_NODE away from EregToPregMatchRector (#3973)
This commit is contained in:
Tomas Votruba 2023-05-26 13:00:21 +00:00
parent af4dc685fb
commit 7587e17e7b
6 changed files with 61 additions and 51 deletions

View File

@ -9,14 +9,12 @@ use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
@ -75,7 +73,8 @@ CODE_SAMPLE
return $this->processMysqliSelectDb($assign, $funcCall);
}
if ($this->isName($funcCall, 'mysql_fetch_field')) {
return $this->processMysqlFetchField($assign, $funcCall);
$this->processMysqlFetchField($funcCall);
return $node;
}
if ($this->isName($funcCall, 'mysql_result')) {
return $this->processMysqlResult($assign, $funcCall);
@ -114,10 +113,10 @@ CODE_SAMPLE
unset($funcCall->args[1]);
return [new Expression($funcCall), new Expression($mysqliQueryAssign)];
}
private function processMysqlFetchField(Assign $assign, FuncCall $funcCall) : Assign
private function processMysqlFetchField(FuncCall $funcCall) : void
{
$funcCall->name = isset($funcCall->args[1]) ? new Name('mysqli_fetch_field_direct') : new Name('mysqli_fetch_field');
return $assign;
$hasExactField = isset($funcCall->args[1]);
$funcCall->name = new Name($hasExactField ? 'mysqli_fetch_field_direct' : 'mysqli_fetch_field');
}
/**
* @return Stmt[]
@ -142,14 +141,6 @@ CODE_SAMPLE
if (!$this->isName($funcCall, $funcName)) {
continue;
}
// @todo remove
$parentNode = $funcCall->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof PropertyFetch) {
continue;
}
if ($parentNode instanceof StaticPropertyFetch) {
continue;
}
$funcCall->name = new Name('mysqli_fetch_field_direct');
$assign->expr = new PropertyFetch($funcCall, $property);
return $assign;

View File

@ -5,6 +5,7 @@ namespace Rector\Php70\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\Concat;
@ -16,7 +17,6 @@ use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php70\EregToPcreTransformer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -54,37 +54,25 @@ final class EregToPregMatchRector extends AbstractRector implements MinPhpVersio
*/
public function getNodeTypes() : array
{
return [FuncCall::class];
return [FuncCall::class, Assign::class];
}
/**
* @param FuncCall $node
* @param FuncCall|Assign $node
*/
public function refactor(Node $node) : ?Node
{
if ($this->shouldSkip($node)) {
if ($node instanceof FuncCall) {
return $this->refactorFuncCall($node);
}
if (!$this->isEregFuncCallWithThreeArgs($node->expr)) {
return null;
}
/** @var string $functionName */
$functionName = $this->getName($node);
$firstArg = $node->getArgs()[0];
$patternNode = $firstArg->value;
if ($patternNode instanceof String_) {
$this->processStringPattern($node, $patternNode, $functionName);
} elseif ($patternNode instanceof Variable) {
$this->processVariablePattern($node, $patternNode, $functionName);
}
$this->processSplitLimitArgument($node, $functionName);
$node->name = new Name(self::OLD_NAMES_TO_NEW_ONES[$functionName]);
// ereg|eregi 3rd argument return value fix
if (\in_array($functionName, ['ereg', 'eregi'], \true) && isset($node->args[2]) && $node->args[2] instanceof Arg) {
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Assign) {
return $this->createTernaryWithStrlenOfFirstMatch($node);
}
}
/** @var FuncCall $funcCall */
$funcCall = $node->expr;
$node->expr = $this->createTernaryWithStrlenOfFirstMatch($funcCall);
return $node;
}
private function shouldSkip(FuncCall $funcCall) : bool
private function shouldSkipFuncCall(FuncCall $funcCall) : bool
{
$functionName = $this->getName($funcCall);
if ($functionName === null) {
@ -142,8 +130,7 @@ final class EregToPregMatchRector extends AbstractRector implements MinPhpVersio
}
private function createTernaryWithStrlenOfFirstMatch(FuncCall $funcCall) : Ternary
{
/** @var Arg $thirdArg */
$thirdArg = $funcCall->args[2];
$thirdArg = $funcCall->getArgs()[2];
$arrayDimFetch = new ArrayDimFetch($thirdArg->value, new LNumber(0));
$strlenFuncCall = $this->nodeFactory->createFuncCall('strlen', [$arrayDimFetch]);
return new Ternary($funcCall, $strlenFuncCall, $this->nodeFactory->createFalse());
@ -155,4 +142,36 @@ final class EregToPregMatchRector extends AbstractRector implements MinPhpVersio
}
return \strpos($functionName, 'spliti') !== \false;
}
private function isEregFuncCallWithThreeArgs(Expr $expr) : bool
{
if (!$expr instanceof FuncCall) {
return \false;
}
$functionName = $this->getName($expr);
if (!\is_string($functionName)) {
return \false;
}
if (!\in_array($functionName, ['ereg', 'eregi'], \true)) {
return \false;
}
return isset($expr->getArgs()[2]);
}
private function refactorFuncCall(FuncCall $funcCall) : ?FuncCall
{
if ($this->shouldSkipFuncCall($funcCall)) {
return null;
}
/** @var string $functionName */
$functionName = $this->getName($funcCall);
$firstArg = $funcCall->getArgs()[0];
$patternExpr = $firstArg->value;
if ($patternExpr instanceof String_) {
$this->processStringPattern($funcCall, $patternExpr, $functionName);
} elseif ($patternExpr instanceof Variable) {
$this->processVariablePattern($funcCall, $patternExpr, $functionName);
}
$this->processSplitLimitArgument($funcCall, $functionName);
$funcCall->name = new Name(self::OLD_NAMES_TO_NEW_ONES[$functionName]);
return $funcCall;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '14fc01359ed4b27798ac64532cb128ab0ad2f32e';
public const PACKAGE_VERSION = 'd8e8a34a606b1e0b0a1a674a4e008822bbedf624';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-26 13:40:03';
public const RELEASE_DATE = '2023-05-26 12:56:35';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

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

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitbcf52e7c41c3b91219804d359c6ee397
class ComposerAutoloaderInit98e810b456ea26931d98fef847b3cf87
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInitbcf52e7c41c3b91219804d359c6ee397
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitbcf52e7c41c3b91219804d359c6ee397', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit98e810b456ea26931d98fef847b3cf87', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitbcf52e7c41c3b91219804d359c6ee397', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit98e810b456ea26931d98fef847b3cf87', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitbcf52e7c41c3b91219804d359c6ee397::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit98e810b456ea26931d98fef847b3cf87::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInitbcf52e7c41c3b91219804d359c6ee397::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit98e810b456ea26931d98fef847b3cf87::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

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