Skip to content
Merged
Show file tree
Hide file tree
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
64 changes: 64 additions & 0 deletions .github/workflows/config_option.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Config Option

# verifies https://github.com/ecsphp/ecs-src/pull/27#issuecomment-4938413629
# the "--config <file>" option must work in every position, including before the
# command name / with no command name, where the Entropy input parser would
# otherwise fail with 'Unknown option: "--config=..."'

on:
pull_request:
push:
branches:
- main

jobs:
config_option:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

-
uses: shivammathur/setup-php@v2
with:
php-version: 8.4
coverage: none

- run: composer install --no-progress --ansi

-
name: "Create a custom config in a non-default location"
run: |
mkdir -p build/nested
printf '<?php $%s=1;\n' 'x' > build/nested/some_file.php
cat > build/custom-ecs.php <<'PHP'
<?php
declare(strict_types=1);
return \Symplify\EasyCodingStandard\Config\ECSConfig::configure()
->withPaths([__DIR__ . '/nested'])
->withPreparedSets(psr12: true);
PHP

-
name: "ecs --config=<file> (leading option, default command) must load the custom config"
run: |
OUTPUT="$(bin/ecs --config=build/custom-ecs.php --no-progress-bar --ansi || true)"
echo "$OUTPUT"
echo "$OUTPUT" | grep -q 'some_file.php'
! echo "$OUTPUT" | grep -q 'Unknown option'

-
name: "ecs --config <file> (space-separated, leading option) must load the custom config"
run: |
OUTPUT="$(bin/ecs --config build/custom-ecs.php --no-progress-bar --ansi || true)"
echo "$OUTPUT"
echo "$OUTPUT" | grep -q 'some_file.php'
! echo "$OUTPUT" | grep -q 'Unknown option'

-
name: "ecs check --config=<file> (option after command) must load the custom config"
run: |
OUTPUT="$(bin/ecs check --config=build/custom-ecs.php --no-progress-bar --ansi || true)"
echo "$OUTPUT"
echo "$OUTPUT" | grep -q 'some_file.php'
! echo "$OUTPUT" | grep -q 'Unknown option'
11 changes: 11 additions & 0 deletions bin/ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ public function loadIfNotLoadedYet(string $file): void
* the "-c" config shortcut to "--config", so the Entropy input parser does not
* treat them as unknown command options.
*
* When the first argument is an option (e.g. "ecs --config=ecs.php"), inject the
* default "check" command, so the Entropy input parser does not mistake the leading
* "--config=<file>" for an unknown boolean flag and fail with "Unknown option".
*
* @param string[] $argv
* @return string[]
*/
Expand Down Expand Up @@ -227,5 +231,12 @@ function ecs_normalize_argv(array $argv): array
$normalized[] = $arg;
}

// $normalized[0] is the script name; the first real argument sits at index 1.
// Keep "-h"/"--help" untouched, so it still prints the global command list.
$helpFlags = ['-h', '--help'];
if (isset($normalized[1]) && str_starts_with($normalized[1], '-') && ! in_array($normalized[1], $helpFlags, true)) {
array_splice($normalized, 1, 0, 'check');
}

return $normalized;
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"phpstan/phpstan-webmozart-assert": "^2.0",
"phpunit/phpunit": "^13.2",
"rector/jack": "^1.0",
"rector/rector": "^2.4",
"rector/rector": "^2.5",
"rector/type-perfect": "^2.1",
"symplify/phpstan-rules": "^14.12",
"symplify/vendor-patches": "^11.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public function isCandidate(Tokens $tokens): bool
continue;
}

if (! (isset($tokens[$index + 3]) && $tokens[$index + 3]->getContent() === ')')) {
if (! isset($tokens[$index + 3])) {
continue;
}

if ($tokens[$index + 3]->getContent() !== ')') {
continue;
}

Expand Down
Loading