Updated Rector to commit 5f86e6bb082d06c1fc7f3ca53a9e42389319c3eb

5f86e6bb08  [CodeQuality] Add negated support on SingleInArrayToCompareRector  (#5205)
This commit is contained in:
Tomas Votruba 2023-10-28 06:44:30 +00:00
parent 59b8839a32
commit a3cb68c58c
2 changed files with 40 additions and 15 deletions

View File

@ -9,6 +9,9 @@ use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\FuncCall;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -49,30 +52,48 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [FuncCall::class];
return [BooleanNot::class, FuncCall::class];
}
/**
* @param FuncCall $node
* @param BooleanNot|FuncCall $node
*/
public function refactor(Node $node) : ?Node
{
if (!$this->isName($node, 'in_array')) {
if ($node instanceof BooleanNot) {
if (!$node->expr instanceof FuncCall) {
return null;
}
$firstArrayItem = $this->resolveArrayItem($node->expr);
if (!$firstArrayItem instanceof ArrayItem) {
return null;
}
return $this->processCompare($firstArrayItem, $node->expr, \true);
}
$firstArrayItem = $this->resolveArrayItem($node);
if (!$firstArrayItem instanceof ArrayItem) {
return null;
}
if ($node->isFirstClassCallable()) {
return $this->processCompare($firstArrayItem, $node);
}
private function resolveArrayItem(FuncCall $funcCall) : ?ArrayItem
{
if (!$this->isName($funcCall, 'in_array')) {
return null;
}
if (!isset($node->args[1])) {
if ($funcCall->isFirstClassCallable()) {
return null;
}
if (!$node->args[1] instanceof Arg) {
if (!isset($funcCall->args[1])) {
return null;
}
if (!$node->args[1]->value instanceof Array_) {
if (!$funcCall->args[1] instanceof Arg) {
return null;
}
if (!$funcCall->args[1]->value instanceof Array_) {
return null;
}
/** @var Array_ $arrayNode */
$arrayNode = $node->args[1]->value;
$arrayNode = $funcCall->args[1]->value;
if (\count($arrayNode->items) !== 1) {
return null;
}
@ -83,15 +104,19 @@ CODE_SAMPLE
if ($firstArrayItem->unpack) {
return null;
}
if (!isset($node->getArgs()[0])) {
if (!isset($funcCall->getArgs()[0])) {
return null;
}
return $firstArrayItem;
}
private function processCompare(ArrayItem $firstArrayItem, FuncCall $funcCall, bool $isNegated = \false) : Node
{
$firstArrayItemValue = $firstArrayItem->value;
$firstArg = $node->getArgs()[0];
$firstArg = $funcCall->getArgs()[0];
// strict
if (isset($node->getArgs()[2])) {
return new Identical($firstArg->value, $firstArrayItemValue);
if (isset($funcCall->getArgs()[2])) {
return $isNegated ? new NotIdentical($firstArg->value, $firstArrayItemValue) : new Identical($firstArg->value, $firstArrayItemValue);
}
return new Equal($firstArg->value, $firstArrayItemValue);
return $isNegated ? new NotEqual($firstArg->value, $firstArrayItemValue) : new Equal($firstArg->value, $firstArrayItemValue);
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '662f53cbf88a58ccb31bbf119118df6afc32afdc';
public const PACKAGE_VERSION = '5f86e6bb082d06c1fc7f3ca53a9e42389319c3eb';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-10-27 17:42:00';
public const RELEASE_DATE = '2023-10-28 13:41:09';
/**
* @var int
*/