Skip to content

fix: exclude null from Option\fromValue() return type - #106

Closed
mathroc wants to merge 1 commit into
texthtml:mainfrom
mathroc:mathroc/option-fromvalue-exclude-the-none-value-from-the
Closed

mathroc wants to merge 1 commit into
texthtml:mainfrom
mathroc:mathroc/option-fromvalue-exclude-the-none-value-from-the

Conversation

@mathroc

@mathroc mathroc commented Nov 16, 2026

Copy link
Copy Markdown
Member

Closes #103

With the default $noneValue (null), Option\fromValue() no longer keeps null in the inner type:

/** @return Option<int> */
function codeblockStart(\ArrayIterator $lines): Option
{
    return Option\fromValue($lines->key()); // OK now (was Option<int|null>)
}
 * @template U
 * @param U|null $value
 * @param (callable(mixed):bool)|null $isSome
 * @phpstan-param (callable(U|($value is null ? null : never)):bool)|null $isSome
 * @return ($isSome is null
 *     ? ($noneValue is null ? Option<U> : Option<U|($value is null ? null : never)>)
 *     : ($noneValue is null ? Option<U|($value is null ? null : never)> : never))
  • Binding through U|null strips null; ($value is null ? null : never) adds it back where it can be a Some value (custom $noneValue, or $isSome). So fromValue(?int, noneValue: 0) stays Option<int|null>, and the $isSome callback is still typed as receiving null (PHPStan & Mago reject fn (int $i) for a ?int value).
  • The precise $isSome type is in @phpstan-param (also read by Mago): Psalm silently drops the whole docblock when a @param type references $value. Psalm sees callable(mixed):bool, it was already blind to callback parameter types.
  • Generic fromValue($t) with T|null is Option<T>. Known gap: in PHPStan, generic code with a custom $noneValue/$isSome now gets Option<T|null> instead of Option<T>.

Not included

  • Custom none values (false, …): fromValue($line, noneValue: false) is still Option<int|false>. $value is false-style conditionals make PHPStan widen generic fromValue($t) to Option<T|false> (regressing the default case), and $value is U|false is always resolved as true by PHPStan, which is unsound (int|false|null with noneValue: 0 inferred as Option<int|null>).
  • of() / tryOf() / ify() / tryIfy(): the equivalent needs conditionals on the callback return type ($callback is callable():null), which only PHPStan supports (Psalm: invalid docblock; Mago: not evaluated).

Tests

New fixtures in tests/type-hinting/option.php (nullable → Option<int>, false kept, generic, custom $noneValue keeps null 🎯, $isSome keeps null, $isSome callback rejecting null 🎯 with a 🫥 psalm blind spot). PHPUnit, doctest, PHPStan, Psalm, mago analyze/lint/fmt pass.

🤖 Generated with Claude Code

With the default `$noneValue` (`null`), `Option\fromValue($nullableInt)` is
now typed `Option<int>` instead of `Option<int|null>`. `null` is kept in the
type when it can be a `Some` value: with a custom `$noneValue` or `$isSome`.

The precise `$isSome` type lives in `@phpstan-param` (read by PHPStan and
Mago) because Psalm drops the whole docblock when a `@param` type
references `$value`.

Custom none values (e.g. `false`) are not excluded: every formulation tried
was either unsound in PHPStan or widened generic `fromValue($t)` to
`Option<T|false>`.

Closes texthtml#103

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (5d6699f) to head (bf2a475).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##                main      #106   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
  Complexity       117       117           
===========================================
  Files             10        10           
  Lines            406       406           
===========================================
  Hits             406       406           
Flag Coverage Δ
php-8.5 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/functions/option.php 100.00% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mathroc mathroc closed this Nov 16, 2026
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.

Option\fromValue(): exclude the none value from the returned Option type

1 participant