Completed
Push — master ( afadea...e6376f )
by Tomáš
03:06 queued 03:01
created

FixerFactory::resolveFixersForLevelsAndFixers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
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\Factory;
9
10
use Symfony\CS\ConfigurationResolver;
11
use Symfony\CS\FixerInterface;
12
13
final class FixerFactory
14
{
15
    /**
16
     * @var ConfigurationResolver
17
     */
18
    private $configurationResolver;
19
20 7
    public function __construct(ConfigurationResolver $configurationResolver)
21
    {
22 7
        $this->configurationResolver = $configurationResolver;
23 7
    }
24
25
    /**
26
     * @return FixerInterface[]
27
     */
28 7
    public function createFromLevelsFixersAndExcludedFixers(array $fixerLevels, array $fixers, array $excludedFixers) : array
29
    {
30 7
        $fixersFromLevels = $this->createFromLevelsAndExcludedFixers($fixerLevels, $excludedFixers);
31 7
        $standaloneFixers = $this->createFromFixers($fixers);
32
33 7
        return array_merge($fixersFromLevels, $standaloneFixers);
34
    }
35
36 7
    private function createFromLevelsAndExcludedFixers(array $fixerLevels, array $excludedFixers) : array
37
    {
38 7
        if (!count($fixerLevels)) {
39 2
            return [];
40
        }
41
42 5
        $fixers = [];
43 5
        foreach ($fixerLevels as $fixerLevel) {
44 5
            $excludedFixersAsString = $this->turnExcludedFixersToString($excludedFixers);
45 5
            $newFixers = $this->resolveFixersForLevelsAndFixers($fixerLevel, $excludedFixersAsString);
46
47 5
            $fixers = array_merge($fixers, $newFixers);
48
        }
49
50 5
        return $fixers;
51
    }
52
53 7
    private function createFromFixers(array $fixers) : array
54
    {
55 7
        if (!count($fixers)) {
56 6
            return [];
57
        }
58
59 1
        $fixersAsString = $this->turnFixersToString($fixers);
60 1
        return $this->resolveFixersForLevelsAndFixers('none', $fixersAsString);
61
    }
62
63 6
    private function resolveFixersForLevelsAndFixers(string $level, string $fixersAsString) : array
64
    {
65 6
        $currentConfigurationResolver = clone $this->configurationResolver;
66 6
        $currentConfigurationResolver->setOption('level', $level);
67 6
        $currentConfigurationResolver->setOption('fixers', $fixersAsString);
68 6
        $currentConfigurationResolver->resolve();
69
70 6
        return $currentConfigurationResolver->getFixers();
71
    }
72
73 1
    private function turnFixersToString(array $fixers) : string
74
    {
75 1
        return $this->implodeWithPresign($fixers);
76
    }
77
78 5
    private function turnExcludedFixersToString(array $excludedFixers) : string
79
    {
80 5
        return $this->implodeWithPresign($excludedFixers, '-');
81
    }
82
83 6
    private function implodeWithPresign(array $items, string $presign = '')
84
    {
85 6
        if (count($items)) {
86 3
            return $presign . implode(',' . $presign, $items);
87
        }
88 3
        return '';
89
    }
90
}
91