I'm working on quite old project and I would like to do some small refactoring.
I have an if statement with condition like
if ($a == 1 || $a == 2 || b == 3)
and I want to replace it (or better write an inspection) with
if (in_array($a, [1, 2, 3]))
I'm able to write Structural replace for exact number of comparison
if ($VAR$ == $VALUE1$ || $VAR$ == $VALUE2$)
but is there a possibility to somehow involve repetion in the process? Sometimes there is one || operator, sometimes there are three or more...
The only solution which come to my mind is to make more intentions each for different number of operators. But I believe there is a better solution than that.
Try rolling up recursively. The idea would be to incrementally move a VALUE into the in_array call:
in_array($a, [1]) || $a == 2 || $a == 4
in_array($a, [1, 2]) || $a == 3 || $a == 4
in_array($a, [1, 2, 3]) || $a == 4
in_array($a, [1, 2, 3, 4])
So the search template would be:
in_array($a$, [$vals$]) || $a$ === $val$
(where Count for $vals$
= [0, Unlimited])
The replace template:
in_array($a$, [$vals$, $val$])
(You'll still have to write a search template for the base case)
Now, in the replace pane, repeatedly press Rerun (Ctrl+F5) and Replace All.