Skip to content

Option/Either: Return both outcomes from conditional return types - #53

Merged
jarstelfox merged 4 commits into
masterfrom
psalm-conditional-types
Nov 13, 2026
Merged

jarstelfox merged 4 commits into
masterfrom
psalm-conditional-types

Conversation

@jarstelfox

Copy link
Copy Markdown
Member

🤖

Psalm 7.3.1 started deciding our conditional return types, and they were wrong. Option<int>->valueOr(null) is now typed int, and match() on an Either<L, R> returns only the right callback's type. The iFixit app picked up 49 Psalm errors from this, and it carries a stub of the corrected types until a release has this fix (iFixit/ifixit#66586).

Why it broke now

Types like valueOr's (T is never ? TT : T) only describe the case where the Option holds a value. Older Psalm couldn't decide T is never for an Option<int>, so it unioned both branches and the result happened to be right. vimeo/psalm#13433 taught Psalm to decide it, so it now takes the T branch alone.

Fix

Every conditional now ends in the case where both outcomes are possible, e.g. (T is never ? TT : T|TT). For Either it also uses the other side being never, so Either::left(1)->leftOr(null) is still just int.

Reading every conditional turned up more wrong types:

  • orRight had its branches swapped.
  • mapRight, andThen and flatMapLeft returned the unmapped type.
  • The *NotNull/*NotFalsy and filterLeft/filterRight types dropped a value they can return.
  • map and mapSafely had @psalm-param $mapFunc callable(T):U, name first, so Psalm never bound U. mapSafely also said it returned self<T>.

CI

Psalm 5 can't check any of this, because it always unions both branches. Psalm 7 needs PHP 8.2+, so it moves to its own job on PHP 8.4 using the 7.4.0 phar. vimeo/psalm leaves require-dev, so the lock still installs on 7.4 for PHPUnit. The commit message for that change lists how each new Psalm 7 report was handled. 14 test-only reports are baselined.

tests/ReturnTypes.php holds 15 @psalm-check-type-exact checks. 13 fail on master's types and all pass here.

QA

  • The PHPUnit jobs pass on 7.4, 8.0 and 8.1
  • The new Psalm job passes
  • After tagging a release, the iFixit app can drop Tests/Psalm/Stubs/php7-optional.phpstub with no new Psalm errors

🤖 Generated with Claude Code

jarstelfox and others added 4 commits November 13, 2026 09:42
`@psalm-param $mapFunc callable(T):U` has the name before the type, so
Psalm never read it and U was never bound. mapSafely also said it
returned self<T> when it returns the mapped Option<U>.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An Option<T> whose T isn't never could be some or none, but types like
valueOr's (T is never ? TT : T) only described the some case. Older
Psalm couldn't decide "T is never" and unioned both branches, which hid
this. Psalm 7.3.1 decides it (vimeo/psalm#13433), so
Option<int>->valueOr(null) became int and match() on an Either<L, R>
returned only the right callback's type.

Each conditional now ends in the case where either outcome is possible.
For Either that also uses the other side being never: Either::left(1)
is an Either<int, never>, so leftOr() on it is still just int.

Fixed along the way:
- orRight had its branches swapped.
- mapRight, andThen and flatMapLeft returned the unmapped type when
  the other side wasn't never.
- leftNotNull, rightNotNull, leftNotFalsy and rightNotFalsy dropped the
  value the Either keeps when it's already the other side.
- filterLeft and filterRight dropped the new value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Psalm 5 unions both branches of every conditional return type, so it
can't check the types fixed in the last commit. Psalm 7 needs PHP 8.2+
while PHPUnit still runs on 7.4 to 8.1, so Psalm now runs in a separate
job on PHP 8.4 from its phar, and vimeo/psalm leaves require-dev so the
lock still installs on 7.4.

What Psalm 7 reports beyond the types:
- Unused code: off, since every public method is the library's API.
- ClassMustBeFinal: suppressed for src, since callers may extend
  Option, Either and the exception. The test classes are now final.
- ImpureMethodCall: MissingValueException's constructor only passed
  its arguments to Exception's, so it's gone.
- The tests' assertEquals() on strings are now assertSame(), and
  SomeComplexThing::doWork() checks for null explicitly.
- Baselined: 14 in tests that pass callbacks Psalm proves never run,
  or assert what Psalm already knows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
13 of these 15 checks fail on the types before the last two type fixes
and pass after, on Psalm 7.4.0. The other two pin the cases where one
outcome is ruled out: Option::none()->valueOr(null) is null and
Either::left($int)->leftOr(null) is int.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jarstelfox
jarstelfox merged commit 229a003 into master Nov 13, 2026
4 checks passed
@jarstelfox
jarstelfox deleted the psalm-conditional-types branch November 13, 2026 19:28
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.

1 participant