Skip to content

Commit 9e92922

Browse files
[Console] Handle hidden "process" with --option without = on ConsoleApplication (#7022)
* [Console] Handle hidden "process" with --option without = on ConsoleApplication * [ci-review] Rector Rectify * final touch: ensure verify has parameter option * final touch: ensure verify has parameter option * Add CI test for command with vs without process with = and not * move to specific yaml file * sync with other e2e * sync with other e2e * add test with php prefix * final touch: eol * less output * less output * with php prefix * reduce time for only configured test --------- Co-authored-by: GitHub Action <[email protected]>
1 parent a37dcf1 commit 9e92922

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: End to End tests command with option
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
push:
8+
branches:
9+
- main
10+
env:
11+
# see https://github.com/composer/composer/issues/9368#issuecomment-718112361
12+
COMPOSER_ROOT_VERSION: "dev-main"
13+
14+
jobs:
15+
code_analysis:
16+
strategy:
17+
fail-fast: false
18+
19+
name: End to End tests command with option
20+
runs-on: ubuntu-latest
21+
timeout-minutes: 10
22+
23+
steps:
24+
- uses: actions/checkout@v4
25+
26+
# see https://github.com/shivammathur/setup-php
27+
-
28+
uses: shivammathur/setup-php@v2
29+
with:
30+
php-version: 8.2
31+
coverage: none
32+
33+
- uses: "ramsey/composer-install@v3"
34+
35+
# test various commands with options
36+
- working-directory: e2e/command-with-option
37+
run: |
38+
# other command
39+
../../bin/rector help
40+
php ../../bin/rector help
41+
42+
# with explicit "process" command
43+
../../bin/rector process --kaizen 1 --dry-run
44+
../../bin/rector process --kaizen=1 --dry-run
45+
php ../../bin/rector process --kaizen 1 --dry-run
46+
php ../../bin/rector process --kaizen=1 --dry-run
47+
../../bin/rector process some_file.php --kaizen 1
48+
../../bin/rector process some_file.php --kaizen=1
49+
php ../../bin/rector process some_file.php --kaizen 1
50+
php ../../bin/rector process some_file.php --kaizen=1
51+
52+
# with implicit process command
53+
../../bin/rector --kaizen 1 --dry-run
54+
../../bin/rector --kaizen=1 --dry-run
55+
php ../../bin/rector --kaizen 1 --dry-run
56+
php ../../bin/rector --kaizen=1 --dry-run
57+
58+
../../bin/rector some_file.php --kaizen 1
59+
../../bin/rector some_file.php --kaizen=1
60+
php ../../bin/rector some_file.php --kaizen 1
61+
php ../../bin/rector some_file.php --kaizen=1
62+

e2e/command-with-option/rector.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
7+
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
8+
9+
return RectorConfig::configure()
10+
->withPaths([
11+
__DIR__ . '/some_file.php'
12+
])
13+
->withRules([
14+
StringClassNameToClassConstantRector::class,
15+
RemovePhpVersionIdCheckRector::class,
16+
]);

e2e/command-with-option/some_file.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
echo "some content";

src/Console/ConsoleApplication.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,23 @@ public function doRun(InputInterface $input, OutputInterface $output): int
6464

6565
$commandName = $input->getFirstArgument();
6666

67-
// if paths exist
68-
if (is_string($commandName) && file_exists($commandName)) {
67+
// if paths exist or if the command name is not the first argument but with --option, eg:
68+
// bin/rector src
69+
// bin/rector --only "RemovePhpVersionIdCheckRector"
70+
// file_exists() can check directory and file
71+
if (is_string($commandName)
72+
&& (
73+
file_exists($commandName)
74+
|| isset($_SERVER['argv'][1])
75+
&& $commandName !== $_SERVER['argv'][1]
76+
// ensure verify has parameter option, eg: --only
77+
&& $input->hasParameterOption($_SERVER['argv'][1])
78+
)
79+
) {
6980
// prepend command name if implicit
7081
$privatesAccessor = new PrivatesAccessor();
7182
$tokens = $privatesAccessor->getPrivateProperty($input, 'tokens');
7283
$tokens = array_merge(['process'], $tokens);
73-
7484
$privatesAccessor->setPrivateProperty($input, 'tokens', $tokens);
7585
}
7686

0 commit comments

Comments
 (0)