Completed
Push — master ( 23bc19...2eec4d )
by Tomáš
11:42
created

CustomPropertyValuesExtractor   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 94.29%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 75
ccs 33
cts 35
cp 0.9429
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A extractFromRulesetXmlFile() 0 18 3
A extractFromRuleXmlElement() 0 17 3
A resolveValue() 0 8 2
A isArrayValue() 0 4 2
A resolveArrayValue() 0 18 3
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\Ruleset\Extractor;
9
10
use SimpleXMLElement;
11
12
final class CustomPropertyValuesExtractor
13
{
14 5
    public function extractFromRulesetXmlFile(string $rulesetXmlFile) : array
15
    {
16 5
        $rulesetXml = simplexml_load_file($rulesetXmlFile);
17
18 5
        $customPropertyValues = [];
19 5
        foreach ($rulesetXml->rule as $ruleElement) {
20 5
            if (!isset($ruleElement['ref'])) {
21
                continue;
22
            }
23
24 5
            $customPropertyValues = array_merge(
25
                $customPropertyValues,
26 5
                $this->extractFromRuleXmlElement($ruleElement)
27
            );
28
        }
29
30 5
        return $customPropertyValues;
31
    }
32
33 8
    public function extractFromRuleXmlElement(SimpleXMLElement $ruleElement) : array
34
    {
35 8
        if (!isset($ruleElement->properties)) {
36 6
            return [];
37
        }
38
39 7
        $sniffCode = (string) $ruleElement['ref'];
40
41 7
        $customPropertyValues = [];
42 7
        foreach ($ruleElement->properties->property as $property) {
43 7
            $name = (string) $property['name'];
44 7
            $value = $this->resolveValue($property);
45 7
            $customPropertyValues[$sniffCode]['properties'][$name] = $value;
46
        }
47
48 7
        return $customPropertyValues;
49
    }
50
51
    /**
52
     * @return array|string
53
     */
54 7
    private function resolveValue(SimpleXMLElement $property)
55
    {
56 7
        if ($this->isArrayValue($property)) {
57 4
            return $this->resolveArrayValue($property);
58
        }
59
60 6
        return (string)$property['value'];
61
    }
62
63 7
    private function isArrayValue(SimpleXMLElement $property) : bool
64
    {
65 7
        return isset($property['type']) === true && (string)$property['type'] === 'array';
66
    }
67
68 4
    private function resolveArrayValue(SimpleXMLElement $arrayProperty) : array
69
    {
70 4
        $value = (string) $arrayProperty['value'];
71
72 4
        $values = [];
73 4
        foreach (explode(',', $value) as $val) {
74 4
            $v = '';
75
76 4
            list($key, $v) = explode('=>', $val . '=>');
77 4
            if ($v !== '') {
78
                $values[$key] = $v;
79
            } else {
80 4
                $values[] = $key;
81
            }
82
        }
83
84 4
        return $values;
85
    }
86
}
87