Completed
Push — master ( 393329...74ff7b )
by Tomáš
10:49
created

SniffPropertyValuesExtractor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.15%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 56
ccs 25
cts 26
cp 0.9615
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
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\Sniff\Xml\Extractor;
9
10
use SimpleXMLElement;
11
12
final class SniffPropertyValuesExtractor
13
{
14 3
    public function extractFromRuleXmlElement(SimpleXMLElement $ruleElement) : array
15
    {
16 3
        if (!isset($ruleElement->properties)) {
17 1
            return [];
18
        }
19
20 2
        $sniffCode = (string) $ruleElement['ref'];
21
22 2
        $customPropertyValues = [];
23 2
        foreach ($ruleElement->properties->property as $property) {
24 2
            $name = (string) $property['name'];
25 2
            $value = $this->resolveValue($property);
26 2
            $customPropertyValues[$sniffCode]['properties'][$name] = $value;
27
        }
28
29 2
        return $customPropertyValues;
30
    }
31
32
    /**
33
     * @return array|string
34
     */
35 2
    private function resolveValue(SimpleXMLElement $property)
36
    {
37 2
        if ($this->isArrayValue($property)) {
38 1
            return $this->resolveArrayValue($property);
39
        }
40
41 1
        return (string)$property['value'];
42
    }
43
44 2
    private function isArrayValue(SimpleXMLElement $property) : bool
45
    {
46 2
        return isset($property['type']) === true && (string)$property['type'] === 'array';
47
    }
48
49 1
    private function resolveArrayValue(SimpleXMLElement $arrayProperty) : array
50
    {
51 1
        $value = (string) $arrayProperty['value'];
52
53 1
        $values = [];
54 1
        foreach (explode(',', $value) as $val) {
55 1
            $v = '';
56
57 1
            list($key, $v) = explode('=>', $val . '=>');
58 1
            if ($v !== '') {
59
                $values[$key] = $v;
60
            } else {
61 1
                $values[] = $key;
62
            }
63
        }
64
65 1
        return $values;
66
    }
67
}
68