diff --git a/src/Console/ConsoleApplication.php b/src/Console/ConsoleApplication.php index 8b637b16f14..c17866b6e36 100644 --- a/src/Console/ConsoleApplication.php +++ b/src/Console/ConsoleApplication.php @@ -16,16 +16,34 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; use Webmozart\Assert\Assert; final class ConsoleApplication extends Application { private const string NAME = 'Rector'; + /** + * @var array + */ + private const array ALLOWED_COMMANDS = [ + // null is for example: bin/rector --version + null, + + // default commands + 'custom-rule', + 'process', + 'list', + 'setup-ci', + 'worker', + 'help', + 'completion', + ]; + /** * @param Command[] $commands */ - public function __construct(array $commands) + public function __construct(array $commands, private readonly SymfonyStyle $symfonyStyle) { parent::__construct(self::NAME, VersionResolver::PACKAGE_VERSION); @@ -73,6 +91,16 @@ public function doRun(InputInterface $input, OutputInterface $output): int $tokens = $privatesAccessor->getPrivateProperty($input, 'tokens'); $tokens = array_merge(['process'], $tokens); $privatesAccessor->setPrivateProperty($input, 'tokens', $tokens); + } elseif (! in_array($commandName, self::ALLOWED_COMMANDS, true)) { + $this->symfonyStyle->error( + sprintf( + 'The following given path does not match any files or directories: %s%s', + "\n\n - ", + $commandName + ) + ); + + return ExitCode::FAILURE; } return parent::doRun($input, $output);