Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] SymonfyStyle - Check value isset to avoid PHP notice #34114

Merged
merged 1 commit into from Feb 3, 2020

Conversation

leevigraham
Copy link
Contributor

Q A
Branch? 4.3
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #34093
License MIT
Doc PR n/a

This PR addresses the issue when a default value is not a valid choice. Currently this would throw a notice which outputs to the console.

This fix is a similar implementation to the QuestionHelper: https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Helper/QuestionHelper.php#L63

Example console command and output can be found in the issue: #34093

@leevigraham leevigraham changed the title [Console] SymonfyStyle - Check value isset to avoid PHP notice [Console] Fix #34093 - SymonfyStyle - Check value isset to avoid PHP notice Oct 25, 2019
@nicolas-grekas nicolas-grekas changed the title [Console] Fix #34093 - SymonfyStyle - Check value isset to avoid PHP notice [Console] SymonfyStyle - Check value isset to avoid PHP notice Oct 25, 2019
@nicolas-grekas nicolas-grekas added this to the 4.3 milestone Oct 25, 2019
@chalasr chalasr modified the milestones: 4.3, 3.4 Oct 27, 2019
@chalasr
Copy link
Member

chalasr commented Oct 27, 2019

Can you please rebase against 3.4 and add a test to prevent regressions?

@leevigraham
Copy link
Contributor Author

@chalasr Yep… I thought I would throw in a quick PR to get some feedback.

@xabbuh
Copy link
Member

xabbuh commented Oct 29, 2019

I am not really sure how this could happen. Could you please add a test that illustrates why this is necessary?

@Nek-
Copy link
Contributor

Nek- commented Oct 29, 2019

@xabbuh did you see #34093 ?

@xabbuh
Copy link
Member

xabbuh commented Oct 29, 2019

I think we need to clarify that the $default argument must be one of the choices. I am not sure that returning it as is when the choice does not exist is a good approach, but IMO we should throw an exception instead.

@chalasr
Copy link
Member

chalasr commented Oct 29, 2019

I thought this was about supporting using the choice key instead of the value as default, read too fast.
I agree with @xabbuh here, the default must be a valid choice. IIRC it's already enforced by the ChoiceQuestion's default validator, let's prevent the notice with a proper exception.

@leevigraham
Copy link
Contributor Author

leevigraham commented Oct 31, 2019

Thanks for the feedback @xabbuh

I am not really sure how this could happen. Could you please add a test that illustrates why this is necessary?

I ran into this issue when I was trying to figure out how to confirm option inputs during the interact function see: #34093

In the original example the flow should probably be that if the user enters an option skip the question but if I did that when I use the value I would need to throw an InvalidArgument Exception in the execute method.

I think we need to clarify that the $default argument must be one of the choices.

That doesn't seem to be enforced in the QuestionHelper https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Helper/QuestionHelper.php#L63

I am not sure that returning it as is when the choice does not exist is a good approach, but IMO we should throw an exception instead.

If the user is prevented an invalid default and quickly presses enter the console shows an Invalid value error and prompts the user to select a valid value.

@nicolas-grekas
Copy link
Member

I think we need to clarify that the $default argument must be one of the choices.

It looks like nothing requires it in QuestionHelper, so I don't see the need for this enforcement.
Anyway, this would need a test case. @leevigraham can you add one please?

@leevigraham
Copy link
Contributor Author

It looks like nothing requires it in QuestionHelper

Yep that was my thinking. The implementation mirrors the QuestionHelper. I'll add test cases today or over the weekend.

@nicolas-grekas
Copy link
Member

friendly ping @leevigraham

@fabpot
Copy link
Member

fabpot commented Feb 3, 2020

Thank you @leevigraham.

fabpot added a commit that referenced this pull request Feb 3, 2020
…tice (leevigraham)

This PR was merged into the 3.4 branch.

Discussion
----------

[Console] SymonfyStyle - Check value isset to avoid PHP notice

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #34093
| License       | MIT
| Doc PR        | n/a

This PR addresses the issue when a default value is not a valid choice. Currently this would throw a notice which outputs to the console.

This fix is a similar implementation to the `QuestionHelper`: https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Helper/QuestionHelper.php#L63

Example console command and output can be found in the issue: #34093

Commits
-------

c9072c7 Check value isset to avoid PHP notice
@fabpot fabpot merged commit c9072c7 into symfony:3.4 Feb 3, 2020
@leevigraham
Copy link
Contributor Author

@fabpot @nicolas-grekas Thanks for merging… I was overseas on holidays for a few months.

This was referenced Feb 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants