phpstan-symfony icon indicating copy to clipboard operation
phpstan-symfony copied to clipboard

[Command] Error when using a constant as argument/option default value

Open fsevestre opened this issue 5 years ago • 4 comments

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.

fsevestre avatar Aug 01 '19 13:08 fsevestre

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 avatar Aug 01 '19 13:08 ondrejmirtes

@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

gennadigennadigennadi avatar Apr 04 '22 17:04 gennadigennadigennadi

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?

gennadigennadigennadi avatar Apr 05 '22 04:04 gennadigennadigennadi

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.

GSpecDrum avatar Jun 17 '22 10:06 GSpecDrum