|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016, 2015 Richard Klees <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This software is licensed under The MIT License. You should have received |
|
8
|
|
|
* a copy of the licence along with the code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Analysis; |
|
12
|
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Variables as Vars; |
|
14
|
|
|
use Lechimp\Dicto\Variables\Variable; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Implementation for Query::compile_var. |
|
18
|
|
|
*/ |
|
19
|
|
|
trait CompilesVars { |
|
20
|
|
|
/** |
|
21
|
|
|
* Get a builder to create queries. |
|
22
|
|
|
* |
|
23
|
|
|
* @return QueryBuilder |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract public function builder(); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Compile a variable to an SQL statement over a named table. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $name |
|
|
|
|
|
|
31
|
|
|
* @param Vars\Variable $var |
|
32
|
|
|
* @param bool $negate |
|
33
|
|
|
* @return string|CompositeExpression |
|
34
|
|
|
*/ |
|
35
|
46 |
|
public function compile_var($table_name, Vars\Variable $var, $negate = false) { |
|
36
|
46 |
|
$b = $this->builder()->expr(); |
|
37
|
|
|
// Since SQL does not have a statement for negating while expressions, |
|
38
|
|
|
// we need to negate the single conditions in the expression, which |
|
39
|
|
|
// most often is the equality operator here. |
|
40
|
46 |
|
if (!$negate) { |
|
41
|
|
|
$eq_op = function($l, $r) use ($b) { |
|
42
|
46 |
|
return $b->eq($l, $r); |
|
43
|
46 |
|
}; |
|
44
|
46 |
|
} |
|
45
|
|
|
else { |
|
46
|
18 |
|
$eq_op = function($l, $r) use ($b) { |
|
47
|
18 |
|
return $b->neq($l, $r); |
|
48
|
18 |
|
}; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// Pattern matching on variable type. |
|
52
|
|
|
|
|
53
|
46 |
|
if ($var instanceof Vars\AsWellAs) { |
|
54
|
|
|
// normal case: $left_condition or $right_condition |
|
|
|
|
|
|
55
|
3 |
View Code Duplication |
if (!$negate) { |
|
|
|
|
|
|
56
|
3 |
|
return $b->orX |
|
57
|
3 |
|
( $this->compile_var($table_name, $var->left()) |
|
58
|
3 |
|
, $this->compile_var($table_name, $var->right()) |
|
59
|
3 |
|
); |
|
60
|
|
|
} |
|
61
|
|
|
// negated case: not ($left_condition or $right_condition) |
|
|
|
|
|
|
62
|
|
|
// = not $left_condition and not $right_condition |
|
63
|
|
View Code Duplication |
if ($negate) { |
|
|
|
|
|
|
64
|
|
|
return $b->andX |
|
65
|
|
|
( $this->compile_var($table_name, $var->left(), true) |
|
66
|
|
|
, $this->compile_var($table_name, $var->right(), true) |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
46 |
View Code Duplication |
if ($var instanceof Vars\ButNot) { |
|
|
|
|
|
|
71
|
18 |
|
return $b->andX |
|
72
|
18 |
|
( $this->compile_var($table_name, $var->left()) |
|
73
|
18 |
|
, $this->compile_var($table_name, $var->right(), true) |
|
74
|
18 |
|
); |
|
75
|
|
|
} |
|
76
|
46 |
|
if ($var instanceof Vars\Classes) { |
|
77
|
41 |
|
return $eq_op("$table_name.type", $b->literal(Variable::CLASS_TYPE)); |
|
78
|
|
|
} |
|
79
|
43 |
|
if ($var instanceof Vars\Everything) { |
|
80
|
23 |
|
return $eq_op($b->literal(1), $b->literal(1)); |
|
81
|
|
|
} |
|
82
|
43 |
|
if ($var instanceof Vars\Files) { |
|
83
|
|
|
return $eq_op("$table_name.type", $b->literal(Variable::FILE_TYPE)); |
|
84
|
|
|
} |
|
85
|
43 |
|
if ($var instanceof Vars\Functions) { |
|
86
|
12 |
|
return $eq_op("$table_name.type", $b->literal(Variable::FUNCTION_TYPE)); |
|
87
|
|
|
} |
|
88
|
40 |
|
if ($var instanceof Vars\Globals) { |
|
89
|
19 |
|
return $eq_op("$table_name.type", $b->literal(Variable::GLOBAL_TYPE)); |
|
90
|
|
|
} |
|
91
|
34 |
|
if ($var instanceof Vars\LanguageConstruct) { |
|
92
|
|
|
// normal case : language construct and name matches |
|
93
|
7 |
|
if (!$negate) { |
|
94
|
7 |
|
return $b->andX |
|
95
|
7 |
|
( $eq_op("$table_name.type", $b->literal(Variable::LANGUAGE_CONSTRUCT_TYPE)) |
|
96
|
7 |
|
, $eq_op("$table_name.name", $b->literal($var->construct_name())) |
|
97
|
7 |
|
); |
|
98
|
|
|
} |
|
99
|
|
|
// negated case: not (language construct and name matches) |
|
100
|
|
|
// = not language construct or not name matches |
|
101
|
|
|
else { |
|
102
|
|
|
|
|
103
|
|
|
return $b->orX |
|
104
|
|
|
( $eq_op("$table_name.type", $b->literal(Variable::LANGUAGE_CONSTRUCT_TYPE)) |
|
105
|
|
|
, $eq_op("$table_name.name", $b->literal($var->construct_name())) |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
29 |
|
if ($var instanceof Vars\Methods) { |
|
110
|
|
|
return $eq_op("$table_name.type", $b->literal(Variable::METHOD_TYPE)); |
|
111
|
|
|
} |
|
112
|
29 |
|
if ($var instanceof Vars\WithName) { |
|
113
|
|
|
// normal case : $condition_left AND regexp matches |
|
114
|
29 |
|
if (!$negate) { |
|
115
|
11 |
|
return $b->andX |
|
116
|
11 |
|
( $this->compile_var($table_name, $var->variable()) |
|
117
|
11 |
|
, "$table_name.name REGEXP ".$b->literal('^'.$var->regexp().'$') |
|
118
|
11 |
|
); |
|
119
|
|
|
} |
|
120
|
|
|
// negated case: not ($condition_left AND regexp matches) |
|
|
|
|
|
|
121
|
|
|
// = not $condition_left OR not regexp matches |
|
122
|
|
|
else { |
|
123
|
18 |
|
return $b->orX |
|
124
|
18 |
|
( $this->compile_var($table_name, $var->variable(), true) |
|
125
|
18 |
|
, "$table_name.name NOT REGEXP ".$b->literal('^'.$var->regexp().'$') |
|
126
|
18 |
|
); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
throw new \LogicException("Can't compile var-type '".get_class($var)."'"); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.