Skip to content

Commit a22afd1

Browse files
[DX] Define list of allowed implicit commands on ConsoleApplication (#7918)
* [DX] Define list of allowed implicit commands on ConsoleApplication * [ci-review] Rector Rectify * fix null command * define array docblock * fix exit code * final touch: early return on null first argument * refactor to use ->has() method --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent a68a80e commit a22afd1

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/Console/ConsoleApplication.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Input\InputOption;
1818
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\Console\Style\SymfonyStyle;
1920
use Webmozart\Assert\Assert;
2021

2122
final class ConsoleApplication extends Application
@@ -25,7 +26,7 @@ final class ConsoleApplication extends Application
2526
/**
2627
* @param Command[] $commands
2728
*/
28-
public function __construct(array $commands)
29+
public function __construct(array $commands, private readonly SymfonyStyle $symfonyStyle)
2930
{
3031
parent::__construct(self::NAME, VersionResolver::PACKAGE_VERSION);
3132

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

5859
$commandName = $input->getFirstArgument();
5960

61+
if ($commandName === null) {
62+
return parent::doRun($input, $output);
63+
}
64+
6065
// if paths exist or if the command name is not the first argument but with --option, eg:
6166
// bin/rector src
6267
// bin/rector --only "RemovePhpVersionIdCheckRector"
6368
// file_exists() can check directory and file
64-
if (is_string($commandName) && (
65-
file_exists($commandName) || isset($_SERVER['argv'][1])
69+
if ((file_exists($commandName) || isset($_SERVER['argv'][1])
6670
&& $commandName !== $_SERVER['argv'][1]
6771
// ensure verify has parameter option, eg: --only
6872
&& $input->hasParameterOption($_SERVER['argv'][1])
@@ -73,6 +77,16 @@ public function doRun(InputInterface $input, OutputInterface $output): int
7377
$tokens = $privatesAccessor->getPrivateProperty($input, 'tokens');
7478
$tokens = array_merge(['process'], $tokens);
7579
$privatesAccessor->setPrivateProperty($input, 'tokens', $tokens);
80+
} elseif (! $this->has($commandName)) {
81+
$this->symfonyStyle->error(
82+
sprintf(
83+
'The following given path does not match any files or directories: %s%s',
84+
"\n\n - ",
85+
$commandName
86+
)
87+
);
88+
89+
return ExitCode::FAILURE;
7690
}
7791

7892
return parent::doRun($input, $output);

0 commit comments

Comments
 (0)