Completed
Branch master (06cb84)
by Tomáš
06:00
created

StandardFinder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 61.53%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 72
ccs 16
cts 26
cp 0.6153
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRulesetPathForStandardName() 0 14 2
A getStandards() 0 15 3
A findRulesetFiles() 0 8 1
A getRulesetNames() 0 4 1
A getRulesetPathsForStandardNames() 0 9 2
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;
9
10
use Exception;
11
use Symfony\Component\Finder\Finder;
12
use Symplify\PHP7_CodeSniffer\Composer\VendorDirProvider;
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
    public function getRulesetPathsForStandardNames(array $names) : array
26
    {
27
        $rulesetPaths = [];
28
        foreach ($names as $name) {
29
            $rulesetPaths[$name] = $this->getRulesetPathForStandardName($name);
30
        }
31
32
        return $rulesetPaths;
33
    }
34
35 2
    public function getRulesetPathForStandardName(string $standardName) : string
36
    {
37 2
        if (isset($this->getStandards()[$standardName])) {
38 2
            return $this->getStandards()[$standardName];
39
        }
40
41
        throw new Exception(
42
            sprintf(
43
                'Ruleset for standard "%s" was not found. Found standards are: %s.',
44
                $standardName,
45
                implode($this->getRulesetNames(), ', ')
46
            )
47
        );
48
    }
49
50
    /**
51
     * @return string[]
52
     */
53 4
    public function getStandards() : array
54
    {
55 4
        if ($this->rulesets) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->rulesets of type string[] 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...
56 3
            return $this->rulesets;
57
        }
58
59 4
        foreach ($this->findRulesetFiles() as $rulesetFile) {
60 4
            $rulesetXml = simplexml_load_file($rulesetFile);
61
62 4
            $rulesetName = (string) $rulesetXml['name'];
63 4
            $this->rulesets[$rulesetName] = $rulesetFile;
64
        }
65
66 4
        return $this->rulesets;
67
    }
68
69
    /**
70
     * @return string[]
71
     */
72 4
    private function findRulesetFiles() : array
73
    {
74 4
        $installedStandards = (new Finder())->files()
75 4
            ->in(VendorDirProvider::provide())
76 4
            ->name('ruleset.xml');
77
78 4
        return array_keys(iterator_to_array($installedStandards));
79
    }
80
81
    private function getRulesetNames() : array
82
    {
83
        return array_keys($this->getStandards());
84
    }
85
}
86