From c41a8cfbd6c3dd9e0eac44e3ec85aed98a7fc36e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 29 Aug 2018 13:31:46 +0200 Subject: [PATCH] LevelsCommand - allow filtering by name, e.g. bin/rector level symfony --- src/Console/Command/LevelsCommand.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Console/Command/LevelsCommand.php b/src/Console/Command/LevelsCommand.php index 2a887c72280..ac9945e1407 100644 --- a/src/Console/Command/LevelsCommand.php +++ b/src/Console/Command/LevelsCommand.php @@ -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)); + }); + } }