SniffsOptionResolver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 25.93 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 14
loc 54
ccs 24
cts 24
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 setAllowedValues() 0 19 3
A setNormalizer() 0 6 1

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 5
    public function getName() : string
24
    {
25 5
        return self::NAME;
26
    }
27
28 3 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 3
        $optionsResolver = new OptionsResolver();
31 3
        $optionsResolver->setDefined(self::NAME);
32
33 3
        $this->setAllowedValues($optionsResolver);
34 3
        $this->setNormalizer($optionsResolver);
35
36 3
        $values = $optionsResolver->resolve([
37 3
            self::NAME => $value
38
        ]);
39
40 2
        return $values[self::NAME];
41
    }
42
43 3
    private function setAllowedValues(OptionsResolver $optionsResolver)
44
    {
45
        $optionsResolver->setAllowedValues(self::NAME, function (array $sniffs) {
46 3
            $sniffs = ValueNormalizer::normalizeCommaSeparatedValues($sniffs);
47
48 3
            foreach ($sniffs as $sniff) {
49 2
                if (substr_count($sniff, '.') !== 2) {
50 1
                    throw new InvalidSniffCodeException(sprintf(
51
                        'The specified sniff code "%s" is invalid.' .
52 1
                        PHP_EOL .
53 2
                        'Correct format is "StandardName.Category.SniffName".',
54
                        $sniff
55
                    ));
56
                }
57
            }
58
59 2
            return true;
60 3
        });
61 3
    }
62
63
    private function setNormalizer(OptionsResolver $optionsResolver)
64
    {
65 3
        $optionsResolver->setNormalizer(self::NAME, function (OptionsResolver $optionsResolver, array $sniffCodes) {
66 2
            return ValueNormalizer::normalizeCommaSeparatedValues($sniffCodes);
67 3
        });
68 3
    }
69
}
70