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; |
9
|
|
|
|
10
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
12
|
|
|
use Symplify\PHP7_CodeSniffer\Event\CheckFileTokenEvent; |
13
|
|
|
use Symplify\PHP7_CodeSniffer\Ruleset\RulesetBuilder; |
14
|
|
|
use Symplify\PHP7_CodeSniffer\SniffFinder\SniffProvider; |
15
|
|
|
|
16
|
|
|
final class Ruleset |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The key is the sniff code |
20
|
|
|
* and the value is the sniff object. |
21
|
|
|
* |
22
|
|
|
* @var Sniff[] |
23
|
|
|
*/ |
24
|
|
|
private $sniffs = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* An array of rules from the ruleset.xml file. |
28
|
|
|
* |
29
|
|
|
* It may be empty, indicating that the ruleset does not override |
30
|
|
|
* any of the default sniff settings. |
31
|
|
|
* |
32
|
|
|
* @var array<string, mixed> |
33
|
|
|
*/ |
34
|
|
|
public $ruleset = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var SniffProvider |
38
|
|
|
*/ |
39
|
|
|
private $sniffProvider; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var RulesetBuilder |
43
|
|
|
*/ |
44
|
|
|
private $rulesetBuilder; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var EventDispatcherInterface |
48
|
|
|
*/ |
49
|
|
|
private $eventDispatcher; |
50
|
|
|
|
51
|
|
|
public function __construct( |
52
|
|
|
SniffProvider $sniffProvider, |
53
|
|
|
RulesetBuilder $rulesetBuilder, |
54
|
|
|
EventDispatcherInterface $eventDispatcher |
55
|
|
|
) { |
56
|
|
|
$this->sniffProvider = $sniffProvider; |
57
|
|
|
$this->rulesetBuilder = $rulesetBuilder; |
58
|
|
|
$this->eventDispatcher = $eventDispatcher; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function createSniffList() |
62
|
|
|
{ |
63
|
|
|
$this->registerSniffs($this->sniffProvider->getActiveSniffs()); |
64
|
|
|
$this->loadSniffsToTokensTheyListenToo(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function registerSniffs(array $sniffClasses) |
68
|
|
|
{ |
69
|
|
|
// these classes should be registered a services |
70
|
|
|
// and collected by event dispatcher, as they subscribe |
71
|
|
|
// to tokens... |
72
|
|
|
foreach ($sniffClasses as $sniffCode => $sniffClass) { |
73
|
|
|
$this->sniffs[$sniffCode] = new $sniffClass; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function loadSniffsToTokensTheyListenToo() |
78
|
|
|
{ |
79
|
|
|
$this->ruleset = $this->rulesetBuilder->getRuleset(); |
80
|
|
|
|
81
|
|
|
foreach ($this->sniffs as $sniffCode => $sniffObject) { |
82
|
|
|
$this->setCustomProperties($sniffCode); |
83
|
|
|
|
84
|
|
|
$tokens = $this->sniffs[$sniffCode]->register(); |
85
|
|
|
foreach ($tokens as $token) { |
86
|
|
|
$this->eventDispatcher->addListener($token, function (CheckFileTokenEvent $checkFileToken) use ($sniffObject) { |
87
|
|
|
$sniffObject->process($checkFileToken->getFile(), $checkFileToken->getStackPointer()); |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function setCustomProperties(string $sniffCode) |
94
|
|
|
{ |
95
|
|
|
if (isset($this->ruleset[$sniffCode]['properties']) === true) { |
96
|
|
|
foreach ($this->ruleset[$sniffCode]['properties'] as $name => $value) { |
97
|
|
|
$this->setSniffProperty($sniffCode, $name, $value); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $sniffCode |
104
|
|
|
* @param string $name |
105
|
|
|
* @param string|array $value |
106
|
|
|
*/ |
107
|
|
|
private function setSniffProperty(string $sniffCode, string $name, $value) |
108
|
|
|
{ |
109
|
|
|
if (isset($this->sniffs[$sniffCode]) === false) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$name = trim($name); |
114
|
|
|
if (is_string($value)) { |
115
|
|
|
$value = trim($value); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Special case for booleans. |
119
|
|
|
if ($value === 'true') { |
120
|
|
|
$value = true; |
121
|
|
|
} elseif ($value === 'false') { |
122
|
|
|
$value = false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->sniffs[$sniffCode]->$name = $value; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|