Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/Console/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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
Expand All @@ -25,7 +26,7 @@ final class ConsoleApplication extends Application
/**
* @param Command[] $commands
*/
public function __construct(array $commands)
public function __construct(array $commands, private readonly SymfonyStyle $symfonyStyle)
{
parent::__construct(self::NAME, VersionResolver::PACKAGE_VERSION);

Expand Down Expand Up @@ -57,12 +58,15 @@ public function doRun(InputInterface $input, OutputInterface $output): int

$commandName = $input->getFirstArgument();

if ($commandName === null) {
return parent::doRun($input, $output);
}

// if paths exist or if the command name is not the first argument but with --option, eg:
// bin/rector src
// bin/rector --only "RemovePhpVersionIdCheckRector"
// file_exists() can check directory and file
if (is_string($commandName) && (
file_exists($commandName) || isset($_SERVER['argv'][1])
if ((file_exists($commandName) || isset($_SERVER['argv'][1])
&& $commandName !== $_SERVER['argv'][1]
// ensure verify has parameter option, eg: --only
&& $input->hasParameterOption($_SERVER['argv'][1])
Expand All @@ -73,6 +77,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 (! $this->has($commandName)) {
$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);
Expand Down