Updated Rector to commit 0227d24e36

0227d24e36 Merge InArrayAndArrayKeysToArrayKeyExistsRector to ArrayKeysAndInArrayToArrayKeyExistsRector with almost identical behavior (#2047)
This commit is contained in:
Tomas Votruba 2022-04-10 19:50:50 +00:00
parent 650661e16c
commit 931bfb66df
10 changed files with 76 additions and 180 deletions

View File

@ -31,7 +31,6 @@ use Rector\CodeQuality\Rector\FuncCall\ArrayMergeOfNonArraysToSimpleArrayRector;
use Rector\CodeQuality\Rector\FuncCall\CallUserFuncWithArrowFunctionToInlineRector;
use Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector;
use Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector;
use Rector\CodeQuality\Rector\FuncCall\InArrayAndArrayKeysToArrayKeyExistsRector;
use Rector\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector;
use Rector\CodeQuality\Rector\FuncCall\IsAWithStringWithThirdArgumentRector;
use Rector\CodeQuality\Rector\FuncCall\RemoveSoleValueSprintfRector;
@ -86,7 +85,6 @@ return static function (\Symfony\Component\DependencyInjection\Loader\Configurat
$services->set(\Rector\CodeQuality\Rector\BooleanNot\ReplaceMultipleBooleanNotRector::class);
$services->set(\Rector\CodeQuality\Rector\Foreach_\ForeachToInArrayRector::class);
$services->set(\Rector\CodeQuality\Rector\Foreach_\SimplifyForeachToCoalescingRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\InArrayAndArrayKeysToArrayKeyExistsRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\SimplifyFuncGetArgsCountRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\SimplifyInArrayValuesRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector::class);

View File

@ -1,4 +1,4 @@
# 510 Rules Overview
# 508 Rules Overview
<br>
@ -6,7 +6,7 @@
- [Arguments](#arguments) (5)
- [CodeQuality](#codequality) (72)
- [CodeQuality](#codequality) (70)
- [CodingStyle](#codingstyle) (35)
@ -354,14 +354,11 @@ Replace `array_keys()` and `in_array()` to `array_key_exists()`
- class: [`Rector\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector`](../rules/CodeQuality/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php)
```diff
class SomeClass
function run($packageName, $values)
{
public function run($packageName, $values)
{
- $keys = array_keys($values);
- return in_array($packageName, $keys, true);
+ return array_key_exists($packageName, $values);
}
- $keys = array_keys($values);
- return in_array($packageName, $keys, true);
+ return array_key_exists($packageName, $values);
}
```
@ -855,19 +852,6 @@ Changes comparison with get_class to instanceof
<br>
### InArrayAndArrayKeysToArrayKeyExistsRector
Simplify `in_array` and `array_keys` functions combination into `array_key_exists` when `array_keys` has one argument only
- class: [`Rector\CodeQuality\Rector\FuncCall\InArrayAndArrayKeysToArrayKeyExistsRector`](../rules/CodeQuality/Rector/FuncCall/InArrayAndArrayKeysToArrayKeyExistsRector.php)
```diff
-in_array("key", array_keys($array), true);
+array_key_exists("key", $array);
```
<br>
### InlineConstructorDefaultToPropertyRector
Move property default from constructor to property default
@ -1243,26 +1227,6 @@ Simplify negated conditions with de Morgan theorem
<br>
### SimplifyDuplicatedTernaryRector
Remove ternary that duplicated return value of true : false
- class: [`Rector\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector`](../rules/CodeQuality/Rector/Ternary/SimplifyDuplicatedTernaryRector.php)
```diff
class SomeClass
{
public function run(bool $value, string $name)
{
- $isTrue = $value ? true : false;
+ $isTrue = $value;
$isName = $name ? true : false;
}
}
```
<br>
### SimplifyEmptyArrayCheckRector
Simplify `is_array` and `empty` functions combination into a simple identical check for an empty array
@ -1641,7 +1605,7 @@ When throwing into a catch block, checks that the previous exception is passed t
### UnnecessaryTernaryExpressionRector
Remove unnecessary ternary expressions.
Remove unnecessary ternary expressions
- class: [`Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector`](../rules/CodeQuality/Rector/Ternary/UnnecessaryTernaryExpressionRector.php)

View File

@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\FunctionLike;
@ -20,22 +21,16 @@ final class ArrayKeysAndInArrayToArrayKeyExistsRector extends \Rector\Core\Recto
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Replace array_keys() and in_array() to array_key_exists()', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
class SomeClass
function run($packageName, $values)
{
public function run($packageName, $values)
{
$keys = array_keys($values);
return in_array($packageName, $keys, true);
}
$keys = array_keys($values);
return in_array($packageName, $keys, true);
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
function run($packageName, $values)
{
public function run($packageName, $values)
{
return array_key_exists($packageName, $values);
}
return array_key_exists($packageName, $values);
}
CODE_SAMPLE
)]);
@ -55,39 +50,28 @@ CODE_SAMPLE
if (!$this->nodeNameResolver->isName($node, 'in_array')) {
return null;
}
if (!isset($node->args[1])) {
return null;
$args = $node->getArgs();
$secondArg = $args[1];
$arrayVariable = $secondArg->value;
$previousAssignArraysKeysFuncCall = $this->findPreviousAssignToArrayKeys($node, $arrayVariable);
if ($previousAssignArraysKeysFuncCall instanceof \PhpParser\Node\Expr\Assign) {
/** @var FuncCall $arrayKeysFuncCall */
$arrayKeysFuncCall = $previousAssignArraysKeysFuncCall->expr;
$this->removeNode($previousAssignArraysKeysFuncCall);
return $this->createArrayKeyExists($node, $arrayKeysFuncCall);
}
if (!$node->args[1] instanceof \PhpParser\Node\Arg) {
return null;
if ($arrayVariable instanceof \PhpParser\Node\Expr\FuncCall && $this->isName($arrayVariable, 'array_keys')) {
$arrayKeysFuncCallArgs = $arrayVariable->getArgs();
if (\count($arrayKeysFuncCallArgs) > 1) {
return null;
}
// unwrap array func call
$secondArg->value = $arrayKeysFuncCallArgs[0]->value;
$node->name = new \PhpParser\Node\Name('array_key_exists');
unset($node->args[2]);
return $node;
}
$arrayVariable = $node->args[1]->value;
/** @var Assign|Node|null $previousAssignArraysKeysFuncCall */
$previousAssignArraysKeysFuncCall = $this->betterNodeFinder->findFirstPreviousOfNode($node, function (\PhpParser\Node $node) use($arrayVariable) : bool {
// breaking out of scope
if ($node instanceof \PhpParser\Node\FunctionLike) {
return \true;
}
if (!$node instanceof \PhpParser\Node\Expr\Assign) {
return !(bool) $this->betterNodeFinder->find($node, function (\PhpParser\Node $n) use($arrayVariable) : bool {
return $this->nodeComparator->areNodesEqual($arrayVariable, $n);
});
}
if (!$this->nodeComparator->areNodesEqual($arrayVariable, $node->var)) {
return \false;
}
if (!$node->expr instanceof \PhpParser\Node\Expr\FuncCall) {
return \false;
}
return $this->nodeNameResolver->isName($node->expr, 'array_keys');
});
if (!$previousAssignArraysKeysFuncCall instanceof \PhpParser\Node\Expr\Assign) {
return null;
}
/** @var FuncCall $arrayKeysFuncCall */
$arrayKeysFuncCall = $previousAssignArraysKeysFuncCall->expr;
$this->removeNode($previousAssignArraysKeysFuncCall);
return $this->createArrayKeyExists($node, $arrayKeysFuncCall);
return null;
}
private function createArrayKeyExists(\PhpParser\Node\Expr\FuncCall $inArrayFuncCall, \PhpParser\Node\Expr\FuncCall $arrayKeysFuncCall) : ?\PhpParser\Node\Expr\FuncCall
{
@ -106,4 +90,28 @@ CODE_SAMPLE
$arguments = [$inArrayFuncCall->args[0], $arrayKeysFuncCall->args[0]];
return new \PhpParser\Node\Expr\FuncCall(new \PhpParser\Node\Name('array_key_exists'), $arguments);
}
/**
* @return \PhpParser\Node|\PhpParser\Node\FunctionLike|null
*/
private function findPreviousAssignToArrayKeys(\PhpParser\Node\Expr\FuncCall $funcCall, \PhpParser\Node\Expr $expr)
{
return $this->betterNodeFinder->findFirstPreviousOfNode($funcCall, function (\PhpParser\Node $node) use($expr) : bool {
// breaking out of scope
if ($node instanceof \PhpParser\Node\FunctionLike) {
return \true;
}
if (!$node instanceof \PhpParser\Node\Expr\Assign) {
return !(bool) $this->betterNodeFinder->find($node, function (\PhpParser\Node $n) use($expr) : bool {
return $this->nodeComparator->areNodesEqual($expr, $n);
});
}
if (!$this->nodeComparator->areNodesEqual($expr, $node->var)) {
return \false;
}
if (!$node->expr instanceof \PhpParser\Node\Expr\FuncCall) {
return \false;
}
return $this->nodeNameResolver->isName($node->expr, 'array_keys');
});
}
}

View File

@ -1,72 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\CodeQuality\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\FuncCall\InArrayAndArrayKeysToArrayKeyExistsRector\InArrayAndArrayKeysToArrayKeyExistsRectorTest
*/
final class InArrayAndArrayKeysToArrayKeyExistsRector extends \Rector\Core\Rector\AbstractRector
{
/**
* @readonly
* @var \Rector\Core\NodeAnalyzer\ArgsAnalyzer
*/
private $argsAnalyzer;
public function __construct(\Rector\Core\NodeAnalyzer\ArgsAnalyzer $argsAnalyzer)
{
$this->argsAnalyzer = $argsAnalyzer;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Simplify `in_array` and `array_keys` functions combination into `array_key_exists` when `array_keys` has one argument only', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample('in_array("key", array_keys($array), true);', 'array_key_exists("key", $array);')]);
}
/**
* @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
{
if (!$this->isName($node, 'in_array')) {
return null;
}
if (!$this->argsAnalyzer->isArgsInstanceInArgsPositions($node->args, [0, 1])) {
return null;
}
/** @var Arg $secondArgument */
$secondArgument = $node->args[1];
if (!$secondArgument->value instanceof \PhpParser\Node\Expr\FuncCall) {
return null;
}
if (!$this->isName($secondArgument->value, 'array_keys')) {
return null;
}
if (\count($secondArgument->value->args) > 1) {
return null;
}
/** @var Arg $keyArg */
$keyArg = $node->args[0];
/** @var Arg $arrayArg */
$arrayArg = $node->args[1];
/** @var FuncCall $innerFuncCallNode */
$innerFuncCallNode = $arrayArg->value;
$arrayArg = $innerFuncCallNode->args[0];
$node->name = new \PhpParser\Node\Name('array_key_exists');
$node->args = [$keyArg, $arrayArg];
return $node;
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '9b8e16187827bd33c853414e20da7a3b4fa8ce89';
public const PACKAGE_VERSION = '0227d24e3636af762e8f800997f0e86fcddce07e';
/**
* @var string
*/
public const RELEASE_DATE = '2022-04-10 19:01:30';
public const RELEASE_DATE = '2022-04-10 19:42:40';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20220410\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

2
vendor/autoload.php vendored
View File

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

View File

@ -1505,7 +1505,6 @@ return array(
'Rector\\CodeQuality\\Rector\\FuncCall\\CallUserFuncWithArrowFunctionToInlineRector' => $baseDir . '/rules/CodeQuality/Rector/FuncCall/CallUserFuncWithArrowFunctionToInlineRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\ChangeArrayPushToArrayAssignRector' => $baseDir . '/rules/CodeQuality/Rector/FuncCall/ChangeArrayPushToArrayAssignRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\CompactToVariablesRector' => $baseDir . '/rules/CodeQuality/Rector/FuncCall/CompactToVariablesRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\InArrayAndArrayKeysToArrayKeyExistsRector' => $baseDir . '/rules/CodeQuality/Rector/FuncCall/InArrayAndArrayKeysToArrayKeyExistsRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\IntvalToTypeCastRector' => $baseDir . '/rules/CodeQuality/Rector/FuncCall/IntvalToTypeCastRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\IsAWithStringWithThirdArgumentRector' => $baseDir . '/rules/CodeQuality/Rector/FuncCall/IsAWithStringWithThirdArgumentRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\RemoveSoleValueSprintfRector' => $baseDir . '/rules/CodeQuality/Rector/FuncCall/RemoveSoleValueSprintfRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitcecd92aed85fb3253143779dc263824e
class ComposerAutoloaderInitcadec8a1bb1db7ed826d29c6d9d16a64
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInitcecd92aed85fb3253143779dc263824e
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitcecd92aed85fb3253143779dc263824e', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitcadec8a1bb1db7ed826d29c6d9d16a64', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitcecd92aed85fb3253143779dc263824e', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitcadec8a1bb1db7ed826d29c6d9d16a64', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitcecd92aed85fb3253143779dc263824e::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitcadec8a1bb1db7ed826d29c6d9d16a64::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInitcecd92aed85fb3253143779dc263824e::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInitcadec8a1bb1db7ed826d29c6d9d16a64::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequirececd92aed85fb3253143779dc263824e($fileIdentifier, $file);
composerRequirecadec8a1bb1db7ed826d29c6d9d16a64($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInitcecd92aed85fb3253143779dc263824e
* @param string $file
* @return void
*/
function composerRequirececd92aed85fb3253143779dc263824e($fileIdentifier, $file)
function composerRequirecadec8a1bb1db7ed826d29c6d9d16a64($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 ComposerStaticInitcecd92aed85fb3253143779dc263824e
class ComposerStaticInitcadec8a1bb1db7ed826d29c6d9d16a64
{
public static $files = array (
'320cde22f66dd4f5d3fd621d3e88b98f' => __DIR__ . '/..' . '/symfony/polyfill-ctype/bootstrap.php',
@ -1874,7 +1874,6 @@ class ComposerStaticInitcecd92aed85fb3253143779dc263824e
'Rector\\CodeQuality\\Rector\\FuncCall\\CallUserFuncWithArrowFunctionToInlineRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FuncCall/CallUserFuncWithArrowFunctionToInlineRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\ChangeArrayPushToArrayAssignRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FuncCall/ChangeArrayPushToArrayAssignRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\CompactToVariablesRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FuncCall/CompactToVariablesRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\InArrayAndArrayKeysToArrayKeyExistsRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FuncCall/InArrayAndArrayKeysToArrayKeyExistsRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\IntvalToTypeCastRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FuncCall/IntvalToTypeCastRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\IsAWithStringWithThirdArgumentRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FuncCall/IsAWithStringWithThirdArgumentRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\RemoveSoleValueSprintfRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FuncCall/RemoveSoleValueSprintfRector.php',
@ -3858,9 +3857,9 @@ class ComposerStaticInitcecd92aed85fb3253143779dc263824e
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitcecd92aed85fb3253143779dc263824e::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitcecd92aed85fb3253143779dc263824e::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitcecd92aed85fb3253143779dc263824e::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitcadec8a1bb1db7ed826d29c6d9d16a64::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitcadec8a1bb1db7ed826d29c6d9d16a64::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitcadec8a1bb1db7ed826d29c6d9d16a64::$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('RectorPrefix20220410\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInitcecd92aed85fb3253143779dc263824e', false) && !interface_exists('ComposerAutoloaderInitcecd92aed85fb3253143779dc263824e', false) && !trait_exists('ComposerAutoloaderInitcecd92aed85fb3253143779dc263824e', false)) {
spl_autoload_call('RectorPrefix20220410\ComposerAutoloaderInitcecd92aed85fb3253143779dc263824e');
if (!class_exists('ComposerAutoloaderInitcadec8a1bb1db7ed826d29c6d9d16a64', false) && !interface_exists('ComposerAutoloaderInitcadec8a1bb1db7ed826d29c6d9d16a64', false) && !trait_exists('ComposerAutoloaderInitcadec8a1bb1db7ed826d29c6d9d16a64', false)) {
spl_autoload_call('RectorPrefix20220410\ComposerAutoloaderInitcadec8a1bb1db7ed826d29c6d9d16a64');
}
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('RectorPrefix20220410\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -59,9 +59,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20220410\print_node(...func_get_args());
}
}
if (!function_exists('composerRequirececd92aed85fb3253143779dc263824e')) {
function composerRequirececd92aed85fb3253143779dc263824e() {
return \RectorPrefix20220410\composerRequirececd92aed85fb3253143779dc263824e(...func_get_args());
if (!function_exists('composerRequirecadec8a1bb1db7ed826d29c6d9d16a64')) {
function composerRequirecadec8a1bb1db7ed826d29c6d9d16a64() {
return \RectorPrefix20220410\composerRequirecadec8a1bb1db7ed826d29c6d9d16a64(...func_get_args());
}
}
if (!function_exists('scanPath')) {