Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

usage of pure-functions/methods within a loop #7003

staabm started this conversation in General
Discussion options

loops are the obvious performance bottlenecks we see in applications every day.
sometimes loops are slower then necessary, because method/function-calls which do not depend on a variable, which changes with iterations is used within the loop, but could just be moved outside the loop.

pseudo-code:

$x = 3;
foreach($iterator as $value) {
 $this->somePureMethod($x);
 // more loop body
}

since the method is pure, invoking it on every iteration is unnecessary and the code could be rewritten to

$x = 3;
this->somePureMethod($x);
foreach($iterator as $value) {
 // loop body
}

should phpstan error when pure functions/methods are used within a loop, but do not depend on its iterations?

another benefit of moving code outside a loop, is more readable code

You must be logged in to vote

Replies: 1 comment

Comment options

It's currently hard to track this and not report code that's valid but in the future this should be possible.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /