-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathFixCode.php
More file actions
97 lines (84 loc) · 3.38 KB
/
FixCode.php
File metadata and controls
97 lines (84 loc) · 3.38 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
<?php
namespace App\Actions;
use App\Factories\ConfigurationResolverFactory;
use LaravelZero\Framework\Exceptions\ConsoleException;
use PhpCsFixer\Console\ConfigurationResolver;
use PhpCsFixer\Differ\NullDiffer;
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'))) {
$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(),
$resolver->getParallelConfig(),
$this->getInput($resolver),
));
return tap([$totalFiles, $changes], fn () => $this->progress->unsubscribe());
}
/**
* Gets 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');
$this->input->setOption('diff', ! $resolver->getDiffer() instanceof NullDiffer);
return $this->input;
}
}