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

POC - Allow short ternary for false #287

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

Closed
VincentLanglet wants to merge 1 commit into phpstan:2.0.x from VincentLanglet:pocShortTernary

Conversation

@VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Oct 5, 2025
edited
Loading

Hi @ondrejmirtes

I recently encountered situation where short ternary could be considered useful, for instance when using Datetime::createfromformat. https://www.php.net/manual/en/datetime.createfromformat.php

Since the return type is object|false there is no risk writing

DateTime::createFromFormat(....) ?: $defaultValue

and this is way shorter than

$value = DateTime::createFromFormat(....);
if ($value === false) {
 $value = $defaultValue;
}

But in other cases I do like to forbid short ternary because things like

$intOrFalse ?: $defaultValue

is risky, I might have forgot that 0 will end in the default value.

So if you're interested I see two solutions:

  1. Relaxing the "forbid ternary" rules to allow nonFalsey|false conditions (and maybe updating the error message behind the bleeding edge tag)
  2. Or introducing a config option to allow nonFalsey|false conditions (maybe you'll have a naming suggestion for this one ?), and when the option is enabled I think the error message should be changed too. (like "Short ternary is not allowed on non boolean falsey type. Use null coalesce operator if applicable or consider using long ternary.")

This is currently a POC which should be updated based on your preferences. :)

Looking at how sometimes some rules are loosen up (like in 8afd4af), I think it could be ok to relax the forbid ternary rule without an option. But your call.

This would also close #268 (Should I assume that since you didn't close the issue, you're open to some suggestion ?)

ruudk reacted with thumbs up emoji
@VincentLanglet VincentLanglet changed the title (削除) Allow short ternary for false (削除ここまで) (追記) POC - Allow short ternary for false (追記ここまで) Oct 5, 2025
Copy link
Member

I'm not too keen on allowing this. For the same reason I don't want to allow empty() even when it's safe in some situations (like on arrays and when the variable always exists).

Seeing $a ?: $b or empty($a) in code always triggers a red alert in my head. I'm really careful and I know loose comparisons are problematic in PHP. I don't ever want to see $a ?: $b because developers should always pause and be on alert when they see a construct like that. So seeing $a ?: $b instead of $a !== false ? $a : $b actually consumes more brain cycles and I like to avoid that in my code.

I believe it should be possible to write a https://phpstan.org/developing-extensions/ignore-error-extensions for people who'd want to keep this in their code in specific situations.

Copy link
Contributor

ruudk commented Oct 6, 2025

I believe it should be possible to write a https://phpstan.org/developing-extensions/ignore-error-extensions for people who'd want to keep this in their code in specific situations.

@VincentLanglet If you decide to write such ignore extension, please do share it here as well 😁 Would be good to give it a try in our project too.

LastDragon-ru reacted with thumbs up emoji

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

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Allow short ternary operator for boolean

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