forked from laravel/pint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixCode.php
More file actions
118 lines (101 loc) · 4.09 KB
/
FixCode.php
File metadata and controls
118 lines (101 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace App\Actions;
use App\Factories\ConfigurationResolverFactory;
use LaravelZero\Framework\Exceptions\ConsoleException;
use PhpCsFixer\Console\ConfigurationResolver;
use PhpCsFixer\Runner\Parallel\ParallelConfig;
use PhpCsFixer\Runner\Runner;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
class FixCode
{
/**
* Creates a new Fix Code instance.
*
* @param \PhpCsFixer\Error\ErrorsManager $errors
* @param \Symfony\Component\EventDispatcher\EventDispatcher $events
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param \App\Output\ProgressOutput $progress
* @return void
*/
public function __construct(
protected $errors,
protected $events,
protected $input,
protected $output,
protected $progress,
) {
//
}
/**
* Fixes the project resolved by the current input and output.
*
* @return array{int, array<string, array{appliedFixers: array<int, string>, diff: string}>}
*/
public function execute()
{
try {
[$resolver, $totalFiles] = ConfigurationResolverFactory::fromIO($this->input, $this->output);
} catch (ConsoleException $exception) {
return [$exception->getCode(), []];
}
if (is_null($this->input->getOption('format')) && ! ConfigurationResolverFactory::runningInAgent()) {
$this->progress->subscribe();
}
$method = $this->input->getOption('parallel') ? 'fixParallel' : 'fixSequential';
/** @var array<string, array{appliedFixers: array<int, string>, diff: string}> $changes */
$changes = (fn () => $this->{$method}())->call(new Runner(
$resolver->getFinder(),
$resolver->getFixers(),
$resolver->getDiffer(),
$this->events,
$this->errors,
$resolver->getLinter(),
$resolver->isDryRun(),
$resolver->getCacheManager(),
$resolver->getDirectory(),
$resolver->shouldStopOnViolation(),
$this->getParallelConfig($resolver),
$this->getInput($resolver),
));
return tap([$totalFiles, $changes], fn () => $this->progress->unsubscribe());
}
/**
* Get the ParallelConfig for the number of cores.
*/
private function getParallelConfig(ConfigurationResolver $resolver): ParallelConfig
{
$maxProcesses = intval($this->input->getOption('max-processes') ?? 0);
if (! $this->input->getOption('parallel') || $maxProcesses < 1) {
return $resolver->getParallelConfig();
}
$parallelConfig = $resolver->getParallelConfig();
return new ParallelConfig(
$maxProcesses,
$parallelConfig->getFilesPerProcess(),
$parallelConfig->getProcessTimeout()
);
}
/**
* Get the input for the PHP CS Fixer Runner.
*/
private function getInput(ConfigurationResolver $resolver): InputInterface
{
// @phpstan-ignore-next-line
$definition = (fn () => $this->definition)->call($this->input);
$definition->addOptions([
new InputOption('stop-on-violation', null, InputOption::VALUE_REQUIRED, ''),
new InputOption('allow-risky', null, InputOption::VALUE_REQUIRED, ''),
new InputOption('rules', null, InputOption::VALUE_REQUIRED, ''),
new InputOption('using-cache', null, InputOption::VALUE_REQUIRED, ''),
]);
$this->input->setOption('stop-on-violation', $resolver->shouldStopOnViolation());
$this->input->setOption('allow-risky', $resolver->getRiskyAllowed() ? 'yes' : 'no');
$this->input->setOption('rules', json_encode($resolver->getRules()));
$this->input->setOption('using-cache', $resolver->getUsingCache() ? 'yes' : 'no');
// Remove config option so it doesn't get passed to php-cs-fixer...
$this->input->setOption('config', null);
return $this->input;
}
}