From c541dfeccd1d1c035e2e27aaf929f6f2583c00da Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 3 Oct 2025 07:00:20 +0700 Subject: [PATCH 1/3] [CodeQuality] Skip not inline comparison identical on RepeatedOrEqualToInArrayRector --- .../Fixture/skip_not_inline_identical.php.inc | 15 ++++++++++ .../RepeatedOrEqualToInArrayRector.php | 28 +++++++++++++------ 2 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector/Fixture/skip_not_inline_identical.php.inc 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..2767186c782 100644 --- a/rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php +++ b/rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php @@ -14,7 +14,6 @@ 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; @@ -24,11 +23,6 @@ */ final class RepeatedOrEqualToInArrayRector extends AbstractRector { - public function __construct( - private readonly BetterNodeFinder $betterNodeFinder, - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -103,8 +97,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 +107,24 @@ public function refactor(Node $node): ?FuncCall return new FuncCall(new Name('in_array'), $args); } + private function findIdenticalsOrEquals(BooleanOr $booleanOr, string $type = Identical::class): array + { + $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; + } + + return $identicalsOrEquals; + } + private function isEqualOrIdentical(Expr $expr): bool { if ($expr instanceof Identical) { From eac3d48c9cb71142b0070a8b392a3fa57b35ed71 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 3 Oct 2025 07:07:36 +0700 Subject: [PATCH 2/3] fix --- phpstan.neon | 11 ----------- .../BooleanOr/RepeatedOrEqualToInArrayRector.php | 13 +++++++++++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 5316d19e84e..756eba937d9 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -345,14 +345,3 @@ 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 - - - '#Method Rector\\(.*?)Rector\:\:refactor\(\) never returns \d so it can be removed from the return type#' diff --git a/rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php b/rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php index 2767186c782..673b59e58e0 100644 --- a/rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php +++ b/rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php @@ -17,6 +17,7 @@ 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 @@ -107,8 +108,13 @@ public function refactor(Node $node): ?FuncCall return new FuncCall(new Name('in_array'), $args); } - private function findIdenticalsOrEquals(BooleanOr $booleanOr, string $type = Identical::class): array + /** + * @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; @@ -122,6 +128,7 @@ private function findIdenticalsOrEquals(BooleanOr $booleanOr, string $type = Ide $identicalsOrEquals[] = $booleanOr->right; } + /** @var array $identicalsOrEquals */ return $identicalsOrEquals; } @@ -172,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; } From d0a3ea685485e2e4ef88b98bea73d647c08485ae Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 3 Oct 2025 07:11:42 +0700 Subject: [PATCH 3/3] fix --- phpstan.neon | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index 756eba937d9..87ad57bc87d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -345,3 +345,9 @@ parameters: - identifier: symplify.noReference message: '#Use explicit return value over magic &reference#' + + - + path: rules/Renaming/Rector/Name/RenameClassRector.php + identifier: return.type + + - '#Method Rector\\(.*?)Rector\:\:refactor\(\) never returns \d so it can be removed from the return type#'