Skip to content

Improve the method signature of Option::fromValue to better understand nullable/optional value - #78

Closed
ojhaujjwal wants to merge 1 commit into
schmittjoh:masterfrom
ojhaujjwal:bugfix/fix-psalm-error-from-value
Closed

Improve the method signature of Option::fromValue to better understand nullable/optional value#78
ojhaujjwal wants to merge 1 commit into
schmittjoh:masterfrom
ojhaujjwal:bugfix/fix-psalm-error-from-value

Conversation

@ojhaujjwal

Copy link
Copy Markdown

When using Option::fromValue($something), psalm decoded it was Option<string | null> because $something is of type string | null.

This is the psalm error I got in my codebase:
image

However, I want to let psalm know that even if I pass something like null to Option::fromValue ,the output is actually only Option<string> instead of Option<string | null>.

@ojhaujjwal

Copy link
Copy Markdown
Author

@schmittjoh It would be great if you could review this.

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

Thanks for the PR. I tested this against current PHPStan and Psalm (both 5 and 6): the headline case still infers Option<string|null> when the value itself is string|null, since neither analyzer subtracts P when solving S from S|P, and the S|P parameter type makes the library's own PHPStan run fail because new Some($value) is now Some<S|P> rather than Some<S>. The improvement it does deliver, no longer widening the return type by the none value or the null default, is achieved more simply by typing $noneValue as mixed, which is under consideration in #63, so I'm closing this in favour of that approach.

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.

2 participants