Updated Rector to commit b0936667c8b0c707cbdbd63875c75335fb4941fc

b0936667c8 [DowngradePhp56] Add DowngradeArrayFilterUseConstantRector (#1573)
This commit is contained in:
Tomas Votruba 2021-12-27 19:30:54 +00:00
parent 27cabcdf40
commit fc5610bdbd
8 changed files with 213 additions and 19 deletions

View File

@ -6,6 +6,7 @@ namespace RectorPrefix20211227;
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DowngradePhp56\Rector\CallLike\DowngradeArgumentUnpackingRector;
use Rector\DowngradePhp56\Rector\FuncCall\DowngradeArrayFilterUseConstantRector;
use Rector\DowngradePhp56\Rector\Pow\DowngradeExponentialAssignmentOperatorRector;
use Rector\DowngradePhp56\Rector\Pow\DowngradeExponentialOperatorRector;
use Rector\DowngradePhp56\Rector\Use_\DowngradeUseFunctionRector;
@ -18,4 +19,5 @@ return static function (\Symfony\Component\DependencyInjection\Loader\Configurat
$services->set(\Rector\DowngradePhp56\Rector\Use_\DowngradeUseFunctionRector::class);
$services->set(\Rector\DowngradePhp56\Rector\Pow\DowngradeExponentialAssignmentOperatorRector::class);
$services->set(\Rector\DowngradePhp56\Rector\Pow\DowngradeExponentialOperatorRector::class);
$services->set(\Rector\DowngradePhp56\Rector\FuncCall\DowngradeArrayFilterUseConstantRector::class);
};

View File

@ -0,0 +1,190 @@
<?php
declare (strict_types=1);
namespace Rector\DowngradePhp56\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\Naming\VariableNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use RectorPrefix20211227\Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://www.php.net/manual/en/function.array-filter.php
*
* @see \Rector\Tests\DowngradePhp56\Rector\FuncCall\DowngradeArrayFilterUseConstantRector\DowngradeArrayFilterUseConstantRectorTest
*/
final class DowngradeArrayFilterUseConstantRector extends \Rector\Core\Rector\AbstractRector
{
/**
* @readonly
* @var \Rector\Naming\Naming\VariableNaming
*/
private $variableNaming;
/**
* @readonly
* @var \Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser
*/
private $simpleCallableNodeTraverser;
public function __construct(\Rector\Naming\Naming\VariableNaming $variableNaming, \RectorPrefix20211227\Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser $simpleCallableNodeTraverser)
{
$this->variableNaming = $variableNaming;
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Replace use ARRAY_FILTER_USE_BOTH and ARRAY_FILTER_USE_KEY to loop to filter it', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
$arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
var_dump(array_filter($arr, function($v, $k) {
return $k == 'b' || $v == 4;
}, ARRAY_FILTER_USE_BOTH));
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
$result = [];
foreach ($arr as $k => $v) {
if ($v === 4 || $k === 'b') {
$result[$k] = $v;
}
}
var_dump($result);
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Expr\FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
$args = $node->getArgs();
if ($this->shouldSkip($node, $args)) {
return null;
}
if ($args[1]->value instanceof \PhpParser\Node\Expr\Closure) {
return $this->processClosure($node, $args);
}
return null;
}
/**
* @param Arg[] $args
*/
private function processClosure(\PhpParser\Node\Expr\FuncCall $funcCall, array $args) : ?\PhpParser\Node\Expr\Variable
{
/** @var Closure $closure */
$closure = $args[1]->value;
/** @var Return_[] $returns */
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($closure, \PhpParser\Node\Stmt\Return_::class);
if ($returns === []) {
return null;
}
$currentStatement = $funcCall->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CURRENT_STATEMENT);
if (!$currentStatement instanceof \PhpParser\Node\Stmt) {
return null;
}
$scope = $funcCall->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
$variable = new \PhpParser\Node\Expr\Variable($this->variableNaming->createCountedValueName('result', $scope));
$this->nodesToAddCollector->addNodeBeforeNode(new \PhpParser\Node\Stmt\Expression(new \PhpParser\Node\Expr\Assign($variable, new \PhpParser\Node\Expr\Array_([]))), $currentStatement);
/** @var ConstFetch $constant */
$constant = $args[2]->value;
$foreach = $this->nodeNameResolver->isName($constant, 'ARRAY_FILTER_USE_KEY') ? $this->applyArrayFilterUseKey($args, $closure, $variable) : $this->applyArrayFilterUseBoth($args, $closure, $variable);
$this->addNodeBeforeNode($foreach, $currentStatement);
return $variable;
}
/**
* @param Arg[] $args
*/
private function applyArrayFilterUseBoth(array $args, \PhpParser\Node\Expr\Closure $closure, \PhpParser\Node\Expr\Variable $variable) : \PhpParser\Node\Stmt\Foreach_
{
$arrayValue = $args[0]->value;
$value = $closure->params[0]->var;
$key = $closure->params[1]->var;
$foreach = new \PhpParser\Node\Stmt\Foreach_($arrayValue, $value, ['keyVar' => $key]);
$stmts = [];
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($closure->stmts, function (\PhpParser\Node $subNode) use($variable, $key, $value, &$stmts) : void {
if (!$subNode instanceof \PhpParser\Node\Stmt) {
return;
}
if (!$subNode instanceof \PhpParser\Node\Stmt\Return_) {
$stmts[] = $subNode;
return;
}
if (!$subNode->expr instanceof \PhpParser\Node\Expr) {
$stmts[] = $subNode;
return;
}
$assign = new \PhpParser\Node\Expr\Assign(new \PhpParser\Node\Expr\ArrayDimFetch($variable, $key), $value);
$stmts[] = new \PhpParser\Node\Stmt\If_($subNode->expr, ['stmts' => [new \PhpParser\Node\Stmt\Expression($assign)]]);
});
$foreach->stmts = $stmts;
return $foreach;
}
/**
* @param Arg[] $args
*/
private function applyArrayFilterUseKey(array $args, \PhpParser\Node\Expr\Closure $closure, \PhpParser\Node\Expr\Variable $variable) : \PhpParser\Node\Stmt\Foreach_
{
$arrayValue = $args[0]->value;
$funcCall = $this->nodeFactory->createFuncCall('array_keys', [$arrayValue]);
$key = $closure->params[0]->var;
$foreach = new \PhpParser\Node\Stmt\Foreach_($funcCall, $key);
$stmts = [];
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($closure->stmts, function (\PhpParser\Node $subNode) use($variable, $key, $arrayValue, &$stmts) : void {
if (!$subNode instanceof \PhpParser\Node\Stmt) {
return;
}
if (!$subNode instanceof \PhpParser\Node\Stmt\Return_) {
$stmts[] = $subNode;
return;
}
if (!$subNode->expr instanceof \PhpParser\Node\Expr) {
$stmts[] = $subNode;
return;
}
$assign = new \PhpParser\Node\Expr\Assign(new \PhpParser\Node\Expr\ArrayDimFetch($variable, $key), new \PhpParser\Node\Expr\ArrayDimFetch($arrayValue, $key));
$stmts[] = new \PhpParser\Node\Stmt\If_($subNode->expr, ['stmts' => [new \PhpParser\Node\Stmt\Expression($assign)]]);
});
$foreach->stmts = $stmts;
return $foreach;
}
/**
* @param Arg[] $args
*/
private function shouldSkip(\PhpParser\Node\Expr\FuncCall $funcCall, array $args) : bool
{
if (!$this->nodeNameResolver->isName($funcCall, 'array_filter')) {
return \true;
}
if (!isset($args[2])) {
return \true;
}
if (!$args[2]->value instanceof \PhpParser\Node\Expr\ConstFetch) {
return \true;
}
return !$this->nodeNameResolver->isNames($args[2]->value, ['ARRAY_FILTER_USE_KEY', 'ARRAY_FILTER_USE_BOTH']);
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '8c6a966741ca3f75c0e1f563b8c951f073c323be';
public const PACKAGE_VERSION = 'b0936667c8b0c707cbdbd63875c75335fb4941fc';
/**
* @var string
*/
public const RELEASE_DATE = '2021-12-27 18:56:17';
public const RELEASE_DATE = '2021-12-27 19:20:37';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20211227\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

2
vendor/autoload.php vendored
View File

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInitf8dbcf00b01e8df797998594abb63b67::getLoader();
return ComposerAutoloaderInit8e23b701e8bfefd5a659805800e5badc::getLoader();

View File

@ -1959,6 +1959,7 @@ return array(
'Rector\\DowngradePhp56\\NodeManipulator\\ArgManipulator' => $baseDir . '/rules/DowngradePhp56/NodeManipulator/ArgManipulator.php',
'Rector\\DowngradePhp56\\NodeManipulator\\UnpackedArgList' => $baseDir . '/rules/DowngradePhp56/NodeManipulator/UnpackedArgList.php',
'Rector\\DowngradePhp56\\Rector\\CallLike\\DowngradeArgumentUnpackingRector' => $baseDir . '/rules/DowngradePhp56/Rector/CallLike/DowngradeArgumentUnpackingRector.php',
'Rector\\DowngradePhp56\\Rector\\FuncCall\\DowngradeArrayFilterUseConstantRector' => $baseDir . '/rules/DowngradePhp56/Rector/FuncCall/DowngradeArrayFilterUseConstantRector.php',
'Rector\\DowngradePhp56\\Rector\\Pow\\DowngradeExponentialAssignmentOperatorRector' => $baseDir . '/rules/DowngradePhp56/Rector/Pow/DowngradeExponentialAssignmentOperatorRector.php',
'Rector\\DowngradePhp56\\Rector\\Pow\\DowngradeExponentialOperatorRector' => $baseDir . '/rules/DowngradePhp56/Rector/Pow/DowngradeExponentialOperatorRector.php',
'Rector\\DowngradePhp56\\Rector\\Use_\\DowngradeUseFunctionRector' => $baseDir . '/rules/DowngradePhp56/Rector/Use_/DowngradeUseFunctionRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitf8dbcf00b01e8df797998594abb63b67
class ComposerAutoloaderInit8e23b701e8bfefd5a659805800e5badc
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInitf8dbcf00b01e8df797998594abb63b67
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitf8dbcf00b01e8df797998594abb63b67', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit8e23b701e8bfefd5a659805800e5badc', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInitf8dbcf00b01e8df797998594abb63b67', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit8e23b701e8bfefd5a659805800e5badc', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitf8dbcf00b01e8df797998594abb63b67::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit8e23b701e8bfefd5a659805800e5badc::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,12 +42,12 @@ class ComposerAutoloaderInitf8dbcf00b01e8df797998594abb63b67
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInitf8dbcf00b01e8df797998594abb63b67::$files;
$includeFiles = Composer\Autoload\ComposerStaticInit8e23b701e8bfefd5a659805800e5badc::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequiref8dbcf00b01e8df797998594abb63b67($fileIdentifier, $file);
composerRequire8e23b701e8bfefd5a659805800e5badc($fileIdentifier, $file);
}
return $loader;
@ -59,7 +59,7 @@ class ComposerAutoloaderInitf8dbcf00b01e8df797998594abb63b67
* @param string $file
* @return void
*/
function composerRequiref8dbcf00b01e8df797998594abb63b67($fileIdentifier, $file)
function composerRequire8e23b701e8bfefd5a659805800e5badc($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 ComposerStaticInitf8dbcf00b01e8df797998594abb63b67
class ComposerStaticInit8e23b701e8bfefd5a659805800e5badc
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -2354,6 +2354,7 @@ class ComposerStaticInitf8dbcf00b01e8df797998594abb63b67
'Rector\\DowngradePhp56\\NodeManipulator\\ArgManipulator' => __DIR__ . '/../..' . '/rules/DowngradePhp56/NodeManipulator/ArgManipulator.php',
'Rector\\DowngradePhp56\\NodeManipulator\\UnpackedArgList' => __DIR__ . '/../..' . '/rules/DowngradePhp56/NodeManipulator/UnpackedArgList.php',
'Rector\\DowngradePhp56\\Rector\\CallLike\\DowngradeArgumentUnpackingRector' => __DIR__ . '/../..' . '/rules/DowngradePhp56/Rector/CallLike/DowngradeArgumentUnpackingRector.php',
'Rector\\DowngradePhp56\\Rector\\FuncCall\\DowngradeArrayFilterUseConstantRector' => __DIR__ . '/../..' . '/rules/DowngradePhp56/Rector/FuncCall/DowngradeArrayFilterUseConstantRector.php',
'Rector\\DowngradePhp56\\Rector\\Pow\\DowngradeExponentialAssignmentOperatorRector' => __DIR__ . '/../..' . '/rules/DowngradePhp56/Rector/Pow/DowngradeExponentialAssignmentOperatorRector.php',
'Rector\\DowngradePhp56\\Rector\\Pow\\DowngradeExponentialOperatorRector' => __DIR__ . '/../..' . '/rules/DowngradePhp56/Rector/Pow/DowngradeExponentialOperatorRector.php',
'Rector\\DowngradePhp56\\Rector\\Use_\\DowngradeUseFunctionRector' => __DIR__ . '/../..' . '/rules/DowngradePhp56/Rector/Use_/DowngradeUseFunctionRector.php',
@ -3842,9 +3843,9 @@ class ComposerStaticInitf8dbcf00b01e8df797998594abb63b67
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitf8dbcf00b01e8df797998594abb63b67::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitf8dbcf00b01e8df797998594abb63b67::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitf8dbcf00b01e8df797998594abb63b67::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit8e23b701e8bfefd5a659805800e5badc::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit8e23b701e8bfefd5a659805800e5badc::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit8e23b701e8bfefd5a659805800e5badc::$classMap;
}, null, ClassLoader::class);
}

View File

@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20211227\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInitf8dbcf00b01e8df797998594abb63b67', false) && !interface_exists('ComposerAutoloaderInitf8dbcf00b01e8df797998594abb63b67', false) && !trait_exists('ComposerAutoloaderInitf8dbcf00b01e8df797998594abb63b67', false)) {
spl_autoload_call('RectorPrefix20211227\ComposerAutoloaderInitf8dbcf00b01e8df797998594abb63b67');
if (!class_exists('ComposerAutoloaderInit8e23b701e8bfefd5a659805800e5badc', false) && !interface_exists('ComposerAutoloaderInit8e23b701e8bfefd5a659805800e5badc', false) && !trait_exists('ComposerAutoloaderInit8e23b701e8bfefd5a659805800e5badc', false)) {
spl_autoload_call('RectorPrefix20211227\ComposerAutoloaderInit8e23b701e8bfefd5a659805800e5badc');
}
if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) {
spl_autoload_call('RectorPrefix20211227\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -78,9 +78,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20211227\print_node(...func_get_args());
}
}
if (!function_exists('composerRequiref8dbcf00b01e8df797998594abb63b67')) {
function composerRequiref8dbcf00b01e8df797998594abb63b67() {
return \RectorPrefix20211227\composerRequiref8dbcf00b01e8df797998594abb63b67(...func_get_args());
if (!function_exists('composerRequire8e23b701e8bfefd5a659805800e5badc')) {
function composerRequire8e23b701e8bfefd5a659805800e5badc() {
return \RectorPrefix20211227\composerRequire8e23b701e8bfefd5a659805800e5badc(...func_get_args());
}
}
if (!function_exists('scanPath')) {