[11.x] Fix prompt fallback return value when using numeric keys #50995
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes an inconsistency issue with any Prompt fallbacks that rely on Symfony's choice component.
The inconsistency occurs when passing an array of options with numeric keys that are not sequential, starting from 0.
Prompts uses PHP's
array_is_list
function to determine whether the returned value should be the selected key or the selected value. Symfony's choice component differs by always returning the value for any array with all numeric keys, regardless of whether they are sequential from 0:This is an issue because the return value is inconsistent depending on whether or not the fallback was activated.
This PR solves this by wrapping each option in a new
PromptOption
class and passing them to Symfony as a list. Symfony can render the string version of thePromptOption
class, and because it's passed as a list, it will always return the selectedPromptOption
class, allowing us to extract the correct value.A nice side effect of always passing a list is that Symfony will not display the underlying array keys to the user, which makes the fallback more consistent with Prompts. It also simplifies the approach we use to inject the "None" option for the
multiselect
fallback.