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\Definition\ArgumentParser; |
15
|
|
|
use Lechimp\Dicto\Indexer\Insert; |
16
|
|
|
use Lechimp\Dicto\Indexer\Location; |
17
|
|
|
use Lechimp\Dicto\Variables\Variable; |
18
|
|
|
use Lechimp\Dicto\Graph\Node; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This is a rule that checks a relation between two entities |
22
|
|
|
* in the code. |
23
|
|
|
*/ |
24
|
|
|
abstract class Relation extends Schema { |
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
4 |
|
public function fetch_arguments(ArgumentParser $parser) { |
29
|
4 |
|
$var = $parser->fetch_variable(); |
30
|
4 |
|
return array($var); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
37 |
|
public function arguments_are_valid(array &$arguments) { |
37
|
37 |
|
if (count($arguments) != 1) { |
38
|
|
|
return false; |
39
|
|
|
} |
40
|
37 |
|
if (!($arguments[0] instanceof Variable)) { |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
37 |
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
29 |
|
public function pprint(Rule $rule) { |
50
|
29 |
|
return $this->name()." ".$rule->argument(0)->name(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
17 |
|
public function compile(Index $index, Rule $rule) { |
57
|
17 |
|
$mode = $rule->mode(); |
58
|
17 |
|
$var_left = $rule->checked_on(); |
59
|
17 |
|
$var_right = $rule->argument(0); |
60
|
17 |
|
$query = $index->query(); |
61
|
17 |
|
$predicate_factory = $query->predicate_factory(); |
62
|
17 |
|
if ($mode == Rule::MODE_CANNOT || $mode == Rule::MODE_ONLY_CAN) { |
63
|
11 |
|
$filter_left = $var_left->compile($predicate_factory); |
64
|
11 |
|
$filter_right = $var_right->compile($predicate_factory); |
65
|
|
|
return [$query |
66
|
11 |
|
->filter($filter_left) |
|
|
|
|
67
|
11 |
|
->expand_relations([$this->name()]) |
68
|
|
View Code Duplication |
->extract(function($e,&$r) use ($rule) { |
|
|
|
|
69
|
6 |
|
$file = $e->property("file"); |
70
|
6 |
|
assert('$file->type() == "file"'); |
71
|
6 |
|
$r["file"] = $file->property("path"); |
72
|
6 |
|
$line = $e->property("line"); |
73
|
6 |
|
$r["line"] = $line; |
74
|
6 |
|
$r["source"] = $file->property("source")[$line - 1]; |
75
|
11 |
|
}) |
76
|
11 |
|
->expand_target() |
77
|
11 |
|
->filter($filter_right)] |
78
|
11 |
|
; |
79
|
|
|
} |
80
|
6 |
|
if ($mode == Rule::MODE_MUST) { |
81
|
6 |
|
$filter_left = $var_left->compile($predicate_factory); |
82
|
6 |
|
$filter_right = $var_right->compile($predicate_factory)->compile(); |
83
|
|
|
return [$query |
84
|
6 |
|
->filter($filter_left) |
|
|
|
|
85
|
|
|
->filter($predicate_factory->_custom(function(Node $n) use ($filter_right) { |
86
|
|
|
$rels = $n->relations(function($r) { |
87
|
6 |
|
return $r->type() == $this->name(); |
88
|
6 |
|
}); |
89
|
6 |
|
if (count($rels) == 0) { |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
6 |
|
foreach ($rels as $rel) { |
93
|
6 |
|
if ($filter_right($rel->target())) { |
94
|
3 |
|
return false; |
95
|
|
|
} |
96
|
4 |
|
} |
97
|
3 |
|
return true; |
98
|
6 |
|
})) |
99
|
|
|
->extract(function($e,&$r) use ($index, $rule) { |
100
|
3 |
|
$rels = $e->relations(function($r) { |
101
|
3 |
|
return $r->type() == "defined in"; |
102
|
3 |
|
}); |
103
|
3 |
|
assert('count($rels) == 1'); |
104
|
3 |
|
$file = $rels[0]->target(); |
105
|
3 |
|
assert('$file->type() == "file"'); |
106
|
3 |
|
$r["file"] = $file->property("path"); |
107
|
3 |
|
$line = $rels[0]->property("start_line"); |
108
|
3 |
|
$r["line"] = $line; |
109
|
3 |
|
$r["source"] = $file->property("source")[$line - 1]; |
110
|
6 |
|
})] |
111
|
6 |
|
; |
112
|
|
|
} |
113
|
|
|
throw new \LogicException("Unknown rule mode: '$mode'"); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Insert this relation somewhere, where it is recorded for all |
118
|
|
|
* entities that the current location is in. |
119
|
|
|
* |
120
|
|
|
* @param Insert $insert |
121
|
|
|
* @param Location $location |
122
|
|
|
* @param mixed $other |
123
|
|
|
* @return null |
124
|
|
|
*/ |
125
|
23 |
|
protected function insert_relation_into(Insert $insert, Location $location, $other) { |
126
|
23 |
|
foreach ($location->in_entities() as $entity) { |
127
|
23 |
|
if ($entity[0] == Variable::FILE_TYPE) { |
128
|
23 |
|
continue; |
129
|
|
|
} |
130
|
23 |
|
$insert->_relation |
131
|
23 |
|
( $entity[1] |
132
|
23 |
|
, $this->name() |
133
|
23 |
|
, $other |
134
|
23 |
|
, $location->file() |
135
|
23 |
|
, $location->line() |
136
|
23 |
|
); |
137
|
23 |
|
} |
138
|
23 |
|
} |
139
|
|
|
} |
140
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: