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; |
9
|
|
|
|
10
|
|
|
use Symfony\CS\ConfigurationResolver; |
11
|
|
|
use Symfony\CS\Fixer; |
12
|
|
|
use Symfony\CS\FixerInterface; |
13
|
|
|
use Symplify\MultiCodingStandard\Contract\Configuration\PhpCsFixerConfigurationInterface; |
14
|
|
|
|
15
|
|
|
final class EnabledFixerResolver |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var PhpCsFixerConfigurationInterface |
19
|
|
|
*/ |
20
|
|
|
private $configuration; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ConfigurationResolverFactory |
24
|
|
|
*/ |
25
|
|
|
private $configurationResolverFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var FixerInterface[] |
29
|
|
|
*/ |
30
|
|
|
private $enabledFixers = []; |
31
|
|
|
|
32
|
1 |
|
public function __construct( |
33
|
|
|
PhpCsFixerConfigurationInterface $configuration, |
34
|
|
|
ConfigurationResolverFactory $configurationResolverFactory |
35
|
|
|
) { |
36
|
1 |
|
$this->configuration = $configuration; |
37
|
1 |
|
$this->configurationResolverFactory = $configurationResolverFactory; |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return FixerInterface[] |
42
|
|
|
*/ |
43
|
1 |
|
public function getEnabledFixers() |
44
|
|
|
{ |
45
|
1 |
|
if ($this->enabledFixers !== []) { |
46
|
|
|
return $this->enabledFixers; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
$configurationResolver = $this->configurationResolverFactory->create(); |
50
|
|
|
|
51
|
1 |
|
$this->addFixersByLevel($configurationResolver); |
52
|
1 |
|
$this->addCustomFixers($configurationResolver); |
53
|
|
|
|
54
|
1 |
|
return $this->enabledFixers; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
private function addFixersByLevel(ConfigurationResolver $configurationResolver) |
58
|
|
|
{ |
59
|
1 |
|
$excludedFixers = $this->getExcludedFixersAsString(); |
60
|
1 |
|
foreach ($this->configuration->getActiveFixerLevels() as $level) { |
61
|
1 |
|
$currentConfigurationResolver = clone $configurationResolver; |
62
|
1 |
|
if ($excludedFixers) { |
63
|
1 |
|
$currentConfigurationResolver->setOption('fixers', $excludedFixers); |
64
|
|
|
} |
65
|
1 |
|
$currentConfigurationResolver->setOption('level', $level); |
66
|
1 |
|
$currentConfigurationResolver->resolve(); |
67
|
|
|
|
68
|
1 |
|
$this->enabledFixers = array_merge($this->enabledFixers, $currentConfigurationResolver->getFixers()); |
69
|
|
|
} |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
1 |
|
private function getExcludedFixersAsString() |
76
|
|
|
{ |
77
|
1 |
|
$excludedFixers = ''; |
78
|
1 |
|
if (count($this->configuration->getExcludedFixers())) { |
79
|
1 |
|
$excludedFixers .= '-' . implode(',-', $this->configuration->getExcludedFixers()); |
80
|
1 |
|
return $excludedFixers; |
81
|
|
|
} |
82
|
|
|
return $excludedFixers; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
1 |
|
private function addCustomFixers(ConfigurationResolver $configurationResolver) |
89
|
|
|
{ |
90
|
1 |
|
$fixers = $this->getEnabledFixersAsString(); |
91
|
|
|
|
92
|
1 |
|
if ($fixers) { |
93
|
1 |
|
$currentConfigurationResolver = clone $configurationResolver; |
94
|
1 |
|
$currentConfigurationResolver->setOption('level', 'none'); |
95
|
1 |
|
$currentConfigurationResolver->setOption('fixers', $fixers); |
96
|
1 |
|
$currentConfigurationResolver->resolve(); |
97
|
|
|
|
98
|
1 |
|
$this->enabledFixers = array_merge($this->enabledFixers, $currentConfigurationResolver->getFixers()); |
99
|
|
|
} |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
1 |
|
private function getEnabledFixersAsString() |
106
|
|
|
{ |
107
|
1 |
|
$fixers = ''; |
108
|
1 |
|
if (count($this->configuration->getActiveFixers())) { |
109
|
1 |
|
$fixers .= implode(',', $this->configuration->getActiveFixers()); |
110
|
1 |
|
return $fixers; |
111
|
|
|
} |
112
|
|
|
return $fixers; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|