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

SourceOptionResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 29.27 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 12
loc 41
ccs 16
cts 16
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 setSourceAllowedValues() 0 15 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\Contract\Configuration\OptionResolver\OptionResolverInterface;
12
use Symplify\PHP7_CodeSniffer\Exception\Configuration\SourceNotFoundException;
13
14
final class SourceOptionResolver implements OptionResolverInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    const NAME = 'source';
20
21 2
    public function getName() : string
22
    {
23 2
        return self::NAME;
24
    }
25
26 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...
27
    {
28 1
        $optionsResolver = new OptionsResolver();
29 1
        $optionsResolver->setDefined(self::NAME);
30 1
        $this->setSourceAllowedValues($optionsResolver);
31
32 1
        $values = $optionsResolver->resolve([
33 1
            self::NAME => $value
34
        ]);
35
36 1
        return $values[self::NAME];
37
    }
38
39
    private function setSourceAllowedValues(OptionsResolver $optionsResolver)
40
    {
41 1
        $optionsResolver->setAllowedValues(self::NAME, function (array $source) {
42 1
            foreach ($source as $singleSource) {
43 1
                if (!file_exists($singleSource)) {
44
                    throw new SourceNotFoundException(sprintf(
45 1
                        'Source "%s" does not exist.',
46
                        $singleSource
47
                    ));
48
                }
49
            }
50
51 1
            return true;
52 1
        });
53 1
    }
54
}
55