A for loop computes a count or length again in every iteration
What it finds
for ($i = 0; $i < count($items); $i++): the condition runs before every iteration, so the count is computed again each time. Computing it once before the loop, or using foreach, does the work once.
From the rule's source, count_in_loop_condition.rs.
<?phpfunction walk($items){ for ($i = 0; $i < count($items); $i++) {tx0 reports: count runs again before every iteration echo $items[$i]; } for ($i = 0, $n = count($items); $i < $n; $i++) { echo $items[$i]; }} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/php:count-in-loop-condition" = "note"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/php:count-in-loop-condition] reason="..."tx0 explain tx0/php:count-in-loop-condition shows the same in the terminal. Found a false positive? Report it.