Skip to content

Conversation

VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Oct 5, 2025

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 ?)

@VincentLanglet VincentLanglet changed the title Allow short ternary for false POC - Allow short ternary for false Oct 5, 2025
@ondrejmirtes
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.

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

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.

Allow short ternary operator for boolean
3 participants