Completed
Branch master (aa8164)
by Tomáš
04:04
created

SniffsOptionResolver::setSniffsAllowedValues()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 1
nop 1
crap 3
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\InvalidSniffCodeException;
14
15
final class SniffsOptionResolver implements OptionResolverInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    const NAME = 'sniffs';
21
22 2
    public function getName() : string
23
    {
24 2
        return self::NAME;
25
    }
26
27 1 View Code Duplication
    public function resolve(array $value) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
28
    {
29 1
        $optionsResolver = new OptionsResolver();
30 1
        $optionsResolver->setDefined(self::NAME);
31 1
        $this->setSniffsAllowedValues($optionsResolver);
32
33 1
        $values = $optionsResolver->resolve([
34 1
            self::NAME => $value
35
        ]);
36
37 1
        return $values[self::NAME];
38
    }
39
40
    private function setSniffsAllowedValues(OptionsResolver $optionsResolver)
41
    {
42 1
        $optionsResolver->setAllowedValues(self::NAME, function (array $sniffs) {
43 1
            $sniffs = ValueNormalizer::normalizeCommaSeparatedValues($sniffs);
44
45 1
            foreach ($sniffs as $sniff) {
46 1
                if (substr_count($sniff, '.') !== 2) {
47
                    throw new InvalidSniffCodeException(sprintf(
48
                        'The specified sniff code "%s" is invalid.' .
49
                        PHP_EOL .
50 1
                        'Correct format is "StandardName.Category.SniffName".',
51
                        $sniff
52
                    ));
53
                }
54
            }
55
56 1
            return true;
57 1
        });
58 1
    }
59
}
60