LevelsCommand - allow filtering by name, e.g. bin/rector level symfony

This commit is contained in:
Tomas Votruba 2018-08-29 13:31:46 +02:00
parent 8fea703d13
commit c41a8cfbd6

View File

@ -2,8 +2,10 @@
namespace Rector\Console\Command;
use Nette\Utils\Strings;
use Rector\Console\ConsoleStyle;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
@ -27,12 +29,17 @@ final class LevelsCommand extends Command
{
$this->setName(CommandNaming::classToName(self::class));
$this->setDescription('List available levels.');
$this->addArgument('name', InputArgument::OPTIONAL, 'Filter levels by provded name');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$levels = $this->getAvailbleLevels();
if ($input->getArgument('name')) {
$levels = $this->filterLevelsByName($input, $levels);
}
$this->consoleStyle->title(sprintf('%d available levels:', count($levels)));
$this->consoleStyle->listing($levels);
@ -56,4 +63,17 @@ final class LevelsCommand extends Command
return array_unique($levels);
}
/**
* @param string[] $levels
* @return string[]
*/
private function filterLevelsByName(InputInterface $input, array $levels): array
{
$name = $input->getArgument('name');
return array_filter($levels, function (string $level) use ($name): bool {
return (bool) Strings::match($level, sprintf('#%s#', $name));
});
}
}