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

extractFromRuleXmlElement()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
crap 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