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

Add rule checking arguments to unset (#2605) #25

Closed
wants to merge 5 commits into from

Conversation

leongersen
Copy link
Contributor

Adds a rule for checking the arguments to unset. Fixes #2605.

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really nice :) Also make sure to run vendor/bin/phing locally before submitting a PR next time, thanks.

I found a few points. Also, I'm not sure if we want to report "possibly undefined variable" - it's perfectly fine to unset something that might (but doesn't have to) exist.

src/Rules/Functions/UnsetRule.php Outdated Show resolved Hide resolved
src/Rules/Functions/UnsetRule.php Outdated Show resolved Hide resolved
src/Rules/Functions/UnsetRule.php Outdated Show resolved Hide resolved
src/Rules/Functions/UnsetRule.php Outdated Show resolved Hide resolved
tests/PHPStan/Rules/Functions/data/unset.php Outdated Show resolved Hide resolved
@leongersen
Copy link
Contributor Author

I've removed the "possibly undefined variable" check and fixed your other remarks, thanks for reviewing.

@ondrejmirtes
Copy link
Member

Hi, I spent a little bit more time to bring the rule over the finish line. Here are the changes from your PR to mine: https://gist.github.com/ondrejmirtes/1f8b2512798df18baa579a39453946f9

  1. You forgot to add the rule to a level. It wouldn't run at all.
  2. Changed namespace.
  3. I moved the if about iterable key type to IterableType.
  4. I changed the return type of canBeUnset to ?RuleError.
  5. I added recursion so that this case is reported too: unset($notSetVariable['a']);

@ondrejmirtes
Copy link
Member

Thanks!

@ondrejmirtes
Copy link
Member

Please feel free to do a similar thing for ?? operator - PhpParser\Node\Expr\BinaryOp\Coalesce node.

Also, similarly, this isn't detected: https://phpstan.org/r/b56820c1-0e96-45b5-9f98-58dc0fe96d58

We only have VariableCertaintyInIssetRule.

@leongersen
Copy link
Contributor Author

Thanks for finishing this up! I just realized I kept missing the Travis warnings when running the build locally (on windows), because it stops somewhere along the way. I'll try to figure out why that is.

@leongersen leongersen deleted the unset-rule branch November 28, 2019 07:27
@ondrejmirtes
Copy link
Member

ondrejmirtes commented Nov 28, 2019 via email

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

Successfully merging this pull request may close these issues.

Missing type check within unset
2 participants