Completed
Branch master (06cb84)
by Tomáš
06:00
created

OptionsResolverFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 93
ccs 32
cts 33
cp 0.9697
rs 10
c 0
b 0
f 0

5 Methods

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