Symfony Console: návratová hodnota příkazu
14. 9. 2021Pokud používáte ve svém projektu oblíbený balíček Symfony/Console ve verzi 4.3 a starší, tak při aktualizaci určitě narazíte na chybu “Return value of "App\Console\myCommand::execute()" must be of the type int, "null" returned.”.
Od verze balíčku 4.4 příkaz “execute” vyžaduje návratovou hodnotu a v případě absence hodnoty program zobrazí výše zmíněnou chybu. Od verze 5.1 můžete využít předpřipravené konstanty.
<?php declare(strict_types=1);
namespace App\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class myCommand
* @package App\Console
*/
final class myCommand extends Command
{
protected function configure(): void
{
// ...
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
// it's equivalent to returning int(0)
return Command::SUCCESS;
// it's equivalent to returning int(1)
return Command::FAILURE;
// it's equivalent to returning int(2)
return Command::INVALID;
}
}
Na stejný problém můžete narazit i při aktualizaci Nette - Contributte Console kde composer obsahuje "symfony/console": "^4.2.9|^5.0.0" (https://github.com/contributte/console/blob/master/composer.json#L21).