Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,6 @@ parameters:
identifier: symplify.noReference
message: '#Use explicit return value over magic &reference#'

# known type
-
identifier: argument.type
message: '#Parameter \#1 \$expr of method Rector\\CodeQuality\\Rector\\BooleanOr\\RepeatedOrEqualToInArrayRector\:\:matchComparedExprAndValueExpr\(\) expects PhpParser\\Node\\Expr\\BinaryOp\\Equal\|PhpParser\\Node\\Expr\\BinaryOp\\Identical, PhpParser\\Node\\Expr given#'

-
path: rules/Renaming/Rector/Name/RenameClassRector.php
identifier: return.type
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\BooleanOr\RepeatedOrEqualToInArrayRector\Fixture;

final class SkipNotInlineIdentical
{
public function demo($someVariable): bool
{
if ($someVariable === 'a' || $someVariable === 'b' || execute(fn(): bool => $someVariable === 'c')) {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,16 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\CodeQuality\ValueObject\ComparedExprAndValueExpr;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\CodeQuality\Rector\BooleanOr\RepeatedOrEqualToInArrayRector\RepeatedOrEqualToInArrayRectorTest
*/
final class RepeatedOrEqualToInArrayRector extends AbstractRector
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -103,8 +98,8 @@ public function refactor(Node $node): ?FuncCall

$args = $this->nodeFactory->createArgs([$firstComparedExprAndValue->getComparedExpr(), $array]);

$identicals = $this->betterNodeFinder->findInstanceOf($node, Identical::class);
$equals = $this->betterNodeFinder->findInstanceOf($node, Equal::class);
$identicals = $this->findIdenticalsOrEquals($node, Identical::class);
$equals = $this->findIdenticalsOrEquals($node, Equal::class);

if ($identicals !== [] && $equals === []) {
$args[] = new Arg(new ConstFetch(new Name('true')));
Expand All @@ -113,6 +108,30 @@ public function refactor(Node $node): ?FuncCall
return new FuncCall(new Name('in_array'), $args);
}

/**
* @return array<Identical|Equal>
*/
private function findIdenticalsOrEquals(BooleanOr $booleanOr, string $type): array
{
Assert::oneOf($type, [Identical::class, Equal::class]);

$identicalsOrEquals = [];
if ($booleanOr->left instanceof $type) {
$identicalsOrEquals[] = $booleanOr->left;
}

if ($booleanOr->right instanceof BooleanOr) {
$identicalsOrEquals[] = $this->findIdenticalsOrEquals($booleanOr->right, $type);
}

if ($booleanOr->right instanceof $type) {
$identicalsOrEquals[] = $booleanOr->right;
}

/** @var array<Identical|Equal> $identicalsOrEquals */
return $identicalsOrEquals;
}

private function isEqualOrIdentical(Expr $expr): bool
{
if ($expr instanceof Identical) {
Expand Down Expand Up @@ -160,7 +179,9 @@ private function matchComparedAndDesiredValues(BooleanOr $booleanOr): ?array
return null;
}

$comparedExprAndValueExprs[] = $this->matchComparedExprAndValueExpr($currentBooleanOr->left->right);
/** @var Identical|Equal $leftRight */
$leftRight = $currentBooleanOr->left->right;
$comparedExprAndValueExprs[] = $this->matchComparedExprAndValueExpr($leftRight);
$currentBooleanOr = $currentBooleanOr->left;
}

Expand Down