8000 [Console] Fix type of InputOption::$default by oliverklee · Pull Request #40428 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Fix type of InputOption::$default #40428

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

Merged
merged 1 commit into from
Mar 11, 2021

Conversation

oliverklee
Copy link
Contributor

Options can also be ints. So add this type to the PHPDoc parameter
type annotation of InputInterface::setOption and the return type
annotation of InputInterface::getOption.

Q A
Branch? 4.4 or 5.2 for bug fixes
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #40427
License MIT

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 5.x branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot carsonbot changed the title bug #40427 [Console] Fix PHPDoc annotations in InputInterface [Console] bug #40427 Fix PHPDoc annotations in InputInterface Mar 9, 2021
@@ -122,7 +122,7 @@ public function getOptions();
*
* @param string $name The option name
*
* @return string|string[]|bool|null The option value
* @return string|string[]|int|bool|null The option value
Copy link
Member

Choose a reason for hiding this comment

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

Thank for this PR, but this change looks wrong, The options can be:

  • string: --option=foo
  • string[]: --option=foo --option=bar
  • bool: --option + VALUE_NONE
  • null: --option + VALUE_OPTIONAL

But there is no case where the value can be an integer.

Note: --option=123 returns the string "123".

Copy link
Member

Choose a reason for hiding this comment

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

Indeed, see also #28374 (comment).

Copy link
Member

Choose a reason for hiding this comment

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

We should rather remove int from the getDefault() doc block.

Copy link
Member

Choose a reason for hiding this comment

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

InputOption declares int as a valid type for the default value: in the constructor signature as well as for getDefault() and setDefault(). This looks like a mistake to me because you can never produce an integer on the command line. But it means that it is possible, when following the documented public API, to receive an integer from getOption().

Copy link
Contributor
@dmaicher dmaicher Mar 10, 2021

Choose a reason for hiding this comment

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

Indeed when the default value is used then it can be the case that getOption returns an integer. I just tested it quickly.

public function configure(): void
{
    $this->addOption('foo', null, InputOption::VALUE_REQUIRED, '', 123);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
    var_dump($input->getOption('foo'));die;
}

And then executed without passing any option at all:
==> int(123)

Edit: It actually seems one can pass anything as a default currently. This also works:

->addOption('foo', null, InputOption::VALUE_REQUIRED, '', new \stdClass())

Copy link
Member

Choose a reason for hiding this comment

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

Would you mind to change this PR to fix the default signature? @oliverklee

@oliverklee
Copy link
Contributor Author

Would you mind to change this PR to fix the default signature?

Will do.

@oliverklee oliverklee force-pushed the bugfix/console/option-return-type branch from 38eb3c4 to 34d044c Compare March 11, 2021 12:21
@oliverklee
Copy link
Contributor Author

I have updated the PR accordingly and repushed.

Copy link
Member
@derrabus derrabus left a comment

Choose a reason for hiding this comment

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

I think we also need to adjust Command::addOption().

The types accepted and provided by `InputInterface::getOption`
and `setOption` do not include `int` (and passing something like
`--option=123` will set the option to the string `"123"`, not
to the integer `123`).

The `InputOption` default types should match this.
@oliverklee oliverklee force-pushed the bugfix/console/option-return-type branch from 34d044c to 3a2d1b4 Compare March 11, 2021 15:23
@oliverklee
Copy link
Contributor Author

I think we also need to adjust Command::addOption().

Thanks! Done and repushed.

@derrabus derrabus changed the title [Console] bug #40427 Fix PHPDoc annotations in InputInterface [Console] Fix type of InputOption::$default Mar 11, 2021
@oliverklee
Copy link
Contributor Author

The test failures do not look like they could be related to my change.

@jderusse
Copy link
Member

Thank you @oliverklee.

@jderusse jderusse merged commit bbf786c into symfony:4.4 Mar 11, 2021
@oliverklee oliverklee deleted the bugfix/console/option-return-type branch March 11, 2021 19:09
leofeyer added a commit to contao/contao that referenced this pull request Mar 29, 2021
Description
-----------

Because command options cannot be integers (see symfony/symfony#40428).

Commits
-------

3257cda Fix the PHPStan issues
leofeyer added a commit to contao/core-bundle that referenced this pull request Mar 29, 2021
Description
-----------

Because command options cannot be integers (see symfony/symfony#40428).

Commits
-------

3257cdac Fix the PHPStan issues
@chalasr
Copy link
Member
chalasr commented Mar 29, 2021

(reply to a comment that has been deleted meanwhile)

An immediate solution would be to ignore that failure in your static analyser configuration.
A long term solution would be to submit a PR to doctrine/dbal that casts the option' default value to string ('7' instead of 7), and cast it back to int when accessing the option value (which is probably already done, as when the value is not the default one it's always been a string).

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.

6 participants
0