createSniffsFromOwnRuleset()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
crap 4
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\Sniff\Factory;
9
10
use Nette\Utils\Strings;
11
use PHP_CodeSniffer\Sniffs\Sniff;
12
use SimpleXMLElement;
13
use Symplify\PHP7_CodeSniffer\Contract\Sniff\Factory\SniffFactoryInterface;
14
use Symplify\PHP7_CodeSniffer\Contract\Sniff\SniffSetFactoryAwareInterface;
15
use Symplify\PHP7_CodeSniffer\Sniff\Finder\SniffFinder;
16
use Symplify\PHP7_CodeSniffer\Sniff\Naming\SniffNaming;
17
use Symplify\PHP7_CodeSniffer\Sniff\SniffSetFactory;
18
use Symplify\PHP7_CodeSniffer\Sniff\Sorter\SniffSorter;
19
use Symplify\PHP7_CodeSniffer\Sniff\Xml\DataCollector\SniffPropertyValueDataCollector;
20
use Symplify\PHP7_CodeSniffer\Sniff\Xml\DataCollector\ExcludedSniffDataCollector;
21
22
final class RulesetXmlToSniffsFactory implements
23
    SniffFactoryInterface,
24
    SniffSetFactoryAwareInterface
25
{
26
    /**
27
     * @var ExcludedSniffDataCollector
28
     */
29
    private $excludedSniffDataCollector;
30
31
    /**
32
     * @var SniffPropertyValueDataCollector
33
     */
34
    private $customSniffPropertyDataCollector;
35
36
    /**
37
     * @var SniffSetFactory
38
     */
39
    private $sniffSetFactory;
40
41
    /**
42
     * @var SniffFinder
43
     */
44
    private $sniffFinder;
45
46
    /**
47
     * @var SingleSniffFactory
48
     */
49
    private $singleSniffFactory;
50
51 1
    public function __construct(
52
        SniffFinder $sniffFinder,
53
        ExcludedSniffDataCollector $excludedSniffDataCollector,
54
        SniffPropertyValueDataCollector $customSniffPropertyDataCollector,
55
        SingleSniffFactory $singleSniffFactory
56
    ) {
57 1
        $this->sniffFinder = $sniffFinder;
58 1
        $this->customSniffPropertyDataCollector = $customSniffPropertyDataCollector;
59 1
        $this->excludedSniffDataCollector = $excludedSniffDataCollector;
60 1
        $this->singleSniffFactory = $singleSniffFactory;
61 1
    }
62
63 9
    public function isMatch(string $reference) : bool
64
    {
65 9
        return Strings::endsWith($reference, 'ruleset.xml');
66
    }
67
68
    /**
69
     * @return Sniff[]
70
     */
71 8
    public function create(string $rulesetXmlFile) : array
72
    {
73 8
        $sniffs = $this->createSniffsFromOwnRuleset($rulesetXmlFile);
74
75 8
        $rulesetXml = simplexml_load_file($rulesetXmlFile);
76 8
        foreach ($rulesetXml->rule as $ruleXmlElement) {
77 8
            if ($this->isRuleXmlElementSkipped($ruleXmlElement)) {
78
                continue;
79
            }
80
81 8
            $this->excludedSniffDataCollector->collectFromRuleXmlElement($ruleXmlElement);
82 8
            $this->customSniffPropertyDataCollector->collectFromRuleXmlElement($ruleXmlElement);
83
84 8
            $sniffs = array_merge($sniffs, $this->sniffSetFactory->create($ruleXmlElement['ref']));
85
        }
86
87 8
        return SniffSorter::sort($sniffs);
88
    }
89
90 11
    public function setSniffSetFactory(SniffSetFactory $sniffSetFactory)
91
    {
92 11
        $this->sniffSetFactory = $sniffSetFactory;
93 11
    }
94
95 8
    private function isRuleXmlElementSkipped(SimpleXMLElement $ruleXmlElement) : bool
96
    {
97 8
        if (!isset($ruleXmlElement['ref'])) {
98
            return true;
99
        }
100
101 8
        if (isset($ruleXmlElement->severity)) {
102 8
            if (SniffNaming::isSniffCode($ruleXmlElement['ref'])) {
103
                return true;
104
            }
105
106 8
            return false;
107
        }
108
109 8
        return false;
110
    }
111
112
    /**
113
     * @return Sniff[]
114
     */
115 8
    private function createSniffsFromOwnRuleset(string $rulesetXmlFile) : array
116
    {
117 8
        $rulesetDir = dirname($rulesetXmlFile);
118 8
        $sniffDir = $rulesetDir.DIRECTORY_SEPARATOR.'Sniffs';
119 8
        if (!is_dir($sniffDir)) {
120 1
            return [];
121
        }
122
123 8
        $sniffClassNames = $this->sniffFinder->findAllSniffClassesInDirectory($sniffDir);
124
125 8
        $sniffs = [];
126 8
        foreach ($sniffClassNames as $sniffClassName) {
127 8
            if ($sniff = $this->singleSniffFactory->create($sniffClassName)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $sniff is correct as $this->singleSniffFactor...create($sniffClassName) (which targets Symplify\PHP7_CodeSniffe...eSniffFactory::create()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
128 8
                $sniffs[] = $sniff;
129
            }
130
        }
131
132 8
        return $sniffs;
133
    }
134
}
135