A for loop computes a count or length again in every iteration

tx0/php:count-in-loop-conditionstrictnoteperformance/algorithmicdepth fastperformance

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.

Example from the rule's tests
<?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];    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

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:

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.