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
|
11 |
|
public function __construct(MultiCsFileLoaderInterface $multiCsFileLoader, SniffNamingInterface $sniffNaming) |
33
|
|
|
{ |
34
|
11 |
|
$this->multiCsFileLoader = $multiCsFileLoader; |
35
|
11 |
|
$this->sniffNaming = $sniffNaming; |
36
|
11 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
8 |
|
public function getActiveSniffs() |
42
|
|
|
{ |
43
|
8 |
|
if (isset($this->getMultiCsFile()[self::SNIFFS])) { |
44
|
5 |
|
$sniffs = $this->getMultiCsFile()[self::SNIFFS]; |
45
|
5 |
|
return $this->sniffNaming->detectUnderscoreLowercaseFromSniffClassesOrNames($sniffs); |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
return []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
8 |
|
public function getActiveStandards() |
55
|
|
|
{ |
56
|
8 |
|
if (isset($this->getMultiCsFile()[self::STANDARDS])) { |
57
|
3 |
|
return $this->getMultiCsFile()[self::STANDARDS]; |
58
|
|
|
} |
59
|
|
|
|
60
|
5 |
|
return []; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
4 |
|
public function getExcludedSniffs() |
67
|
|
|
{ |
68
|
4 |
|
if (isset($this->getMultiCsFile()[self::EXCLUDED_SNIFFS])) { |
69
|
2 |
|
return $this->getMultiCsFile()[self::EXCLUDED_SNIFFS]; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
return []; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public function getActiveFixers() |
79
|
|
|
{ |
80
|
1 |
|
if (isset($this->getMultiCsFile()[self::FIXERS])) { |
81
|
1 |
|
return $this->getMultiCsFile()[self::FIXERS]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return []; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
1 |
|
public function getExcludedFixers() |
91
|
|
|
{ |
92
|
1 |
|
if (isset($this->getMultiCsFile()[self::EXCLUDED_FIXERS])) { |
93
|
1 |
|
return $this->getMultiCsFile()[self::EXCLUDED_FIXERS]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return []; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
1 |
|
public function getActiveFixerLevels() |
103
|
|
|
{ |
104
|
1 |
|
if (isset($this->getMultiCsFile()[self::FIXER_LEVELS])) { |
105
|
1 |
|
$fixerLevels = $this->getMultiCsFile()[self::FIXER_LEVELS]; |
106
|
1 |
|
$this->ensureLevelsAreValid($fixerLevels); |
107
|
|
|
|
108
|
1 |
|
return $fixerLevels; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return []; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
11 |
|
private function getMultiCsFile() |
118
|
|
|
{ |
119
|
11 |
|
if ($this->multiCsFile) { |
|
|
|
|
120
|
10 |
|
return $this->multiCsFile; |
121
|
|
|
} |
122
|
|
|
|
123
|
11 |
|
$this->multiCsFile = $this->multiCsFileLoader->load(); |
124
|
|
|
|
125
|
11 |
|
return $this->multiCsFile; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string[] |
130
|
|
|
*/ |
131
|
1 |
|
private function getFixerLevels() |
132
|
|
|
{ |
133
|
1 |
|
return ['psr0', 'psr1', 'psr2', 'symfony']; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @throws \Exception |
138
|
|
|
*/ |
139
|
1 |
|
private function ensureLevelsAreValid(array $fixerLevels) |
140
|
|
|
{ |
141
|
1 |
|
foreach ($fixerLevels as $fixerLevel) { |
142
|
1 |
|
if (!in_array($fixerLevel, $this->getFixerLevels(), true)) { |
143
|
|
|
throw new \Exception( |
144
|
|
|
sprintf( |
145
|
|
|
'Level "%s" is not supported. Available levels are: %s.', |
146
|
|
|
$fixerLevel, |
147
|
1 |
|
implode($this->getFixerLevels(), ', ') |
148
|
|
|
) |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
} |
152
|
1 |
|
} |
153
|
|
|
} |
154
|
|
|
|
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.