Completed
Push — master ( 50809e...8af631 )
by Tomáš
04:38 queued 02:05
created

RulesetFileSystem::getRulesetPathForStandardName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
crap 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\MultiCodingStandard\CodeSniffer\FileSystem;
9
10
use Exception;
11
use Nette\Utils\Strings;
12
use Symfony\Component\Finder\Finder;
13
use Symplify\MultiCodingStandard\Contract\CodeSniffer\FileSystem\RulesetFileSystemInterface;
14
15
final class RulesetFileSystem implements RulesetFileSystemInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    private $vendorDir;
21
22
    /**
23
     * @var strings[]
24
     */
25
    private $rulesets = [];
26
27
    /**
28
     * @param string $vendorDir
29
     */
30 7
    public function __construct($vendorDir)
31
    {
32 7
        $this->vendorDir = $vendorDir;
33 7
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 4
    public function getRulesetPathForStandardName($standardName)
39
    {
40 4
        if (isset($this->getRulesets()[$standardName])) {
41 3
            return $this->getRulesets()[$standardName];
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getRulesets()[$standardName]; of type integer|string|Symplify\...ffer\FileSystem\strings adds the type Symplify\MultiCodingStan...ffer\FileSystem\strings to the return on line 41 which is incompatible with the return type declared by the interface Symplify\MultiCodingStan...esetPathForStandardName of type string.
Loading history...
42
        }
43
44 1
        throw new Exception(
45
            sprintf(
46 1
                'Ruleset for standard "%s" was not found. Found standards are: %s.',
47
                $standardName,
48 1
                implode($this->getRulesetNames(), ', ')
49
            )
50
        );
51
    }
52
53
    /**
54
     * @return array
55
     */
56 4
    private function getRulesets()
57
    {
58 4
        if ($this->rulesets) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->rulesets of type Symplify\MultiCodingStan...er\FileSystem\strings[] 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...
59 4
            return $this->rulesets;
60
        }
61
62 4
        foreach ($this->findRulesetFiles() as $rulesetFile) {
63 4
            $rulesetXml = simplexml_load_file($rulesetFile);
64
65 4
            $rulesetName = (string) $rulesetXml['name'];
66 4
            $this->rulesets[$rulesetName] = $rulesetFile;
67
        }
68
69 4
        return $this->rulesets;
70
    }
71
72
    /**
73
     * @return string[]
74
     */
75 4 View Code Duplication
    private function findRulesetFiles()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77 4
        $installedStandards = (new Finder())->files()
78 4
            ->in($this->vendorDir)
79 4
            ->name('ruleset.xml');
80
81 4
        return array_keys(iterator_to_array($installedStandards));
82
    }
83
84
    /**
85
     * @return string[]
86
     */
87 1
    private function getRulesetNames()
88
    {
89 1
        return array_keys($this->getRulesets());
90
    }
91
}
92