Skip to content

Commit 2e8b27b

Browse files
committed
Make it possible for PHP CS Fixer and PHPStan to have completely custom commands
1 parent 7aa2ebb commit 2e8b27b

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/Action/PHPCSFixer.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function count;
1313
use function escapeshellarg;
1414
use function preg_match;
15+
use function str_replace;
1516

1617
final class PHPCSFixer extends Action
1718
{
@@ -70,11 +71,23 @@ protected function shouldSkipFileCheck(string $file, array $excludedFiles): bool
7071
protected function fixFile(string $file, Config $config, Config\Action $action): array
7172
{
7273
$process = new Processor();
73-
$result = $process->run($config->getPhpPath() . ' ' . $action->getOptions()->get('fixer_path') . ' fix --dry-run --diff ' . escapeshellarg($file));
74+
75+
$result = $process->run($this->getCommand($file, $config, $action));
7476

7577
return [
7678
'success' => $result->isSuccessful(),
7779
'output' => $result->getStdOut(),
7880
];
7981
}
82+
83+
protected function getCommand(string $file, Config $config, Config\Action $action): string
84+
{
85+
$command = $action->getOptions()->get('command');
86+
87+
if ($command !== null) {
88+
return str_replace('%file%', escapeshellarg($file), $command);
89+
}
90+
91+
return $config->getPhpPath() . ' ' . $action->getOptions()->get('fixer_path') . ' fix --dry-run --diff ' . escapeshellarg($file);
92+
}
8093
}

src/Action/PHPStan.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function count;
1313
use function escapeshellarg;
1414
use function preg_match;
15+
use function str_replace;
1516

1617
final class PHPStan extends Action
1718
{
@@ -70,13 +71,26 @@ protected function shouldSkipFileCheck(string $file, array $excludedFiles): bool
7071
protected function analyzeFile(string $file, Config $config, Config\Action $action): array
7172
{
7273
$process = new Processor();
73-
$phpstanPath = $action->getOptions()->get('phpstan_path') ?? 'vendor/bin/phpstan';
74-
$level = $action->getOptions()->get('level') ?? 8;
75-
$result = $process->run($config->getPhpPath() . ' ' . $phpstanPath . ' analyse --level=' . $level . ' ' . escapeshellarg($file));
74+
75+
$result = $process->run($this->getCommand($file, $config, $action));
7676

7777
return [
7878
'success' => $result->isSuccessful(),
7979
'output' => $result->getStdOut(),
8080
];
8181
}
82+
83+
protected function getCommand(string $file, Config $config, Config\Action $action): string
84+
{
85+
$command = $action->getOptions()->get('command');
86+
87+
if ($command !== null) {
88+
return str_replace('%file%', escapeshellarg($file), $command);
89+
}
90+
91+
$phpstanPath = $action->getOptions()->get('phpstan_path') ?? 'vendor/bin/phpstan';
92+
$level = $action->getOptions()->get('level') ?? 8;
93+
94+
return $config->getPhpPath() . ' ' . $phpstanPath . ' analyse --level=' . $level . ' ' . escapeshellarg($file);
95+
}
8296
}

0 commit comments

Comments
 (0)