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

SniffsOptionResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 26.67 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 12
loc 45
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A resolve() 12 12 1
A setSniffsAllowedValues() 0 19 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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