Also totally legal, it is generally stupid to do an assignment in an if() statement (for instance, it is not possible in Ada, for good reasons).
Writing:
$a = $b;
if ($a) { ...
won't kill anybody, but writing:
if ($a = $b != 0) { ...
is not going to work going to work as expected by 99% of the programmers (okay, maybe only 60%, still!)
As a C/C++ programmer, I can tell you that we run in the exact same problem and I still see tons of people doing it wrong... and getting bugs like shown here.
I don't really know how the parser works, if we can easily detect an if() statement, then checking if an equal operator appears between the () should be easy.
Thank you.
Alexis Wilke
Comments
Comment #1
dawehnerBut we have to exclude stuff like:
Its used everywhere.
Comment #2
AlexisWilke commentedDereine,
Yes. And writing that on two lines is safer and easier to read:
The one where I would agree we'd have a "problem" is the while() with a database call as in:
And in this case it is annoying to remove the ability...
Comment #3
snufkin commentedWell a useful trick is to do comparisons like this:
and if you entered just one '=' php will simply throw an error, because you cant assign value to strings. I quite like the compact form of ($result = blabla), i do it quite often in cache checks, or db result checks.
Comment #4
AlexisWilke commentedsnufkin,
In this example,
It would become this:
which then would work properly.
Now, how many programmers, of all those who program modules on Drupal will write such a statement? (i.e. the 2nd one.)
The idea for coder is to help silly billies like me to find bugs, I think... 8-)
Comment #5
hefox commentedAssignments in control structures is perfectly legal in and, tmk (considering it's used in core a lot in the $bleh = blah() way), follows coding standards fine. I'd won't fix this if I was maintainer of coder.
To my understanding of coder, it's not suppose to be adding *new* coding standards, but instead enforcing existing drupal coding standards, ie to have coder add this check, first need to have drupal coding standards have this. Coder tough love?
Comment #6
douggreen commentedI added a similar rule to the 7.x-2.x druplart review (because I have made this suggestion in my latest yet unpublished article. I agree that there shOuld be some valid exceptions and would appreciate your help in getting this right.
Comment #7
AlexisWilke commentedIt is cool to hear that you want to apply this after all. 8-)
I'm not using Drupal 7.x at all so I won't be able to help in the implementation and I'm not too sure where D7.x is at in terms of using the assignment operator "in the wrong place".
I would test the if(), while() and for() second term at the start.
The only one that was a problem in D6 was the while($row = db_query()) { ... }. But D7 changed the way they handle database queries so that may not even be a concern anymore and such loops can be fixed using something like this:
A way to determine what should be accepted would be to first do a run on Drupal Core and see what appear in all those if(), while(), and for(;;) so we can determine what is considered "okay" and what is not.
Thank you.
Alexis Wilke
P.S. Also it is a little more writing, the compilers know better than you how to optimize and writing everything inside an if() or while() does not help make the code run faster. So the for(;;) {...} example I present here is as fast as a while($row = db_query()).
Comment #8
salvis@AlexisWilke:
You're going way overboard with this abomination.
Just because you want to flag
is no reason to bark at perfectly fine constructs such as
and
I completely disagree with your #2:
It is harder to write, harder to read, it obscures the logic, it wastes precious space on the screen, in the eye, and in the mind.
We're all free to have our pet peeves, but forcing others to comply with yours is a different matter.
A big -1!
Comment #9
douggreen commentedI am adding some form of check to an optional review, not the style review. And I've changed the output to clearly flag which review each warning comes from. I think that people who want more suggestions will run this review, just like people who want more suggestions now run coder tough love. I was hoping to bring coder tough love under the coder distribution (I haven't talked to @Morbus yet). If there's an uproar over this, we can certainly leave the optional reviews as separate projects, but IMO, distributing them all as once is helpful.
Comment #10
douggreen commentedComment #11
klausiCoder 7.x is frozen now and will not receive updates. Coder 8.x-2.x can be used to check code for any Drupal version, Coder 8.x-2.x also supports the phpcbf command to automatically fix conding standard errors. Please check if this issue is still relevant and reopen against that version if necessary.