|
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
|
|
|
public function __construct( |
|
33
|
|
|
PhpCsFixerConfigurationInterface $configuration, |
|
34
|
|
|
ConfigurationResolverFactory $configurationResolverFactory |
|
35
|
|
|
) { |
|
36
|
|
|
$this->configuration = $configuration; |
|
37
|
|
|
$this->configurationResolverFactory = $configurationResolverFactory; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return FixerInterface[] |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getEnabledFixers() |
|
44
|
|
|
{ |
|
45
|
|
|
if ($this->enabledFixers !== []) { |
|
46
|
|
|
return $this->enabledFixers; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$configurationResolver = $this->configurationResolverFactory->create(); |
|
50
|
|
|
|
|
51
|
|
|
$this->addFixersByLevel($configurationResolver); |
|
52
|
|
|
$this->addCustomFixers($configurationResolver); |
|
53
|
|
|
|
|
54
|
|
|
return $this->enabledFixers; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function addFixersByLevel(ConfigurationResolver $configurationResolver) |
|
58
|
|
|
{ |
|
59
|
|
|
$excludedFixers = $this->getExcludedFixersAsString(); |
|
60
|
|
|
foreach ($this->configuration->getActiveFixerLevels() as $level) { |
|
61
|
|
|
$currentConfigurationResolver = clone $configurationResolver; |
|
62
|
|
|
if ($excludedFixers) { |
|
63
|
|
|
$currentConfigurationResolver->setOption('fixers', $excludedFixers); |
|
64
|
|
|
} |
|
65
|
|
|
$currentConfigurationResolver->setOption('level', $level); |
|
66
|
|
|
$currentConfigurationResolver->resolve(); |
|
67
|
|
|
|
|
68
|
|
|
$this->enabledFixers = array_merge($this->enabledFixers, $currentConfigurationResolver->getFixers()); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
private function getExcludedFixersAsString() |
|
76
|
|
|
{ |
|
77
|
|
|
$excludedFixers = ''; |
|
78
|
|
|
if (count($this->configuration->getExcludedFixers())) { |
|
79
|
|
|
$excludedFixers .= '-' . implode(',-', $this->configuration->getExcludedFixers()); |
|
80
|
|
|
return $excludedFixers; |
|
81
|
|
|
} |
|
82
|
|
|
return $excludedFixers; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
private function addCustomFixers(ConfigurationResolver $configurationResolver) |
|
89
|
|
|
{ |
|
90
|
|
|
$fixers = $this->getEnabledFixersAsString(); |
|
91
|
|
|
|
|
92
|
|
|
if ($fixers) { |
|
93
|
|
|
$currentConfigurationResolver = clone $configurationResolver; |
|
94
|
|
|
$currentConfigurationResolver->setOption('level', 'none'); |
|
95
|
|
|
$currentConfigurationResolver->setOption('fixers', $fixers); |
|
96
|
|
|
$currentConfigurationResolver->resolve(); |
|
97
|
|
|
|
|
98
|
|
|
$this->enabledFixers = array_merge($this->enabledFixers, $currentConfigurationResolver->getFixers()); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
private function getEnabledFixersAsString() |
|
106
|
|
|
{ |
|
107
|
|
|
$fixers = ''; |
|
108
|
|
|
if (count($this->configuration->getActiveFixers())) { |
|
109
|
|
|
$fixers .= implode(',', $this->configuration->getActiveFixers()); |
|
110
|
|
|
return $fixers; |
|
111
|
|
|
} |
|
112
|
|
|
return $fixers; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|