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
|
|
|
|