-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathCallbackVisitor.php
More file actions
159 lines (131 loc) · 3.83 KB
/
Copy pathCallbackVisitor.php
File metadata and controls
159 lines (131 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace Psecio\Parse;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Node;
use Psecio\Parse\DocComment\DocCommentFactoryInterface;
/**
* Evaluate rules and call callback on failure
*/
class CallbackVisitor extends NodeVisitorAbstract
{
const ENABLE_TAG = 'psecio\parse\enable';
const DISABLE_TAG = 'psecio\parse\disable';
/**
* @var RuleCollection Rules to evaluate
*/
private $ruleCollection;
/**
* @var array List of enabled rules, stored as ruleName => bool
*/
private $enabledRules;
/**
* @var callable Fail callback
*/
private $callback;
/**
* @var File Current file under evaluation
*/
private $file;
/**
* @var bool If false, ignore all annotations
*/
private $useAnnotations;
/**
* @var DocCommentFactoryInterface
*/
private $docCommentFactory;
/**
* Inject rule collection
*
* @param RuleCollection $ruleCollection
* @param bool $useAnnotations If false, ignore all annotations
*/
public function __construct(RuleCollection $ruleCollection,
DocCommentFactoryInterface $docCommentFactory,
$useAnnotations)
{
$this->ruleCollection = $ruleCollection;
$this->enabledRules = [];
foreach ($this->ruleCollection as $rule) {
$this->enabledRules[strtolower($rule->getName())] = true;
}
$this->useAnnotations = $useAnnotations;
$this->docCommentFactory = $docCommentFactory;
}
/**
* Register failure callback
*
* @param callable $callback
* @return void
*/
public function onNodeFailure(callable $callback)
{
$this->callback = $callback;
}
/**
* Set file under evaluation
*
* @param File $file
* @return void
*/
public function setFile(File $file)
{
$this->file = $file;
}
/**
* Interface function called when node is first hit
*
* @param Node $node
*/
public function enterNode(Node $node)
{
if ($this->useAnnotations) {
$this->updateRuleFilters($node);
}
foreach ($this->ruleCollection as $rule) {
$this->evaluateRule($node, $rule);
}
}
protected function evaluateRule($node, $rule)
{
if (!$this->enabledRules[strtolower($rule->getName())]) {
return;
}
if (!$rule->isValid($node)) {
call_user_func($this->callback, $rule, $node, $this->file);
}
}
public function leaveNode(Node $node)
{
if (!$node->hasAttribute('oldEnabledRules')) {
return;
}
// Restore rules as they were before this node
$this->enabledRules = $node->getAttribute('oldEnabledRules');
}
private function updateRuleFilters($node)
{
$docBlock = $node->getDocComment();
if (empty($docBlock)) {
return;
}
$node->setAttribute('oldEnabledRules', $this->enabledRules);
$this->enabledRules = $this->evaluateDocBlock($docBlock, $this->enabledRules);
}
private function evaluateDocBlock($docBlock, $rules)
{
$comment = $this->docCommentFactory->createDocComment($docBlock);
$this->checkTags($comment, $rules, self::ENABLE_TAG, true);
$this->checkTags($comment, $rules, self::DISABLE_TAG, false);
return $rules;
}
private function checkTags(DocComment\DocCommentInterface $comment, &$rules, $tag, $value)
{
$tags = $comment->getIMatchingTags($tag);
foreach ($tags as $rule) {
// Get the first word from content. This allows you to add comments to rules.
$ruleName = trim(strtolower(strtok($rule, '//')));
$rules[$ruleName] = $value;
}
}
}