Completed
Push — master ( 2fd8e1...1442eb )
by Tomáš
03:40
created

SniffPropertyValuesExtractor   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 80
ccs 31
cts 31
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A extractFromRuleXmlElement() 0 15 3
A normalizeValue() 0 14 3
A isArrayValue() 0 4 2
A normalizeBoolValue() 0 8 2
A normalizeArrayValue() 0 16 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
    /**
15
     * @var bool[]
16
     */
17
    private $stringToBoolMap = [
18
        'true' => true,
19
        'TRUE' => true,
20
        'false' => false,
21
        'FALSE' => false
22
    ];
23
24 9
    public function extractFromRuleXmlElement(SimpleXMLElement $ruleXmlElement) : array
25
    {
26 9
        if (!isset($ruleXmlElement->properties)) {
27 1
            return [];
28
        }
29
30 8
        $propertyValues = [];
31 8
        foreach ($ruleXmlElement->properties->property as $propertyXmlElement) {
32 8
            $name = (string) $propertyXmlElement['name'];
33 8
            $value = $this->normalizeValue((string) $propertyXmlElement['value'], $propertyXmlElement);
34 8
            $propertyValues[$name] = $value;
35
        }
36
37 8
        return $propertyValues;
38
    }
39
40
    /**
41
     * @return mixed
42
     */
43 8
    private function normalizeValue(string $value, SimpleXMLElement $propertyXmlElement)
44
    {
45 8
        $value = trim($value);
46
47 8
        if (is_numeric($value)) {
48 5
            return (int) $value;
49
        }
50
51 7
        if ($this->isArrayValue($propertyXmlElement)) {
52 5
            return $this->normalizeArrayValue($value);
53
        }
54
55 6
        return $this->normalizeBoolValue($value);
56
    }
57
58 7
    private function isArrayValue(SimpleXMLElement $property) : bool
59
    {
60 7
        return isset($property['type']) === true && (string) $property['type'] === 'array';
61
    }
62
63
    /**
64
     * @return mixed
65
     */
66 6
    private function normalizeBoolValue(string $value)
67
    {
68 6
        if (isset($this->stringToBoolMap[$value])) {
69 4
            return $this->stringToBoolMap[$value];
70
        }
71
72 6
        return $value;
73
    }
74
75 5
    private function normalizeArrayValue(string $value) : array
76
    {
77 5
        $values = [];
78 5
        foreach (explode(',', $value) as $val) {
79 5
            $v = '';
80
81 5
            list($key, $v) = explode('=>', $val . '=>');
82 5
            if ($v !== '') {
83 1
                $values[$key] = $v;
84
            } else {
85 5
                $values[] = $key;
86
            }
87
        }
88
89 5
        return $values;
90
    }
91
}
92