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

EnabledFixerResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 1
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