Skip to content

Conversation

@jderusse
Copy link
Member

This PR asserts that enums returned by the API are wellknown values. Otherwise it fallback to the UNKNOWN_TO_SDK generic value.

In that way, we respect the contract when saying methods returns a FooEnum::*.

Side note: We still have to keep the psalm exceptions, because psalm can not infer the type of Enum::exists($value) ? $value : Enum:FALLBACK to Enum::*.

see https://psalm.dev/r/518a0924fe

We would need an equivalent of typescript's is keyword https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates

fix #1921

@jderusse jderusse force-pushed the unknown-enum branch 5 times, most recently from 1d991a6 to 5513c75 Compare November 27, 2025 09:04
@stof
Copy link
Member

stof commented Dec 24, 2025

The static analysis can actually be fixed by using @psalm-assert-if-true: https://psalm.dev/r/b90672fbda

@jderusse jderusse force-pushed the unknown-enum branch 2 times, most recently from 920fb66 to 845cb29 Compare December 24, 2025 14:29
@jderusse
Copy link
Member Author

Diff is huge.. Here is a summary:

  1. added public const UNKNOWN_TO_SDK = 'UNKNOWN_TO_SDK'; in all Enums
  2. added @psalm-assert-if-true self::* $value on all Enum's static function exists methods
  3. in resutls: replaced $value by !TheEnum::exists($value) ? TheEnum::UNKNOWN_TO_SDK : $value.
  4. in input: added /** @psalm-suppress NoValue */ after if (!TheEnum::exists($v)) { throw ... } because I want to keep the check even if the docbloc says $v is a TheEnum::* and I'm not sure we should relax the docblock to TheEnum::*|string

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.

Better handling of unknown enum values in responses

2 participants