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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

#154 - Add new Ternary sniff #237

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

#154 - Add new Ternary sniff #237

wants to merge 3 commits into from

Conversation

tfrommen
Copy link
Contributor

This PR adds a new sniff, HM.PHP.Ternary that adds a warning for unnecessary ternary expression, as mentioned in #154:

  • $expr ? true : false;
  • $expr ? false : true.

See fixtures for (passing and failing) example code.

Output is as follows:

  3 | WARNING | Unnecessary ternary found: Instead of "$expr ? true : false", use "(bool) $expr"
  4 | WARNING | Unnecessary ternary found: Instead of "$expr ? false : true", use "! $expr"

Can anyone think of any edge-case usage that the sniff would either flag as false-positive, or that it would miss?

I'm not 100% sure I considered all tokens that would end the ternary, which currently are: semicolon, closing parenthesis, closing curly brace (function or any other scope), and also comma (which I just added in a subsequent commit).
Anything else?
Maybe @jrfnl? 馃檪

@tfrommen tfrommen self-assigned this Oct 28, 2020
@tfrommen
Copy link
Contributor Author

tfrommen commented Feb 4, 2021

Instead of a custom sniff, we might want to use the UselessTernaryOperatorSniff one included in the Slevomat Coding Standards...? This one is also fixable, which is nice.

I think I will create a separate issue to discuss pulling in the Slevomat Coding Standards...

@tfrommen tfrommen removed their assignment Mar 25, 2024
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.

None yet

1 participant