/
CodeStyler.php
90 lines (72 loc) · 2.08 KB
/
CodeStyler.php
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
<?php
declare(strict_types=1);
namespace DragonCode\CodeStyler\Processors;
use DragonCode\CodeStyler\Support\PhpVersion;
use DragonCode\Support\Facades\Helpers\Arr;
use PhpCsFixer\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
abstract class CodeStyler extends BaseProcessor
{
protected array $options = [
'path' => __DIR__,
'fix' => true,
];
protected array $options_check = [];
public function run(): void
{
$this->styler();
}
protected function styler(): void
{
$application = new Application();
$application->run($this->getArgv());
}
protected function getArgv(): ArgvInput
{
return new ArgvInput(
$this->resolveOptions()
);
}
protected function resolveOptions(): array
{
return Arr::of($this->getOptions())
->map(function ($value, string $key)
{
if (is_bool($value)) {
return $key;
}
return sprintf('%s=%s', $key, $this->resolvePath($value));
})
->values()
->toArray();
}
protected function resolvePath(mixed $value): mixed
{
if (is_string($value) && file_exists($value)) {
return realpath($value);
}
return $value;
}
protected function getOptions(): array
{
return array_merge($this->options, $this->options_check, [
'--config' => $this->getConfigFilename(),
], $this->getDecorationOption());
}
protected function getDecorationOption(): array
{
return $this->output->isDecorated()
? ['--ansi' => true]
: ['--no-ansi' => true];
}
protected function getConfigFilename(): string
{
$path = __DIR__ . '/../../rules/';
$config = $path . $this->getPhpVersion() . '.php';
return file_exists($config) ? $config : $path . PhpVersion::DEFAULT . '.php';
}
protected function getPhpVersion(): string
{
return PhpVersion::make()->get();
}
}