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

Squiz/ObjectInstantiation: bug fix - account for null coalesce #3348

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented May 11, 2021

The Squiz.Objects.ObjectInstantiation did not take null coalesce into account.

It also was potentially a little too lenient for ternaries as an inline then or inline else token before the new keyword would be considered an assignment, while it was never checked that the result of the ternary was actually assigned to something.

While still not 100%, this commit improves the sniffs to:

  1. Allow for null coalesce assignments ??=.
  2. For ternary tokens + null coalesce ??: verify if the result of either of these is actually "assigned" to something (or preceded by one of the other "allowed" tokens).

This should reduce the number of both false positives as well as false negatives, though there is still the potential for some false negatives - see the test on line 32 of the test case file.
This could be further reduced in a future iteration on this sniff, but for now, this fix can be considered a significant step forward.

Includes unit tests.

Fixes #3333

The `Squiz.Objects.ObjectInstantiation` did not take null coalesce into account.

It also was potentially a little too lenient for ternaries as an inline then or inline else token before the `new` keyword would be considered an assignment, while it was never checked that the result of the ternary was actually assigned to something.

While still not 100%, this commit improves the sniffs to:
1. Allow for null coalesce assignments `??=`.
2. For ternary tokens + null coalesce `??`: verify if the result of either of these is actually "assigned" to something (or preceded by one of the other "allowed" tokens).

This should reduce the number of both false positives as well as false negatives, though there is still the potential for some false negatives - see the test on line 32 of the test case file.
This could be further reduced in a future iteration on this sniff, but for now, this fix can be considered a significant step forward.

Includes unit tests.

Fixes 3333
@gsherwood
Copy link
Member

Thanks for fixing this.

Line 32 probably is a false negative, although it is an interesting one and could go either way.

@jrfnl jrfnl deleted the feature/3333-squiz-objectinstantiation-bugfix-allow-for-null-coalesce branch August 12, 2021 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Squiz.Objects.ObjectInstantiation: null coalesce operators are not recognized as assignment
2 participants