Comments (3)
@keevan This is not really a place to ask these questions as this is not a support forum for standard designers, but a place to report bugs in sniffs and/or request new sniffs.
Whether you want to add a sniff or not, is up to the individual standards designer.
Please also know that if you like some errors in a sniff, but not others, you can choose to include/exclude based on error code.
<rule ref="Universal.Operators.DisallowStandalonePostIncrementDecrement">
<exclude name="Universal.Operators.DisallowStandalonePostIncrementDecrement.PostIncrementFound"/>
<exclude name="Universal.Operators.DisallowStandalonePostIncrementDecrement.PostDecrementFound"/>
</rule>
... is effectively the same as...
<rule ref="Universal.Operators.DisallowStandalonePostIncrementDecrement.MultipleOperatorsFound"/>
The short version of "why this sniff was added", comes down to:
- I've seen too many devs not know the difference and treating pre/post-increment/decrement as if they were doing the exact same thing.
- This is largely caused by most code samples of
for
loops using$i++
in the third part of the condition and devs only knowing that form, but not actually realizing that++$i
and$i++
will only do the same thing when used in stand-alone statements. - As soon as incrementing/decrementing is combined with other code, the difference will cause different behaviour/results, which is where the bugs come in (and I've had to fix enough of those).
- In too many of those cases, the bug was caused by the dev not knowing about pre-increment/decrement or not knowing the difference between pre and post-increment/decrement and expecting the behaviour to be the same. The intended behaviour was in most cases pre-increment/decrement.
- That combined with the minor performance (memory usage) improvement, made that it seemed like a good sniff to add, especially as it should also help educate devs about the difference.
Also have a look at: https://3v4l.org/hLVgC which will stump every dev who doesn't know/understand the difference.
As for why the sniff only checks stand-alone statements - the sniff cannot determine whether the post-increment/decrement was intentional or is a bug and auto-fixing non-stand-alone statements would cause a change in the behaviour of the code, which is something sniffs should not do.
Hope this helps a little.
from phpcsextra.
Thank you @jrfnl. I appreciate the time you've taken to give a detailed explanation, laying it all out, as well as going further and creating the example code snippet.
Yes, it is a topic many devs might not be familiar with, and it might be because some never needed to deal with it as extensively. In that respect usage may differ from project to project.
I've never thought about copying the $i++
and combining it directly with other code without giving it some thought. But from your experience, it seems like this happens with devs more often than I thought, and so the sniff aims to preemptively protect against such misuse 👍
I'm happy to close this for now. And I'm sure a future traveler would find this extra bit of documentation / discussion useful.
from phpcsextra.
And I'm sure a future traveler would find this extra bit of documentation / discussion useful.
Indeed ;-)
from phpcsextra.
Related Issues (20)
- Add the unneeded ternary sniff HOT 1
- Sniff(s) to detect proper usage of preg_quote()
- Sniff to detect the irregular whitespace characters HOT 3
- Sniff to discourage "long" closures
- False positive in Universal.CodeAnalysis.ConstructorDestructorReturn HOT 3
- Incorrect detection of PHP 4-style constructors by Universal.CodeAnalysis.ConstructorDestructorReturn HOT 9
- Sniff to enforce naming conventions for namespace names
- Sniff to enforce naming conventions for class/function/const aliases
- Sniff to detect useless alias names HOT 1
- Sniff to flag `is_a()` and possibly replace with `instanceof`
- Ignore specific keys for Universal.Arrays.MixedArrayKeyTypes and Universal.Arrays.MixedKeyedUnkeyedArray HOT 9
- Universal.Arrays.MixedArrayKeyTypes should report on explicit keys, if implicit keys are the majority HOT 2
- Add option to NormalizedArrays.Arrays.ArrayBraceSpacing to allow single- or multi-line empty arrays HOT 4
- preg_replace replacement with variable must be wrapped in addcslashes HOT 1
- Problem with NormalizedArrays.Arrays.CommaAfterLast in some cases HOT 7
- Feature suggestion: new sniff `EchoConcatenation`
- Potential new sniff: scan code for "Trojan source"
- Feature suggestion: new sniff for detecting brand name or text string HOT 1
- Please add a sniff to format array key => value pairs HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpcsextra.