diff --git a/phpstan.neon b/phpstan.neon index 5316d19e84e..87ad57bc87d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -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 diff --git a/rules-tests/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector/Fixture/skip_not_inline_identical.php.inc b/rules-tests/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector/Fixture/skip_not_inline_identical.php.inc new file mode 100644 index 00000000000..47992f5321b --- /dev/null +++ b/rules-tests/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector/Fixture/skip_not_inline_identical.php.inc @@ -0,0 +1,15 @@ + $someVariable === 'c')) { + return true; + } + + return false; + } +} diff --git a/rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php b/rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php index 4d0361a4476..673b59e58e0 100644 --- a/rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php +++ b/rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php @@ -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( @@ -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'))); @@ -113,6 +108,30 @@ public function refactor(Node $node): ?FuncCall return new FuncCall(new Name('in_array'), $args); } + /** + * @return array + */ + 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 $identicalsOrEquals */ + return $identicalsOrEquals; + } + private function isEqualOrIdentical(Expr $expr): bool { if ($expr instanceof Identical) { @@ -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; }