Completed
Push — master ( 74ff7b...2fd8e1 )
by Tomáš
03:25
created

SniffPropertyValueDataCollector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 52
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A collectFromRuleXmlElement() 0 12 2
A getForSniff() 0 9 2
A addSniffPropertyValues() 0 11 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\Sniff\Xml\DataCollector;
9
10
use PHP_CodeSniffer\Sniffs\Sniff;
11
use SimpleXMLElement;
12
use Symplify\PHP7_CodeSniffer\Sniff\Naming\SniffNaming;
13
use Symplify\PHP7_CodeSniffer\Sniff\Xml\Extractor\SniffPropertyValuesExtractor;
14
15
final class SniffPropertyValueDataCollector
16
{
17
    /**
18
     * @var SniffPropertyValuesExtractor
19
     */
20
    private $sniffPropertyValuesExtractor;
21
22
    /**
23
     * @var array[]
24
     */
25
    private $sniffPropertyValuesBySniffClass = [];
26
27 16
    public function __construct(SniffPropertyValuesExtractor $sniffPropertyValuesExtractor)
28
    {
29 16
        $this->sniffPropertyValuesExtractor = $sniffPropertyValuesExtractor;
30 16
    }
31
32 8
    public function collectFromRuleXmlElement(SimpleXMLElement $ruleXmlElement)
33
    {
34 8
        if (!isset($ruleXmlElement->properties)) {
35 6
            return;
36
        }
37
38 6
        $sniffCode = (string) $ruleXmlElement['ref'];
39 6
        $sniffClass = SniffNaming::guessClassByCode($sniffCode);
40
41 6
        $properties = $this->sniffPropertyValuesExtractor->extractFromRuleXmlElement($ruleXmlElement);
42 6
        $this->addSniffPropertyValues($sniffClass, $properties);
43 6
    }
44
45 10
    public function getForSniff(Sniff $sniff) : array
46
    {
47 10
        $sniffClass = get_class($sniff);
48 10
        if (!isset($this->sniffPropertyValuesBySniffClass[$sniffClass])) {
49 9
            return [];
50
        }
51
52 2
        return $this->sniffPropertyValuesBySniffClass[$sniffClass];
53
    }
54
55 6
    private function addSniffPropertyValues(string $sniffCode, array $propertyValues)
56
    {
57 6
        if (!isset($this->sniffPropertyValuesBySniffClass[$sniffCode])) {
58 4
            $this->sniffPropertyValuesBySniffClass[$sniffCode] = [];
59
        }
60
61 6
        $this->sniffPropertyValuesBySniffClass[$sniffCode] = array_merge(
62 6
            $this->sniffPropertyValuesBySniffClass[$sniffCode],
63
            $propertyValues
64
        );
65 6
    }
66
}
67