Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add operand in ternary operators rule #39

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

Conversation

@dmytro-dymarchuk
Copy link

@dmytro-dymarchuk dmytro-dymarchuk commented Oct 1, 2018
edited
Loading

Find unnecessary ternary operators, when if and else part are equal or if and else part could be replaced by condition casted to bool.

jasny reacted with thumbs up emoji
Copy link
Contributor

@dmitrydymarchuk This is already handled by https://github.com/slevomat/coding-standard#slevomatcodingstandardcontrolstructuresdisallowshortternaryoperator-. Also, using the short ternary operator ?: isn't really safe, as you can hide some boolean cases inside the first member.

adaamz reacted with thumbs up emoji

@dmytro-dymarchuk dmytro-dymarchuk force-pushed the add-operands-in-ternary-operator-rule branch from 15aa3fa to 0edc460 Compare November 8, 2018 10:15
Copy link
Author

@carusogabriel PHPStan can be used without PHP CodeSniffer. I think phpstan-strict-rules should return error for shorthand operator in this case.

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.

Hi, I agree with this part If and else parts of ternary operator are equal (%s). but not with Ternary operator is not needed.. PHPStan isn't a codestyle tool so this requirement is too opinionated.

@dmytro-dymarchuk dmytro-dymarchuk force-pushed the add-operands-in-ternary-operator-rule branch from ecb1ffa to cf13bd7 Compare August 5, 2020 12:21
@dmytro-dymarchuk dmytro-dymarchuk force-pushed the add-operands-in-ternary-operator-rule branch from cf13bd7 to 198546b Compare August 5, 2020 12:40
Copy link
Author

Ternary operator is not needed.

This part was removed

@ondrejmirtes ondrejmirtes merged commit c5fa47c into phpstan:master Sep 30, 2020
Copy link
Member

Thank you!

Copy link
Member

Hi, unfortunately I had to revert this rule (03807e3). It finds false positives in cases like:

function (\stdClass $a, \stdClass $b): void {
 rand(0, 1) ? [$a] : [$b];
};

Unfortunately PHPStan\Type\Type has no method to handle these scenarios which would be required to check whether some value is identical or not.

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

Reviewers

@ondrejmirtes ondrejmirtes ondrejmirtes approved these changes

+1 more reviewer

@carusogabriel carusogabriel carusogabriel left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /