phpstan-symfony
phpstan-symfony copied to clipboard
[Command] Error when using a constant as argument/option default value
Hello,
We have the following error messages while using a command argument/option default value as a constant:
------ ------------------------------------------------------------------------------------------------------------------------------
42 Parameter #4 $default of method Symfony\Component\Console\Command\Command::addArgument() expects string|null, mixed given.
42 Parameter #5 $default of method Symfony\Component\Console\Command\Command::addOption() expects int|string|null, mixed given.
------ ------------------------------------------------------------------------------------------------------------------------------
The following code should return the error:
public const MODE_CREATED = 'created';
protected function configure()
{
$this
->setName('abc')
->addArgument('a', InputArgument::REQUIRED, '', static::MODE_CREATED)
->addOption('b', null, InputOption::VALUE_REQUIRED, '', static::MODE_CREATED)
;
}
Even if the constant value is a string, it's understood as mixed.
For the moment, to avoid the error message, I have a private method which returns the constant value and use it as default value.
Don't know where this error is coming from, mixed
shouldn't be reported like this anywhere.
Anyway, if you change it to self::MODE_CREATED
, it will start working.
@ondrejmirtes to you know what causes this problem?
I get a similar error.
$this->addArgument('threshold', InputArgument::OPTIONAL, 'The maximum number of calls to the API.', 50);
I get this error and as you sad, the DocBlock says mixed
:
------ --------------------------------------------------------------------------------------------------------------------------
69 Parameter #4 $default of method Symfony\Component\Console\Command\Command::addArgument() expects string|null, int given.
------ --------------------------------------------------------------------------------------------------------------------------
I have added my case to the Test and it fails, maybe it helps: https://github.com/gennadigennadigennadi/phpstan-symfony/tree/inputArgument-fails-with-int
Okay.
That's the case properly because an Argument is always a string. Even if default
could be mixed, it doesn't make sense it to anything else but a string or null, right?
Hello!
Could someone explain why this error is thrown? Symfony 5 expects mixed
in all places where this variable is used. For symfony 6 type of this variable is string|bool|int|float|array
Looks like this is from Symfony 4: https://github.com/symfony/symfony/blob/v4.2.5/src/Symfony/Component/Console/Input/InputArgument.php#L37
4.2.5 was the latest version of Symfony when code that throws this error had been written: https://github.com/phpstan/phpstan-symfony/commit/e9e5578d92a854f57e467e5cb3328eda7ed11541. Since then Symfony code has been chaged, so I think there is need to update this library to match with types in symony package.