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
|
|
|
use Symplify\MultiCodingStandard\Contract\PhpCsFixer\FileSystem\FixerFileSystemInterface; |
15
|
|
|
|
16
|
|
|
final class EnabledFixerResolver |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var PhpCsFixerConfigurationInterface |
20
|
|
|
*/ |
21
|
|
|
private $configuration; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var FixerFileSystemInterface |
25
|
|
|
*/ |
26
|
|
|
private $fixerFileSystem; |
27
|
|
|
|
28
|
1 |
|
public function __construct( |
29
|
|
|
PhpCsFixerConfigurationInterface $configuration, |
30
|
|
|
FixerFileSystemInterface $fixerFileSystem |
31
|
|
|
) { |
32
|
1 |
|
$this->configuration = $configuration; |
33
|
1 |
|
$this->fixerFileSystem = $fixerFileSystem; |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return FixerInterface[] |
38
|
|
|
*/ |
39
|
1 |
|
public function getEnabledFixers() |
40
|
|
|
{ |
41
|
1 |
|
$allFixerFiles = $this->fixerFileSystem->findAllFixers(); |
42
|
1 |
|
$allFixerObjects = $this->createObjectsFromFixerFiles($allFixerFiles); |
43
|
|
|
|
44
|
1 |
|
$configurationResolver = new ConfigurationResolver(); |
45
|
1 |
|
$configurationResolver->setAllFixers($allFixerObjects); |
46
|
|
|
|
47
|
|
|
// 2. filter fixers in by level |
48
|
1 |
|
$finalFixersToBeRegistered = []; |
49
|
|
|
|
50
|
|
|
// filter out custom ones |
51
|
1 |
|
$excludedFixers = ''; |
52
|
1 |
|
if (count($this->configuration->getExcludedFixers())) { |
53
|
1 |
|
$excludedFixers .= '-'.implode(',-', $this->configuration->getExcludedFixers()); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
foreach ($this->configuration->getActiveFixerLevels() as $level) { |
57
|
1 |
|
$currentConfigurationResolver = clone $configurationResolver; |
58
|
1 |
|
if ($excludedFixers) { |
59
|
1 |
|
$currentConfigurationResolver->setOption('fixers', $excludedFixers); |
60
|
|
|
} |
61
|
1 |
|
$currentConfigurationResolver->setOption('level', $level); |
62
|
1 |
|
$currentConfigurationResolver->resolve(); |
63
|
|
|
|
64
|
1 |
|
$finalFixersToBeRegistered += $currentConfigurationResolver->getFixers(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// 3. filter in custom ones |
68
|
1 |
|
$fixers = ''; |
69
|
1 |
|
if (count($this->configuration->getActiveFixers())) { |
70
|
1 |
|
$fixers .= implode(',', $this->configuration->getActiveFixers()); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
if ($fixers) { |
74
|
1 |
|
$currentConfigurationResolver = clone $configurationResolver; |
75
|
1 |
|
$currentConfigurationResolver->setOption('level', 'none'); |
76
|
1 |
|
$currentConfigurationResolver->setOption('fixers', $fixers); |
77
|
1 |
|
$currentConfigurationResolver->resolve(); |
78
|
|
|
|
79
|
1 |
|
$finalFixersToBeRegistered = array_merge( |
80
|
|
|
$finalFixersToBeRegistered, |
81
|
1 |
|
$currentConfigurationResolver->getFixers() |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return $finalFixersToBeRegistered; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return object[] |
90
|
|
|
*/ |
91
|
1 |
|
private function createObjectsFromFixerFiles(array $fixerFiles) |
92
|
|
|
{ |
93
|
1 |
|
$fixerObjects = []; |
94
|
1 |
|
foreach ($fixerFiles as $file) { |
95
|
1 |
|
$relativeNamespace = $file->getRelativePath(); |
96
|
1 |
|
$class = 'Symfony\\CS\\Fixer\\' . ($relativeNamespace ? $relativeNamespace . '\\' : '') . $file->getBasename('.php'); |
97
|
1 |
|
$fixerObjects[] = new $class; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
return $fixerObjects; |
101
|
|
|
} |
102
|
|
|
} |