Skip to content
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

Do not trigger for unused value in foreach loop with key used #65

Closed
kkmuffme opened this issue Jan 21, 2019 · 4 comments
Closed

Do not trigger for unused value in foreach loop with key used #65

kkmuffme opened this issue Jan 21, 2019 · 4 comments

Comments

@kkmuffme
Copy link

kkmuffme commented Jan 21, 2019

Sometimes I have to only use the keys of an array. Using an foreach loop is faster than first doing array_keys() and then looping.
However it will always report the $value as unused (bc it is unused).

If would be nice if it would NOT report the $value as unused if the $key is used:
(or if that is too complicated, maybe not at all if its a key => value foreach loop)

foreach ( $array as $key => $value ) {

@sirbrillig
Copy link
Owner

That's an interesting idea. I could have the sniff ignore variables created that way, but in the mean time you can do this:

Configure validUndefinedVariableNames to add something like ignored. Then if you don't plan on using the value of an associative foreach loop,

foreach ( $array as $key => $ignored ) {

@sirbrillig
Copy link
Owner

I think it would be a good idea to make this a new configuration option. Maybe ignoreUnusedLoopValues

@kkmuffme
Copy link
Author

Thanks good idea.

The small issue with $ignored is, that some time in x years, someone else will go through the code and will have no idea what the value of the array actually is in case he wants to use it now, so he first needs to debug it or I add additional superflous comments.

@sirbrillig
Copy link
Owner

That's a good point. I do think that this comes up often enough to at least be an option. Once we have that, we can consider if we want that option to be on by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants