Skip to content

[Console] Additional validation of mode in InputArgument and InputOption #52055

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

Draft
wants to merge 4 commits into
base: 7.4
Choose a base branch
from

Conversation

jnoordsij
Copy link
Contributor

@jnoordsij jnoordsij commented Oct 14, 2023

Q A
Branch? 6.4
Bug fix? no
New feature? no
Deprecations? yes
Tickets n/a
License MIT

Currently it is allowed to to combine various combinations of input value requirement specifiers, which are logically exclusive behavior-wise. To prevent mistakes or possibly ambiguous behavior, this PR adds additional validation to ensure there's always exactly one specifier part of the mode after finishing the constructor.

This is done by adding a deprecation message for these cases, which can be replaced with an InvalidArgumentException in a future major version. The tests added are currently checking for the deprecation message and could be updated to check for this exception instead.

Also automatically set OPTIONAL whenever REQUIRED is not specified.
@jnoordsij
Copy link
Contributor Author

Tests failing due to issues fixed in #52058; I'll finish this after that is merged.

@jnoordsij jnoordsij marked this pull request as draft October 14, 2023 09:18
…Option

Also automatically set NONE whenever REQUIRED is not specified.
@jnoordsij jnoordsij force-pushed the console-input-mode-validation branch from b1e241c to 251b242 Compare October 14, 2023 09:48
Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this suggestion, that will avoid mistakes. I don't see any legitimate case of using these contradictory options together.

My comments are proposals, to be discussed.

{
$this->expectDeprecation('Since symfony/console 6.4: InputOption mode should be either none, required or optional.');

new InputOption('foo', 'f', InputOption::VALUE_NONE | InputOption::VALUE_REQUIRED);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered using a data provider to reduce repetition?

throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
}

$requirementSpecifier = ($mode & (self::VALUE_NONE | self::VALUE_REQUIRED | self::VALUE_OPTIONAL));
if (!(self::VALUE_NONE === $requirementSpecifier || self::VALUE_REQUIRED === $requirementSpecifier || self::VALUE_OPTIONAL === $requirementSpecifier)) {
trigger_deprecation('symfony/console', '6.4', 'InputOption mode should be either none, required or optional.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add the option name to the deprecation message. That would help to find where the change need to be done when our tools don't provide the backtrace of the trigger.

Co-authored-by: Jérôme Tamarelle <jerome@tamarelle.net>
@OskarStark OskarStark changed the title [Console] Additional validation of mode in InputArgument and InputOption [Console] Additional validation of mode in InputArgument and InputOption Oct 17, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@OskarStark
Copy link
Contributor

What is the status of this PR? If you still want to work on it, please rebase on top of 7.1 branch, thanks

@jnoordsij
Copy link
Contributor Author

What is the status of this PR? If you still want to work on it, please rebase on top of 7.1 branch, thanks

Awaiting the results of #52058, which addresses the test failing here. Once a decision is reached there, I'll be rebasing and adjusting this further.

@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
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.

7 participants