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\OptionResolver\StandardNotFoundException; |
14
|
|
|
use Symplify\PHP7_CodeSniffer\Standard\Finder\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
|
5 |
|
public function __construct(StandardFinder $standardFinder) |
29
|
|
|
{ |
30
|
5 |
|
$this->standardFinder = $standardFinder; |
31
|
5 |
|
} |
32
|
|
|
|
33
|
5 |
|
public function getName() : string |
34
|
|
|
{ |
35
|
5 |
|
return self::NAME; |
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
View Code Duplication |
public function resolve(array $value) : array |
|
|
|
|
39
|
|
|
{ |
40
|
3 |
|
$optionsResolver = new OptionsResolver(); |
41
|
3 |
|
$optionsResolver->setDefined(self::NAME); |
42
|
3 |
|
$this->setAllowedValues($optionsResolver); |
43
|
3 |
|
$this->setNormalizer($optionsResolver); |
44
|
|
|
|
45
|
3 |
|
$values = $optionsResolver->resolve([ |
46
|
3 |
|
self::NAME => $value |
47
|
|
|
]); |
48
|
|
|
|
49
|
2 |
|
return $values[self::NAME]; |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
private function setNormalizer(OptionsResolver $optionsResolver) |
53
|
|
|
{ |
54
|
3 |
|
$optionsResolver->setNormalizer( |
55
|
3 |
|
self::NAME, |
56
|
|
|
function (OptionsResolver $optionsResolver, array $standardNames) { |
57
|
2 |
|
return ValueNormalizer::normalizeCommaSeparatedValues($standardNames); |
58
|
3 |
|
} |
59
|
|
|
); |
60
|
3 |
|
} |
61
|
|
|
|
62
|
|
|
private function setAllowedValues(OptionsResolver $optionsResolver) |
63
|
|
|
{ |
64
|
3 |
|
$optionsResolver->setAllowedValues(self::NAME, function (array $standards) { |
65
|
3 |
|
$standards = ValueNormalizer::normalizeCommaSeparatedValues($standards); |
66
|
|
|
|
67
|
3 |
|
$availableStandards = $this->standardFinder->getStandards(); |
68
|
3 |
|
foreach ($standards as $standardName) { |
69
|
3 |
|
if (!array_key_exists($standardName, $availableStandards)) { |
70
|
1 |
|
throw new StandardNotFoundException(sprintf( |
71
|
1 |
|
'Standard "%s" is not supported. Pick one of: %s.', |
72
|
|
|
$standardName, |
73
|
3 |
|
implode(array_keys($availableStandards), ', ') |
74
|
|
|
)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
return true; |
79
|
3 |
|
}); |
80
|
3 |
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.