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

SniffPropertyValueDataCollector   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 51.35%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 95
ccs 19
cts 37
cp 0.5135
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A collectFromRuleXmlElement() 0 9 2
A getForSniff() 0 5 1
A getForSniffClass() 0 9 2
A addCustomSniffProperty() 0 11 2
A normalizeValues() 0 8 2
A normalizeValue() 0 5 1
A trimStringValue() 0 8 2
B normalizeBoolValue() 0 12 5
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
14
final class SniffPropertyValueDataCollector
15
{
16
    /**
17
     * @var array[]
18
     */
19
    private $customSniffPropertyValuesBySniffCode = [];
20
21 6
    public function collectFromRuleXmlElement(SimpleXMLElement $ruleXmlElement)
22
    {
23 6
        if (isset($ruleXmlElement->properties)) {
24 4
            $this->addCustomSniffProperty(
25 4
                (string) $ruleXmlElement['ref'],
26 4
                (array) $ruleXmlElement->properties
27
            );
28
        }
29 6
    }
30
31 8
    public function getForSniff(Sniff $sniff) : array
32
    {
33 8
        $sniffClassName = get_class($sniff);
34 8
        return $this->getForSniffClass($sniffClassName);
35
    }
36
37 8
    private function getForSniffClass(string $sniffClassName) : array
38
    {
39 8
        $sniffCode = SniffNaming::guessCodeByClass($sniffClassName);
40 8
        if (!isset($this->customSniffPropertyValuesBySniffCode[$sniffCode])) {
41 8
            return [];
42
        }
43
44
        return $this->normalizeValues($this->customSniffPropertyValuesBySniffCode[$sniffCode]);
45
    }
46
47 4
    private function addCustomSniffProperty(string $sniffCode, array $properties)
48
    {
49 4
        if (!isset($this->customSniffPropertyValuesBySniffCode[$sniffCode])) {
50 2
            $this->customSniffPropertyValuesBySniffCode[$sniffCode] = [];
51
        }
52
53 4
        $this->customSniffPropertyValuesBySniffCode[$sniffCode] = array_merge(
54 4
            $this->customSniffPropertyValuesBySniffCode[$sniffCode],
55
            $properties
56
        );
57 4
    }
58
59
    private function normalizeValues(array $customSniffPropertyValues) : array
60
    {
61
        foreach ($customSniffPropertyValues as $property => $value) {
62
            $customSniffPropertyValues[$property] = $this->normalizeValue($value);
63
        }
64
65
        return $customSniffPropertyValues;
66
    }
67
68
    /**
69
     * @param mixed $value
70
     * @return mixed
71
     */
72
    private function normalizeValue($value)
73
    {
74
        $value = $this->trimStringValue($value);
75
        return $this->normalizeBoolValue($value);
76
    }
77
78
79
    /**
80
     * @param mixed $value
81
     * @return mixed
82
     */
83
    private function trimStringValue($value)
84
    {
85
        if (is_string($value)) {
86
            return trim($value);
87
        }
88
89
        return $value;
90
    }
91
92
    /**
93
     * @param mixed $value
94
     * @return mixed
95
     */
96
    private function normalizeBoolValue($value)
97
    {
98
        if ($value === 'true' || $value === 'TRUE') {
99
            return true;
100
        }
101
102
        if ($value === 'false' || $value === 'FALSE') {
103
            return false;
104
        }
105
106
        return $value;
107
    }
108
}
109