|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Symplify |
|
5
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Symplify\MultiCodingStandard\PhpCsFixer\Factory; |
|
9
|
|
|
|
|
10
|
|
|
use Symfony\CS\ConfigurationResolver; |
|
11
|
|
|
|
|
12
|
|
|
final class FixerResolver |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var ConfigurationResolver |
|
16
|
|
|
*/ |
|
17
|
|
|
private $configurationResolver; |
|
18
|
|
|
|
|
19
|
7 |
|
public function __construct(ConfigurationResolver $configurationResolver) |
|
20
|
|
|
{ |
|
21
|
7 |
|
$this->configurationResolver = $configurationResolver; |
|
22
|
7 |
|
} |
|
23
|
|
|
|
|
24
|
6 |
|
public function resolveFixersByLevelAndExcludedFixers(array $fixerLevels, array $excludedFixers) : array |
|
25
|
|
|
{ |
|
26
|
6 |
|
if (!count($fixerLevels)) { |
|
27
|
1 |
|
return []; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
5 |
|
$fixers = []; |
|
31
|
5 |
|
foreach ($fixerLevels as $fixerLevel) { |
|
32
|
5 |
|
$currentConfigurationResolver = clone $this->configurationResolver; |
|
33
|
|
|
|
|
34
|
5 |
|
$excludedFixersAsString = $this->turnExcludedFixersToString($excludedFixers); |
|
35
|
5 |
|
$currentConfigurationResolver->setOption('level', $fixerLevel); |
|
36
|
5 |
|
$currentConfigurationResolver->setOption('fixers', $excludedFixersAsString); |
|
37
|
5 |
|
$currentConfigurationResolver->resolve(); |
|
38
|
|
|
|
|
39
|
5 |
|
$fixers = array_merge($fixers, $currentConfigurationResolver->getFixers()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
5 |
|
return $fixers; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function resolveFixers(array $fixers) : array |
|
46
|
|
|
{ |
|
47
|
1 |
|
if (!count($fixers)) { |
|
48
|
1 |
|
return []; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
$fixersAsString = $this->turnFixersToString($fixers); |
|
52
|
|
|
|
|
53
|
1 |
|
$currentConfigurationResolver = clone $this->configurationResolver; |
|
54
|
1 |
|
$currentConfigurationResolver->setOption('level', 'none'); |
|
55
|
1 |
|
$currentConfigurationResolver->setOption('fixers', $fixersAsString); |
|
56
|
1 |
|
$currentConfigurationResolver->resolve(); |
|
57
|
|
|
|
|
58
|
1 |
|
return $currentConfigurationResolver->getFixers(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
private function turnFixersToString(array $fixers) : string |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->implodeWithPresign($fixers); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
5 |
|
private function turnExcludedFixersToString(array $excludedFixers) : string |
|
67
|
|
|
{ |
|
68
|
5 |
|
return $this->implodeWithPresign($excludedFixers, '-'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
6 |
|
private function implodeWithPresign(array $items, string $presign = '') |
|
72
|
|
|
{ |
|
73
|
6 |
|
if (count($items)) { |
|
74
|
3 |
|
return $presign . implode(',' . $presign, $items); |
|
75
|
|
|
} |
|
76
|
3 |
|
return ''; |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|