1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Richard Klees <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This software is licensed under The MIT License. You should have received |
8
|
|
|
* a copy of the license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Rules; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Regexp; |
14
|
|
|
use Lechimp\Dicto\Analysis\Index; |
15
|
|
|
use Lechimp\Dicto\Analysis\Violation; |
16
|
|
|
use Lechimp\Dicto\Definition\ArgumentParser; |
17
|
|
|
use Lechimp\Dicto\Graph\Node; |
18
|
|
|
use Lechimp\Dicto\Graph\PredicateFactory; |
19
|
|
|
use Lechimp\Dicto\Graph; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This checks wheather there is some text in some entity. |
23
|
|
|
*/ |
24
|
|
|
class ContainText extends Schema { |
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
52 |
|
public function name() { |
29
|
52 |
|
return "contain text"; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
7 |
|
public function fetch_arguments(ArgumentParser $parser) { |
36
|
7 |
|
$regexp = new Regexp($parser->fetch_string()); |
37
|
7 |
|
return array($regexp); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
20 |
|
public function arguments_are_valid(array &$arguments) { |
44
|
20 |
|
if (count($arguments) != 1 || !($arguments[0] instanceof Regexp)) { |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
20 |
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
12 |
|
public function compile(Index $index, Rule $rule) { |
54
|
12 |
|
$mode = $rule->mode(); |
55
|
12 |
|
$pred_factory = $index->query()->predicate_factory(); |
56
|
12 |
|
$filter = $rule->checked_on()->compile($pred_factory); |
57
|
|
|
// ContainText needs to have to kinds of queries, one for files, one |
58
|
|
|
// for none-files. To make the queries faster, we separate the filters |
59
|
|
|
// to the different types. |
60
|
12 |
|
$filter_non_files = $pred_factory->_and |
61
|
12 |
|
([$pred_factory->_not($pred_factory->_type_is("file")) |
62
|
12 |
|
, $filter |
63
|
12 |
|
]); |
64
|
12 |
|
$filter_files = $pred_factory->_and |
65
|
12 |
|
([$pred_factory->_type_is("file") |
66
|
12 |
|
, $filter |
67
|
12 |
|
]); |
68
|
12 |
|
$regexp = $rule->argument(0); |
69
|
|
|
// TODO: This behaviour might better be encoded as a method on regexp. |
70
|
|
|
// This is needed in get_source_location to find the number of |
71
|
|
|
// the line the regexp was found. |
72
|
12 |
|
$loc_regexp = new Regexp(".*".$rule->argument(0)->raw()); |
73
|
|
|
|
74
|
12 |
|
if ($mode == Rule::MODE_CANNOT || $mode == Rule::MODE_ONLY_CAN) { |
75
|
|
|
return |
76
|
9 |
|
[ $index->query() |
77
|
9 |
|
->filter($filter_non_files) |
|
|
|
|
78
|
9 |
|
->expand_relations(["defined in"]) |
79
|
9 |
|
->filter($this->regexp_source_filter($pred_factory, $regexp, false)) |
80
|
|
|
->extract(function($e,&$r) use ($regexp, $loc_regexp) { |
81
|
1 |
|
$file = $e->target(); |
82
|
1 |
|
$found_at_line = $this->get_source_location($e, $loc_regexp); |
83
|
1 |
|
$start_line = $e->property("start_line"); |
84
|
1 |
|
$line = $start_line + $found_at_line - 1; |
85
|
|
|
// -1 is for the line where the class starts, this would |
86
|
|
|
// count double otherwise. |
87
|
1 |
|
$r["file"] = $file->property("path"); |
88
|
1 |
|
$r["line"] = $line; |
89
|
1 |
|
$r["source"] = $file->property("source")[$line-1]; |
90
|
9 |
|
}) |
91
|
9 |
|
, $index->query() |
92
|
9 |
|
->filter($filter_files) |
|
|
|
|
93
|
9 |
|
->filter($this->regexp_source_filter($pred_factory, $regexp, false)) |
94
|
|
|
->extract(function($e,&$r) use ($regexp, $loc_regexp) { |
95
|
2 |
|
$line = $this->get_source_location($e, $loc_regexp); |
96
|
2 |
|
$r["file"] = $e->property("path"); |
97
|
2 |
|
$r["line"] = $line; |
98
|
2 |
|
$r["source"] = $e->property("source")[$line-1]; |
99
|
9 |
|
}) |
100
|
9 |
|
]; |
101
|
|
|
} |
102
|
3 |
|
if ($mode == Rule::MODE_MUST) { |
103
|
|
|
return |
104
|
3 |
|
[ $index->query() |
105
|
3 |
|
->filter($filter_non_files) |
|
|
|
|
106
|
3 |
|
->expand_relations(["defined in"]) |
107
|
4 |
|
->filter($this->regexp_source_filter($pred_factory, $regexp, true)) |
108
|
|
View Code Duplication |
->extract(function($e,&$r) use ($rule) { |
|
|
|
|
109
|
1 |
|
$file = $e->target(); |
110
|
1 |
|
$r["file"] = $file->property("path"); |
111
|
1 |
|
$line = $e->property("start_line"); |
112
|
1 |
|
$r["line"] = $line; |
113
|
1 |
|
$r["source"] = $file->property("source")[$line - 1]; |
114
|
3 |
|
}) |
115
|
|
|
// TODO: add implementation for files here. |
116
|
3 |
|
]; |
117
|
|
|
} |
118
|
|
|
throw new \LogicException("Unknown rule mode: '$mode'"); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Helpers for compile |
122
|
|
|
|
123
|
9 |
|
protected function get_source_for(Graph\Entity $e) { |
124
|
9 |
|
if ($e->type() == "defined in") { |
125
|
5 |
|
return $this->get_source_for_defined_in($e); |
|
|
|
|
126
|
|
|
} |
127
|
4 |
|
if ($e->type() == "file") { |
128
|
5 |
|
return $this->get_source_for_file($e); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
throw new \LogicException( |
131
|
|
|
"Can't get source for entity with type '".$e->type()."'"); |
132
|
|
|
} |
133
|
|
|
|
134
|
5 |
|
protected function get_source_for_defined_in(Graph\Relation $r) { |
135
|
5 |
|
assert('$r->type() == "defined in"'); |
136
|
5 |
|
$start_line = $r->property("start_line"); |
137
|
5 |
|
$end_line = $r->property("end_line"); |
138
|
|
|
return implode |
139
|
5 |
|
( "\n" |
140
|
5 |
|
, array_slice |
141
|
5 |
|
( $r->target()->property("source") |
142
|
5 |
|
, $start_line - 1 |
143
|
5 |
|
, $end_line - $start_line |
144
|
5 |
|
) |
145
|
5 |
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
4 |
|
protected function get_source_for_file(Graph\Node $f) { |
149
|
4 |
|
assert('$f->type() == "file"'); |
150
|
|
|
return implode |
151
|
4 |
|
( "\n" |
152
|
4 |
|
, $f->property("source") |
153
|
4 |
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
12 |
|
protected function regexp_source_filter(PredicateFactory $f, Regexp $regexp, $negate) { |
157
|
12 |
|
assert('is_bool($negate)'); |
158
|
12 |
|
return $f->_custom(function(Graph\Entity $e) use ($regexp, $negate) { |
159
|
9 |
|
$source = $this->get_source_for($e); |
160
|
9 |
|
if(!$negate) { |
161
|
7 |
|
return $regexp->search($source); |
162
|
|
|
} |
163
|
|
|
else { |
164
|
2 |
|
return !$regexp->search($source); |
165
|
|
|
} |
166
|
12 |
|
}); |
167
|
|
|
} |
168
|
|
|
|
169
|
3 |
|
protected function get_source_location(Graph\Entity $e, Regexp $regexp) { |
170
|
3 |
|
$matches = []; |
171
|
3 |
|
$source = $this->get_source_for($e); |
172
|
3 |
|
$regexp->search($source, true, $matches); |
173
|
3 |
|
return substr_count($matches[0], "\n") + 1; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @inheritdoc |
178
|
|
|
*/ |
179
|
12 |
|
public function pprint(Rule $rule) { |
180
|
12 |
|
return $this->name().' "'.$rule->argument(0)->raw().'"'; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: