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\PHP7_CodeSniffer\Configuration; |
9
|
|
|
|
10
|
|
|
use Exception; |
11
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
12
|
|
|
use Symplify\PHP7_CodeSniffer\Exception\Configuration\InvalidSniffCodeException; |
13
|
|
|
use Symplify\PHP7_CodeSniffer\Exception\Configuration\SourceNotFoundException; |
14
|
|
|
use Symplify\PHP7_CodeSniffer\Exception\Configuration\StandardNotFoundException; |
15
|
|
|
use Symplify\PHP7_CodeSniffer\Standard\StandardFinder; |
16
|
|
|
|
17
|
|
|
final class ConfigurationResolver |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var StandardFinder |
21
|
|
|
*/ |
22
|
|
|
private $standardFinder; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var OptionsResolver |
26
|
|
|
*/ |
27
|
|
|
private $optionsResolver; |
28
|
|
|
|
29
|
2 |
|
public function __construct(StandardFinder $standardFinder) |
30
|
|
|
{ |
31
|
2 |
|
$this->standardFinder = $standardFinder; |
32
|
2 |
|
$this->createAndSetupOptionsResolver(); |
33
|
2 |
|
} |
34
|
|
|
|
35
|
2 |
|
public function resolve(array $options) : array |
36
|
|
|
{ |
37
|
2 |
|
return $this->optionsResolver->resolve($options); |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
private function createAndSetupOptionsResolver() |
41
|
|
|
{ |
42
|
2 |
|
$this->optionsResolver = new OptionsResolver(); |
43
|
2 |
|
$this->setDefaults(); |
44
|
2 |
|
$this->setRequired(); |
45
|
2 |
|
$this->setAllowedValues(); |
46
|
2 |
|
$this->setNormalizers(); |
47
|
2 |
|
} |
48
|
|
|
|
49
|
2 |
|
private function setDefaults() |
50
|
|
|
{ |
51
|
2 |
|
$this->optionsResolver->setDefaults([ |
52
|
2 |
|
'fix' => false, |
53
|
|
|
'source' => [], |
54
|
|
|
'standards' => ['PSR2'], |
55
|
|
|
'sniffs' => [] |
56
|
|
|
]); |
57
|
2 |
|
} |
58
|
|
|
|
59
|
2 |
|
private function setRequired() |
60
|
|
|
{ |
61
|
2 |
|
$this->optionsResolver->setRequired(['source', 'standards']); |
62
|
2 |
|
} |
63
|
|
|
|
64
|
2 |
|
private function setAllowedValues() |
65
|
|
|
{ |
66
|
|
|
$this->optionsResolver->setAllowedValues('standards', function (array $standards) { |
67
|
2 |
|
$standards = $this->normalizeCommaSeparatedValues($standards); |
68
|
|
|
|
69
|
2 |
|
$availableStandards = $this->standardFinder->getStandards(); |
70
|
2 |
|
foreach ($standards as $standardName) { |
71
|
2 |
|
if (!array_key_exists($standardName, $availableStandards)) { |
72
|
1 |
|
throw new StandardNotFoundException(sprintf( |
73
|
1 |
|
'Standard "%s" is not supported. Pick one of: %s', |
74
|
|
|
$standardName, |
75
|
2 |
|
implode(array_keys($availableStandards), ', ') |
76
|
|
|
)); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
return true; |
81
|
2 |
|
}); |
82
|
|
|
|
83
|
|
|
$this->optionsResolver->setAllowedValues('sniffs', function (array $sniffs) { |
84
|
1 |
|
$sniffs = $this->normalizeCommaSeparatedValues($sniffs); |
85
|
|
|
|
86
|
1 |
|
foreach ($sniffs as $sniff) { |
87
|
|
|
if (substr_count($sniff, '.') !== 2) { |
88
|
|
|
throw new InvalidSniffCodeException(sprintf( |
89
|
|
|
'The specified sniff code "%s" is invalid.'. |
90
|
|
|
PHP_EOL. |
91
|
|
|
'Correct format is "StandardName.Category.SniffName".', |
92
|
|
|
$sniff |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
return true; |
98
|
2 |
|
}); |
99
|
|
|
|
100
|
|
|
$this->optionsResolver->setAllowedValues('source', function (array $source) { |
101
|
2 |
|
foreach ($source as $singleSource) { |
102
|
|
|
if (!file_exists($singleSource)) { |
103
|
|
|
throw new SourceNotFoundException(sprintf( |
104
|
|
|
'Source "%s" does not exist.', |
105
|
|
|
$singleSource |
106
|
|
|
)); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
return true; |
111
|
2 |
|
}); |
112
|
2 |
|
} |
113
|
|
|
|
114
|
|
|
private function setNormalizers() |
115
|
|
|
{ |
116
|
2 |
|
$this->optionsResolver->setNormalizer('standards', function (OptionsResolver $optionsResolver, array $standardNames) { |
117
|
1 |
|
$standardNames = $this->normalizeCommaSeparatedValues($standardNames); |
118
|
|
|
|
119
|
1 |
|
return $this->standardFinder->getRulesetPathsForStandardNames($standardNames); |
120
|
2 |
|
}); |
121
|
2 |
|
} |
122
|
|
|
|
123
|
2 |
|
private function normalizeCommaSeparatedValues(array $values) : array |
124
|
|
|
{ |
125
|
2 |
|
$newValues = []; |
126
|
2 |
|
foreach ($values as $value) { |
127
|
2 |
|
$newValues = array_merge($newValues, explode(',', $value)); |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
return $newValues; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|