Skip to content

[New Rule] Function/method call in for loop declaration MUST be avoided #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lenaorobei opened this issue Jan 31, 2019 · 10 comments
Closed
Labels
need to discuss Rule requires discussion new rule New feature implementation proposal New rule proposal waiting for feedback Additional explanation needed

Comments

@lenaorobei
Copy link
Contributor

lenaorobei commented Jan 31, 2019

Rule

Function/method call in for loop declaration MUST be avoided.

Reason

PHP will call the method/function on each iteration of the for loop.

Bad Example:

for ($i = 0; $i < count($array); $i++){
    //code goes here
}

Good example:

$count = count($array);
for ($i = 0; $i < $count; $i++){
    //code goes here
}

Implementation

  • Subscribe to the T_FOR token.
  • Check if condition contains function call (T_STRING with followed by open parentheses).
@lenaorobei lenaorobei added proposal New rule proposal need to discuss Rule requires discussion new rule New feature implementation labels Jan 31, 2019
@ravmenon
Copy link

ravmenon commented Feb 2, 2019

Hopefully some future versions of PHP compiler will optimize away such constructs to make this sniff unnecessary (most modern compilers handle this well), but for now, if we have this sniff, it should only be a warning.

@alexkiselov
Copy link

This rule requires better description.

The title confuses: I thought inside the loop itself, not in "for" statement definition.

@lenaorobei lenaorobei changed the title [New Rule] Function/method call in for loop MUST be avoided [New Rule] Function/method call in for loop declarationMUST be avoided Feb 5, 2019
@lenaorobei
Copy link
Contributor Author

@akiseliov thanks for the feedback. I updated the title. Hope it makes more sense now.

@lenaorobei lenaorobei changed the title [New Rule] Function/method call in for loop declarationMUST be avoided [New Rule] Function/method call in for loop declaration MUST be avoided Feb 5, 2019
@Zifius
Copy link
Member

Zifius commented Feb 6, 2019

Hi @lenaorobei, something like this was done in the ECG Sniff: https://github.com/magento-ecg/coding-standard/blob/master/Ecg/Sniffs/Performance/LoopSniff.php

Maybe it makes sense to port it. What's your vision generally on those sniffs?

@lenaorobei
Copy link
Contributor Author

Hi @Zifius. We use this rule for Magento Marketplace verification and I noticed a lot of false-positive findings. This is the reason why I didn't include this sniff and created a proposal. Performance/LoopSniff detects specific function/method calls inside the loop and from my point of view the detection of function call in the for loop declaration (for ($i = 0; $i < count($array); $i++)) gives more accurate results.

@orlangur
Copy link

@lenaorobei this rule is totally useless actually and was rejected endless amount of times, for instance: magento/magento2#17501 (comment)

@lenaorobei lenaorobei added the waiting for feedback Additional explanation needed label Feb 13, 2019
@lenaorobei
Copy link
Contributor Author

@orlangur I'm really interested in understanding your firm position regarding this rule.

Few points from my side about this:

  1. This rule is not only for count, but for any function/method call in the for loop.
  2. It's well-known "PHP bad practice" item. I agree that it is not about performance. It's more about code style. If we want to write stable and reliable code, we need to follow such best practices. If we can eliminate potential performance issues, we need to do this. It's like we encourage the use of strict comparison === to eliminate potential security issues because of type casting.
  3. Once introduced the rule will be applicable to changed files and in future we will be able to fix legacy issues as well.

Looking forward to constructive dialogue.

@orlangur
Copy link

  1. not only for count, but for any function/method call in the for loop

Calling function in loop signature is perfectly valid, just like calling within if condition, for example. For function in general such rule is not applicable as there is no guarantee it is idempotent.

2. It's well-known "PHP bad practice" item

Proof, please. All previous contributions I met were just about performance impact and I provided an evidence such concerns are groundless.

2. It's like we encourage the use of strict comparison === to eliminate potential security issues because of type casting.

Security aspect is only applicable to compare some hashes where string may be accidentally interpreted as number with == being in use.

3. Once introduced the rule will be applicable to changed files and in future we will be able to fix legacy issues as well.

Don't see how it is related to this issue, generally a real standard is something our code is 100% compliant with (like it was after magento/magento2#8685) and we can have additional checks for new files like it works for declare(strict_types=1). Basically, there is no need to distract contributors with "legacy" coding style issues, we must simply eliminate them.

So, to summarize this particular rule, there is no need to put some function call out of loop signature until profiler shows it is a bottleneck.

@lenaorobei
Copy link
Contributor Author

The rule is already included from OOB Generic Coding Standard:

<rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall">
    <severity>7</severity>
    <type>warning</type>
</rule>

@orlangur
Copy link

@lenaorobei so what is an actual basis for this list #14? Why it wasn't discussed and cleaned up from useless rules like this one?

magento-devops-reposync-svc pushed a commit that referenced this issue Aug 25, 2021
…-coding-standard-231

[Imported] Version 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need to discuss Rule requires discussion new rule New feature implementation proposal New rule proposal waiting for feedback Additional explanation needed
Projects
None yet
Development

No branches or pull requests

5 participants