StandardsOptionResolver   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 19.7 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 13
loc 66
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A resolve() 13 13 1
A setNormalizer() 0 9 1
A setAllowedValues() 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\StandardNotFoundException;
14
use Symplify\PHP7_CodeSniffer\Standard\Finder\StandardFinder;
15
16
final class StandardsOptionResolver implements OptionResolverInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    const NAME = 'standards';
22
23
    /**
24
     * @var StandardFinder
25
     */
26
    private $standardFinder;
27
28 5
    public function __construct(StandardFinder $standardFinder)
29
    {
30 5
        $this->standardFinder = $standardFinder;
31 5
    }
32
33 5
    public function getName() : string
34
    {
35 5
        return self::NAME;
36
    }
37
38 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...
39
    {
40 3
        $optionsResolver = new OptionsResolver();
41 3
        $optionsResolver->setDefined(self::NAME);
42 3
        $this->setAllowedValues($optionsResolver);
43 3
        $this->setNormalizer($optionsResolver);
44
45 3
        $values = $optionsResolver->resolve([
46 3
            self::NAME => $value
47
        ]);
48
49 2
        return $values[self::NAME];
50
    }
51
52 3
    private function setNormalizer(OptionsResolver $optionsResolver)
53
    {
54 3
        $optionsResolver->setNormalizer(
55 3
            self::NAME,
56
            function (OptionsResolver $optionsResolver, array $standardNames) {
57 2
                return ValueNormalizer::normalizeCommaSeparatedValues($standardNames);
58 3
            }
59
        );
60 3
    }
61
62
    private function setAllowedValues(OptionsResolver $optionsResolver)
63
    {
64 3
        $optionsResolver->setAllowedValues(self::NAME, function (array $standards) {
65 3
            $standards = ValueNormalizer::normalizeCommaSeparatedValues($standards);
66
67 3
            $availableStandards = $this->standardFinder->getStandards();
68 3
            foreach ($standards as $standardName) {
69 3
                if (!array_key_exists($standardName, $availableStandards)) {
70 1
                    throw new StandardNotFoundException(sprintf(
71 1
                        'Standard "%s" is not supported. Pick one of: %s.',
72
                        $standardName,
73 3
                        implode(array_keys($availableStandards), ', ')
74
                    ));
75
                }
76
            }
77
78 2
            return true;
79 3
        });
80 3
    }
81
}
82