Skip to content
Draft
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
69 changes: 69 additions & 0 deletions app/Support/PhpCsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use PhpCsFixer\ToolInfo;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\Process;

class PhpCsFixer extends Tool
{
Expand Down Expand Up @@ -44,6 +46,10 @@ public function fix(): int

private function process(): int
{
if ($this->hasProjectBinary() && $this->hasProjectConfig()) {
return $this->processViaProjectBinary();
}

$output = app()->get(OutputInterface::class);

$resolver = new ConfigurationResolver(
Expand Down Expand Up @@ -83,6 +89,69 @@ private function process(): int
return app()->get(ElaborateSummary::class)->execute($totalFiles, $changes);
}

private function hasProjectBinary(): bool
{
return file_exists(Project::path() . '/vendor/bin/php-cs-fixer');
}

private function hasProjectConfig(): bool
{
$configPath = $this->getConfigFilePath();

return str_starts_with($configPath, Project::path())
&& ! str_contains($configPath, 'standards/');
}

private function processViaProjectBinary(): int
{
$output = app()->get(OutputInterface::class);

$command = [
Project::path() . '/vendor/bin/php-cs-fixer',
'fix',
'--config=' . $this->getConfigFilePath(),
'--allow-risky=yes',
'--show-progress=dots',
'--path-mode=override',
];

if ($this->dusterConfig->get('mode') === 'lint') {
$command[] = '--dry-run';
}

if ($output->isVerbose()) {
$command[] = '--diff';
}

if ($output->isDebug()) {
$command[] = '-vvv';
} elseif ($output->isVeryVerbose()) {
$command[] = '-vv';
} elseif ($output->isVerbose()) {
$command[] = '-v';
} elseif ($output->isQuiet()) {
$command[] = '--quiet';
}

foreach ($this->dusterConfig->get('paths', []) as $path) {
$command[] = $path;
}

$dusterConfig = DusterConfig::loadLocal();
$process = new Process($command, Project::path());
$process->setTimeout($dusterConfig['processTimeout'] ?? 60);

try {
$process->run(fn ($type, $buffer) => $output->write($buffer));

return $process->getExitCode();
} catch (ProcessTimedOutException $e) {
$this->failure($e->getMessage() . '<br />You can overwrite this timeout with the processTimeout key in your duster.json file.');

return 1;
}
}

private function getConfig(): ConfigInterface
{
$config = $this->includeConfig();
Expand Down