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\PHP7_CodeSniffer\Standard\Finder; |
9
|
|
|
|
10
|
|
|
use Symfony\Component\Finder\Finder; |
11
|
|
|
use Symplify\PHP7_CodeSniffer\Composer\VendorDirProvider; |
12
|
|
|
use Symplify\PHP7_CodeSniffer\Exception\Configuration\OptionResolver\StandardNotFoundException; |
13
|
|
|
|
14
|
|
|
final class StandardFinder |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string[] |
18
|
|
|
*/ |
19
|
|
|
private $rulesets = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string[] $names |
23
|
|
|
* @return string[] |
24
|
|
|
*/ |
25
|
6 |
|
public function getRulesetPathsForStandardNames(array $names) : array |
26
|
|
|
{ |
27
|
6 |
|
$rulesetPaths = []; |
28
|
6 |
|
foreach ($names as $name) { |
29
|
5 |
|
$rulesetPaths[$name] = $this->getRulesetPathForStandardName($name); |
30
|
|
|
} |
31
|
|
|
|
32
|
6 |
|
return $rulesetPaths; |
33
|
|
|
} |
34
|
|
|
|
35
|
9 |
|
public function getRulesetPathForStandardName(string $standardName) : string |
36
|
|
|
{ |
37
|
9 |
|
if (isset($this->getStandards()[$standardName])) { |
38
|
8 |
|
return $this->getStandards()[$standardName]; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
throw new StandardNotFoundException( |
42
|
|
|
sprintf( |
43
|
1 |
|
'Standard "%s" was not found. Found standards are: %s.', |
44
|
|
|
$standardName, |
45
|
1 |
|
implode($this->getRulesetNames(), ', ') |
46
|
|
|
) |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string[] |
52
|
|
|
*/ |
53
|
13 |
|
public function getStandards() : array |
54
|
|
|
{ |
55
|
13 |
|
if ($this->rulesets) { |
|
|
|
|
56
|
10 |
|
return $this->rulesets; |
57
|
|
|
} |
58
|
|
|
|
59
|
13 |
|
foreach ($this->findRulesetFiles() as $rulesetFile) { |
60
|
13 |
|
$rulesetXml = simplexml_load_file($rulesetFile); |
61
|
|
|
|
62
|
13 |
|
$rulesetName = (string) $rulesetXml['name']; |
63
|
13 |
|
$this->rulesets[$rulesetName] = $rulesetFile; |
64
|
|
|
} |
65
|
|
|
|
66
|
13 |
|
return $this->rulesets; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string[] |
71
|
|
|
*/ |
72
|
13 |
|
private function findRulesetFiles() : array |
73
|
|
|
{ |
74
|
13 |
|
$installedStandards = (new Finder())->files() |
75
|
13 |
|
->in(VendorDirProvider::provide()) |
76
|
13 |
|
->name('ruleset.xml'); |
77
|
|
|
|
78
|
13 |
|
return array_keys(iterator_to_array($installedStandards)); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
private function getRulesetNames() : array |
82
|
|
|
{ |
83
|
1 |
|
return array_keys($this->getStandards()); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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.