Skip to content

Conversation

@dmytro-dymarchuk
Copy link

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

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

@carusogabriel
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.

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

Ternary operator is not needed.

This part was removed

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

Thank you!

@ondrejmirtes
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants