Error Identifier: symfonyConsole.optionNotFound
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class GreetCommand extends Command
{
protected function configure(): void
{
$this->addOption('name', null, InputOption::VALUE_REQUIRED, 'Who to greet');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$greeting = $input->getOption('greeting');
return Command::SUCCESS;
}
}
Why is it reported? #
This error is reported by the phpstan-symfony extension.
The command calls $input->getOption() with an option name that was not defined in the configure() method. In the example above, the command defines the option name but tries to retrieve the option greeting, which does not exist. This will throw an InvalidArgumentException at runtime.
How to fix it #
Use an option name that matches one defined in the configure() method:
<?php declare(strict_types = 1);
protected function execute(InputInterface $input, OutputInterface $output): int
{
- $greeting = $input->getOption('greeting');
+ $name = $input->getOption('name');
return Command::SUCCESS;
}
Or add the missing option definition:
<?php declare(strict_types = 1);
protected function configure(): void
{
$this->addOption('name', null, InputOption::VALUE_REQUIRED, 'Who to greet');
+ $this->addOption('greeting', null, InputOption::VALUE_REQUIRED, 'The greeting to use');
}
How to ignore this error #
You can use the identifier symfonyConsole.optionNotFound to ignore this error using a comment:
// @phpstan-ignore symfonyConsole.optionNotFound
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: symfonyConsole.optionNotFound
Rules that report this error #
- PHPStan\Rules\Symfony\UndefinedOptionRule [1] phpstan/phpstan-symfony