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\Analysis\Index; |
14
|
|
|
use Lechimp\Dicto\Analysis\Violation; |
15
|
|
|
use Lechimp\Dicto\Definition\ArgumentParser; |
16
|
|
|
use Lechimp\Dicto\Indexer\ListenerRegistry; |
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
|
|
|
* TODO: Test if ContainText finds text in files. |
25
|
|
|
*/ |
26
|
|
|
class ContainText extends Schema { |
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
42 |
|
public function name() { |
31
|
42 |
|
return "contain text"; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
7 |
|
public function fetch_arguments(ArgumentParser $parser) { |
38
|
7 |
|
$regexp = $parser->fetch_string(); |
39
|
7 |
|
return array($regexp); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
16 |
View Code Duplication |
public function arguments_are_valid(array &$arguments) { |
|
|
|
|
46
|
16 |
|
if (count($arguments) != 1) { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
16 |
|
$regexp = $arguments[0]; |
50
|
16 |
|
if (!is_string($regexp) || @preg_match("%$regexp%", "") === false) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
16 |
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
8 |
|
public function compile(Index $index, Rule $rule) { |
60
|
8 |
|
$mode = $rule->mode(); |
61
|
8 |
|
$query = $index->query(); |
62
|
8 |
|
$predicate_factory = $query->predicate_factory(); |
63
|
8 |
|
$filter = $rule->checked_on()->compile($predicate_factory); |
64
|
8 |
|
$regexp = $rule->argument(0); |
65
|
|
|
$regexp_filter = function(Graph\Relation $r) use ($regexp) { |
|
|
|
|
66
|
|
|
$start_line = $r->property("start_line"); |
67
|
|
|
$end_line = $r->property("end_line"); |
68
|
|
|
$source = implode |
69
|
|
|
( "\n" |
70
|
|
|
, array_slice |
71
|
|
|
( $r->target()->property("source") |
72
|
|
|
, $start_line - 1 |
73
|
|
|
, $end_line - $start_line |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
return preg_match("%$regexp%", $source) == 1; |
77
|
8 |
|
}; |
78
|
|
|
|
79
|
8 |
|
if ($mode == Rule::MODE_CANNOT || $mode == Rule::MODE_ONLY_CAN) { |
80
|
|
|
return $query |
81
|
5 |
|
->filter($filter) |
|
|
|
|
82
|
5 |
|
->expand_relations(["defined in"]) |
83
|
5 |
|
->filter($this->regexp_source_filter($predicate_factory, $regexp, false)) |
84
|
|
|
->extract(function($e,&$r) use ($rule, $regexp) { |
85
|
1 |
|
$matches = []; |
86
|
1 |
|
$source = $this->get_source_for($e); |
87
|
1 |
|
preg_match("%(.*)$regexp%", $source, $matches); |
88
|
|
|
|
89
|
1 |
|
$file = $e->target(); |
90
|
1 |
|
$r["file"] = $file->property("path"); |
91
|
1 |
|
$start_line = $e->property("start_line"); |
92
|
2 |
|
$found_at_line = substr_count($matches[0], "\n") + 1; |
93
|
1 |
|
$line = $start_line + $found_at_line; |
94
|
1 |
|
$r["line"] = $line + 1; |
95
|
1 |
|
$r["source"] = $file->property("source")[$line]; |
96
|
5 |
|
}); |
97
|
|
|
} |
98
|
3 |
|
if ($mode == Rule::MODE_MUST) { |
99
|
3 |
|
return $index->query() |
100
|
3 |
|
->filter($filter) |
|
|
|
|
101
|
3 |
|
->expand_relations(["defined in"]) |
102
|
3 |
|
->filter($this->regexp_source_filter($predicate_factory, $regexp, true)) |
103
|
|
View Code Duplication |
->extract(function($e,&$r) use ($rule) { |
|
|
|
|
104
|
1 |
|
$file = $e->target(); |
105
|
1 |
|
$r["file"] = $file->property("path"); |
106
|
1 |
|
$line = $e->property("start_line"); |
107
|
1 |
|
$r["line"] = $line; |
108
|
1 |
|
$r["source"] = $file->property("source")[$line - 1]; |
109
|
3 |
|
}); |
110
|
|
|
} |
111
|
|
|
throw new \LogicException("Unknown rule mode: '$mode'"); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Helpers for compile |
115
|
|
|
|
116
|
6 |
|
protected function get_source_for(Graph\Relation $r) { |
117
|
6 |
|
assert('$r->type() == "defined in"'); |
118
|
5 |
|
$start_line = $r->property("start_line"); |
119
|
5 |
|
$end_line = $r->property("end_line"); |
120
|
|
|
return implode |
121
|
5 |
|
( "\n" |
122
|
5 |
|
, array_slice |
123
|
5 |
|
( $r->target()->property("source") |
124
|
5 |
|
, $start_line - 1 |
125
|
5 |
|
, $end_line - $start_line |
126
|
5 |
|
) |
127
|
5 |
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
8 |
|
protected function regexp_source_filter(PredicateFactory $f, $regexp, $negate) { |
131
|
8 |
|
assert('is_string($regexp)'); |
132
|
8 |
|
assert('is_bool($negate)'); |
133
|
8 |
|
return $f->_custom(function(Graph\Relation $r) use ($regexp, $negate) { |
134
|
5 |
|
$source = $this->get_source_for($r); |
135
|
5 |
|
if(!$negate) { |
136
|
3 |
|
return preg_match("%$regexp%", $source) == 1; |
137
|
|
|
} |
138
|
|
|
else { |
139
|
2 |
|
return preg_match("%$regexp%", $source) == 0; |
140
|
|
|
} |
141
|
8 |
|
}); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @inheritdoc |
146
|
|
|
*/ |
147
|
8 |
|
public function pprint(Rule $rule) { |
148
|
8 |
|
return $this->name().' "'.$rule->argument(0).'"'; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* No listeners required for contains text. |
153
|
|
|
* |
154
|
|
|
* @inheritdoc |
155
|
|
|
*/ |
156
|
7 |
|
public function register_listeners(ListenerRegistry $registry) { |
157
|
7 |
|
} |
158
|
|
|
} |
159
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.