Completed
Push — master ( fe5f39...abd96b )
by Tomáš
05:18 queued 02:13
created

EnabledFixerResolver   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 27 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.86%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 1
cbo 3
dl 27
loc 100
ccs 39
cts 42
cp 0.9286
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getEnabledFixers() 0 13 2
A addFixersByLevel() 14 14 3
A getExcludedFixersAsString() 0 9 2
A addCustomFixers() 13 13 2
A getEnabledFixersAsAstring() 0 9 2

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\MultiCodingStandard\PhpCsFixer;
9
10
use Symfony\CS\ConfigurationResolver;
11
use Symfony\CS\Fixer;
12
use Symfony\CS\FixerInterface;
13
use Symplify\MultiCodingStandard\Contract\Configuration\PhpCsFixerConfigurationInterface;
14
15
final class EnabledFixerResolver
16
{
17
    /**
18
     * @var PhpCsFixerConfigurationInterface
19
     */
20
    private $configuration;
21
22
    /**
23
     * @var ConfigurationResolverFactory
24
     */
25
    private $configurationResolverFactory;
26
27
    /**
28
     * @var FixerInterface[]
29
     */
30
    private $enabledFixers = [];
31
32 1
    public function __construct(
33
        PhpCsFixerConfigurationInterface $configuration,
34
        ConfigurationResolverFactory $configurationResolverFactory
35
    ) {
36 1
        $this->configuration = $configuration;
37 1
        $this->configurationResolverFactory = $configurationResolverFactory;
38 1
    }
39
    
40
    /**
41
     * @return FixerInterface[]
42
     */
43 1
    public function getEnabledFixers()
44
    {
45 1
        if ($this->enabledFixers !== []) {
46
            return $this->enabledFixers;
47
        }
48
49 1
        $configurationResolver = $this->configurationResolverFactory->create();
50
51 1
        $this->addFixersByLevel($configurationResolver);
52 1
        $this->addCustomFixers($configurationResolver);
53
54 1
        return $this->enabledFixers;
55
    }
56
57 1 View Code Duplication
    private function addFixersByLevel(ConfigurationResolver $configurationResolver)
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...
58
    {
59 1
        $excludedFixers = $this->getExcludedFixersAsString();
60 1
        foreach ($this->configuration->getActiveFixerLevels() as $level) {
61 1
            $currentConfigurationResolver = clone $configurationResolver;
62 1
            if ($excludedFixers) {
63 1
                $currentConfigurationResolver->setOption('fixers', $excludedFixers);
64
            }
65 1
            $currentConfigurationResolver->setOption('level', $level);
66 1
            $currentConfigurationResolver->resolve();
67
68 1
            $this->enabledFixers = array_merge($this->enabledFixers, $currentConfigurationResolver->getFixers());
69
        }
70 1
    }
71
72
    /**
73
     * @return string
74
     */
75 1
    private function getExcludedFixersAsString()
76
    {
77 1
        $excludedFixers = '';
78 1
        if (count($this->configuration->getExcludedFixers())) {
79 1
            $excludedFixers .= '-' . implode(',-', $this->configuration->getExcludedFixers());
80 1
            return $excludedFixers;
81
        }
82
        return $excludedFixers;
83
    }
84
85
    /**
86
     * @return array
87
     */
88 1 View Code Duplication
    private function addCustomFixers(ConfigurationResolver $configurationResolver)
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...
89
    {
90 1
        $fixers = $this->getEnabledFixersAsAstring();
91
92 1
        if ($fixers) {
93 1
            $currentConfigurationResolver = clone $configurationResolver;
94 1
            $currentConfigurationResolver->setOption('level', 'none');
95 1
            $currentConfigurationResolver->setOption('fixers', $fixers);
96 1
            $currentConfigurationResolver->resolve();
97
98 1
            $this->enabledFixers = array_merge($this->enabledFixers, $currentConfigurationResolver->getFixers());
99
        }
100 1
    }
101
102
    /**
103
     * @return string
104
     */
105 1
    private function getEnabledFixersAsAstring()
106
    {
107 1
        $fixers = '';
108 1
        if (count($this->configuration->getActiveFixers())) {
109 1
            $fixers .= implode(',', $this->configuration->getActiveFixers());
110 1
            return $fixers;
111
        }
112
        return $fixers;
113
    }
114
}
115