|
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\OptionResolver; |
|
9
|
|
|
|
|
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
11
|
|
|
use Symplify\PHP7_CodeSniffer\Configuration\ValueNormalizer; |
|
12
|
|
|
use Symplify\PHP7_CodeSniffer\Contract\Configuration\OptionResolver\OptionResolverInterface; |
|
13
|
|
|
use Symplify\PHP7_CodeSniffer\Exception\Configuration\StandardNotFoundException; |
|
14
|
|
|
use Symplify\PHP7_CodeSniffer\Standard\StandardFinder; |
|
15
|
|
|
|
|
16
|
|
|
final class StandardsOptionResolver implements OptionResolverInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
const NAME = 'standards'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var StandardFinder |
|
25
|
|
|
*/ |
|
26
|
|
|
private $standardFinder; |
|
27
|
|
|
|
|
28
|
2 |
|
public function __construct(StandardFinder $standardFinder) |
|
29
|
|
|
{ |
|
30
|
2 |
|
$this->standardFinder = $standardFinder; |
|
31
|
2 |
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
public function getName() : string |
|
34
|
|
|
{ |
|
35
|
2 |
|
return self::NAME; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
2 |
|
public function resolve(array $value) : array |
|
39
|
|
|
{ |
|
40
|
2 |
|
$optionsResolver = new OptionsResolver(); |
|
41
|
2 |
|
$optionsResolver->setDefined(self::NAME); |
|
42
|
2 |
|
$this->setNormalizers($optionsResolver); |
|
43
|
2 |
|
$this->setAllowedValues($optionsResolver); |
|
44
|
|
|
|
|
45
|
2 |
|
$values = $optionsResolver->resolve([ |
|
46
|
2 |
|
self::NAME => $value |
|
47
|
|
|
]); |
|
48
|
|
|
|
|
49
|
1 |
|
return $values[self::NAME]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
private function setNormalizers(OptionsResolver $optionsResolver) |
|
53
|
|
|
{ |
|
54
|
2 |
|
$optionsResolver->setNormalizer( |
|
55
|
2 |
|
self::NAME, |
|
56
|
|
|
function (OptionsResolver $optionsResolver, array $standardNames) { |
|
57
|
1 |
|
$standardNames = ValueNormalizer::normalizeCommaSeparatedValues($standardNames); |
|
58
|
1 |
|
return $this->standardFinder->getRulesetPathsForStandardNames($standardNames); |
|
59
|
2 |
|
} |
|
60
|
|
|
); |
|
61
|
2 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function setAllowedValues(OptionsResolver $optionsResolver) |
|
64
|
|
|
{ |
|
65
|
2 |
|
$optionsResolver->setAllowedValues(self::NAME, function (array $standards) { |
|
66
|
2 |
|
$standards = ValueNormalizer::normalizeCommaSeparatedValues($standards); |
|
67
|
|
|
|
|
68
|
2 |
|
$availableStandards = $this->standardFinder->getStandards(); |
|
69
|
2 |
|
foreach ($standards as $standardName) { |
|
70
|
2 |
|
if (!array_key_exists($standardName, $availableStandards)) { |
|
71
|
1 |
|
throw new StandardNotFoundException(sprintf( |
|
72
|
1 |
|
'Standard "%s" is not supported. Pick one of: %s.', |
|
73
|
|
|
$standardName, |
|
74
|
2 |
|
implode(array_keys($availableStandards), ', ') |
|
75
|
|
|
)); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
return true; |
|
80
|
2 |
|
}); |
|
81
|
2 |
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|