Completed
Pull Request — master (#10)
by Tomáš
24:14
created

Configuration::getActiveFixerLevels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 6
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\Configuration;
9
10
use Symfony\CS\FixerInterface;
11
use Symplify\MultiCodingStandard\Contract\CodeSniffer\Naming\SniffNamingInterface;
12
use Symplify\MultiCodingStandard\Contract\Configuration\ConfigurationInterface;
13
use Symplify\MultiCodingStandard\Contract\Configuration\MultiCsFileLoaderInterface;
14
15
final class Configuration implements ConfigurationInterface
16
{
17
    /**
18
     * @var MultiCsFileLoaderInterface
19
     */
20
    private $multiCsFileLoader;
21
22
    /**
23
     * @var SniffNamingInterface
24
     */
25
    private $sniffNaming;
26
27
    /**
28
     * @var array
29
     */
30
    private $multiCsFile;
31
32 4
    public function __construct(MultiCsFileLoaderInterface $multiCsFileLoader, SniffNamingInterface $sniffNaming)
33
    {
34 4
        $this->multiCsFileLoader = $multiCsFileLoader;
35 4
        $this->sniffNaming = $sniffNaming;
36 4
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 2
    public function getActiveSniffs()
42
    {
43 2
        if (isset($this->getMultiCsFile()[self::SNIFFS])) {
44 1
            $sniffs = $this->getMultiCsFile()[self::SNIFFS];
45 1
            return $this->sniffNaming->detectUnderscoreLowercaseFromSniffNames($sniffs);
46
        }
47
48 1
        return [];
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 2
    public function getActiveStandards()
55
    {
56 2
        if (isset($this->getMultiCsFile()[self::STANDARDS])) {
57 1
            return $this->getMultiCsFile()[self::STANDARDS];
58
        }
59
60 1
        return [];
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2
    public function getExcludedSniffs()
67
    {
68 2
        if (isset($this->getMultiCsFile()[self::EXCLUDED_SNIFFS])) {
69 1
            return $this->getMultiCsFile()[self::EXCLUDED_SNIFFS];
70
        }
71
72 1
        return [];
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getActiveFixers()
79
    {
80
        if (isset($this->getMultiCsFile()[self::FIXERS])) {
81
            return $this->getMultiCsFile()[self::FIXERS];
82
        }
83
84
        return [];
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getExcludedFixers()
91
    {
92
        if (isset($this->getMultiCsFile()[self::EXCLUDED_FIXERS])) {
93
            return $this->getMultiCsFile()[self::EXCLUDED_FIXERS];
94
        }
95
96
        return [];
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getActiveFixerLevels()
103
    {
104
        if (isset($this->getMultiCsFile()[self::FIXER_LEVELS])) {
105
            $fixerLevels = $this->getMultiCsFile()[self::FIXER_LEVELS];
106
            $this->ensureLevelsAreValid($fixerLevels);
107
108
            return $fixerLevels;
109
        }
110
111
        return [];
112
    }
113
114
    /**
115
     * @return array
116
     */
117 4
    private function getMultiCsFile()
118
    {
119 4
        if ($this->multiCsFile) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->multiCsFile of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
120 3
            return $this->multiCsFile;
121
        }
122
123 4
        $this->multiCsFile = $this->multiCsFileLoader->load();
124
125 4
        return $this->multiCsFile;
126
    }
127
128
    /**
129
     * @param string[] $fixerLevels
130
     */
131
    private function ensureLevelsAreValid(array $fixerLevels)
132
    {
133
        foreach ($fixerLevels as $fixerLevel) {
134
            if (!in_array($fixerLevel, $this->getFixerLevels(), true)) {
135
                throw new \Exception(
136
                    sprintf(
137
                        'Level "%s" is not supported. Available levels are: %s.',
138
                        $fixerLevel,
139
                        implode($this->getFixerLevels(), ', ')
140
                    )
141
                );
142
            }
143
        }
144
    }
145
146
    /**
147
     * @return string[]
148
     */
149
    private function getFixerLevels()
150
    {
151
        return ['psr0', 'psr1', 'psr2', 'symfony'];
152
    }
153
}
154