Completed
Branch master (f7eb77)
by Tomáš
03:26
created

SniffsOptionResolver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 22.58 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 14
loc 62
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A resolve() 14 14 1
A setNormalizer() 0 14 2
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\OptionResolver\InvalidSniffCodeException;
14
use Symplify\PHP7_CodeSniffer\Sniff\Naming\SniffNaming;
15
16
final class SniffsOptionResolver implements OptionResolverInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    const NAME = 'sniffs';
22
23 6
    public function getName() : string
24
    {
25 6
        return self::NAME;
26
    }
27
28 5 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...
29
    {
30 5
        $optionsResolver = new OptionsResolver();
31 5
        $optionsResolver->setDefined(self::NAME);
32
33 5
        $this->setNormalizer($optionsResolver);
34 5
        $this->setSniffsAllowedValues($optionsResolver);
35
36 5
        $values = $optionsResolver->resolve([
37 5
            self::NAME => $value
38
        ]);
39
40 5
        return $values[self::NAME];
41
    }
42
43 5
    private function setNormalizer(OptionsResolver $optionsResolver)
44
    {
45 5
        $optionsResolver->setNormalizer(
46 5
            self::NAME,
47
            function (OptionsResolver $optionsResolver, array $sniffCodes) {
48 5
                $sniffCodeToClasses = [];
49 5
                foreach ($sniffCodes as $sniffCode) {
50 3
                    $sniffCodeToClasses[$sniffCode] = SniffNaming::guessClassByCode($sniffCode);
51
                }
52
53 5
                return $sniffCodeToClasses;
54 5
            }
55
        );
56 5
    }
57
58
    private function setSniffsAllowedValues(OptionsResolver $optionsResolver)
59
    {
60 5
        $optionsResolver->setAllowedValues(self::NAME, function (array $sniffs) {
61 5
            $sniffs = ValueNormalizer::normalizeCommaSeparatedValues($sniffs);
62
63 5
            foreach ($sniffs as $sniff) {
64 3
                if (substr_count($sniff, '.') !== 2) {
65
                    throw new InvalidSniffCodeException(sprintf(
66
                        'The specified sniff code "%s" is invalid.' .
67
                        PHP_EOL .
68 3
                        'Correct format is "StandardName.Category.SniffName".',
69
                        $sniff
70
                    ));
71
                }
72
            }
73
74 5
            return true;
75 5
        });
76 5
    }
77
}
78