From 5c76ed94ebeabd1755891f81579c41ba49a8f83e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 14 Oct 2026 20:14:35 +0200 Subject: [PATCH] [CodeQuality] Skip constant expression in RepeatedOrEqualToInArrayRector and RepeatedAndNotEqualToNotInArrayRector --- .../Fixture/skip_constant_expression.php.inc | 16 ++++++++++++++++ .../Fixture/skip_constant_expression.php.inc | 16 ++++++++++++++++ .../RepeatedAndNotEqualToNotInArrayRector.php | 19 +++++++++++++++++++ .../RepeatedOrEqualToInArrayRector.php | 19 +++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/BooleanAnd/RepeatedAndNotEqualToNotInArrayRector/Fixture/skip_constant_expression.php.inc create mode 100644 rules-tests/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector/Fixture/skip_constant_expression.php.inc diff --git a/rules-tests/CodeQuality/Rector/BooleanAnd/RepeatedAndNotEqualToNotInArrayRector/Fixture/skip_constant_expression.php.inc b/rules-tests/CodeQuality/Rector/BooleanAnd/RepeatedAndNotEqualToNotInArrayRector/Fixture/skip_constant_expression.php.inc new file mode 100644 index 00000000000..616dfeebfc6 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/BooleanAnd/RepeatedAndNotEqualToNotInArrayRector/Fixture/skip_constant_expression.php.inc @@ -0,0 +1,16 @@ +isInConstantExpression($node)) { + return null; + } + if (! $this->isNotEqualOrNotIdentical($node->right)) { return null; } @@ -96,6 +102,19 @@ public function refactor(Node $node): ?BooleanNot return new BooleanNot($inArrayFuncCall); } + private function isInConstantExpression(BooleanAnd $booleanAnd): bool + { + if ($booleanAnd->getAttribute(AttributeKey::IS_DEFAULT_PROPERTY_VALUE) === true) { + return true; + } + + if ($booleanAnd->getAttribute(AttributeKey::IS_CLASS_CONST_VALUE) === true) { + return true; + } + + return $booleanAnd->getAttribute(AttributeKey::IS_PARAM_DEFAULT) === true; + } + private function isNotEqualOrNotIdentical(Expr $expr): bool { if ($expr instanceof NotIdentical) { diff --git a/rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php b/rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php index b757b90cb82..f49fa8fab74 100644 --- a/rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php +++ b/rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php @@ -15,6 +15,7 @@ use PhpParser\Node\Name; use Rector\CodeQuality\NodeFactory\InArrayFromRepeatedCompareFactory; use Rector\CodeQuality\ValueObject\ComparedExprAndValueExpr; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; @@ -67,6 +68,11 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?FuncCall { + // in_array() func call is not allowed in a constant expression + if ($this->isInConstantExpression($node)) { + return null; + } + if (! $this->isEqualOrIdentical($node->right)) { return null; } @@ -102,6 +108,19 @@ public function refactor(Node $node): ?FuncCall return new FuncCall(new Name('in_array'), $args); } + private function isInConstantExpression(BooleanOr $booleanOr): bool + { + if ($booleanOr->getAttribute(AttributeKey::IS_DEFAULT_PROPERTY_VALUE) === true) { + return true; + } + + if ($booleanOr->getAttribute(AttributeKey::IS_CLASS_CONST_VALUE) === true) { + return true; + } + + return $booleanOr->getAttribute(AttributeKey::IS_PARAM_DEFAULT) === true; + } + private function isEqualOrIdentical(Expr $expr): bool { if ($expr instanceof Identical) {