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\Query; |
14
|
|
|
use Lechimp\Dicto\Analysis\Violation; |
15
|
|
|
use Lechimp\Dicto\Definition\ArgumentParser; |
16
|
|
|
use Lechimp\Dicto\Indexer\ListenerRegistry; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This checks wheather there is some text in some entity. |
20
|
|
|
* |
21
|
|
|
* TODO: Test if ContainText finds text in files. |
22
|
|
|
*/ |
23
|
|
|
class ContainText extends Schema { |
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
27 |
|
public function name() { |
28
|
27 |
|
return "contain text"; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
5 |
|
public function fetch_arguments(ArgumentParser $parser) { |
35
|
5 |
|
$regexp = $parser->fetch_string(); |
36
|
5 |
|
return array($regexp); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
19 |
View Code Duplication |
public function arguments_are_valid(array &$arguments) { |
|
|
|
|
43
|
19 |
|
if (count($arguments) != 1) { |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
19 |
|
$regexp = $arguments[0]; |
47
|
19 |
|
if (!is_string($regexp) || @preg_match("%$regexp%", "") === false) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
19 |
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
13 |
|
public function compile(Query $query, Rule $rule) { |
57
|
13 |
|
$builder = $query->builder(); |
58
|
13 |
|
$b = $builder->expr(); |
59
|
13 |
|
$mode = $rule->mode(); |
60
|
13 |
|
$checked_on = $rule->checked_on(); |
61
|
13 |
|
$regexp = $rule->argument(0); |
62
|
13 |
|
if ($mode == Rule::MODE_CANNOT || $mode == Rule::MODE_ONLY_CAN) { |
63
|
|
|
return $builder |
|
|
|
|
64
|
9 |
|
->select |
65
|
9 |
|
( "d.name" |
66
|
9 |
|
, "f.path as file" |
67
|
9 |
|
, "src.line" |
68
|
9 |
|
, "src.source" |
69
|
9 |
|
) |
70
|
9 |
|
->from($query->definition_table(), "d") |
71
|
|
|
->join |
72
|
9 |
|
( "d", $query->file_table(), "f" |
73
|
9 |
|
, $b->eq("d.file", "f.id") |
74
|
9 |
|
) |
75
|
|
|
->join |
76
|
9 |
|
( "d", $query->name_table(), "n" |
77
|
9 |
|
, $b->eq("d.name", "n.id") |
78
|
9 |
|
) |
79
|
|
|
->join |
80
|
9 |
|
( "d", $query->source_table(), "src" |
81
|
9 |
|
, $b->andX |
82
|
9 |
|
( $b->gte("src.line", "d.start_line") |
83
|
9 |
|
, $b->lte("src.line", "d.end_line") |
84
|
9 |
|
, $b->eq("src.file", "d.file") |
85
|
9 |
|
, "src.source REGEXP ?" |
86
|
9 |
|
) |
87
|
9 |
|
) |
88
|
|
|
->where |
89
|
9 |
|
( $checked_on->compile($b, "n") |
90
|
9 |
|
) |
91
|
9 |
|
->setParameter(0, $regexp) |
92
|
9 |
|
->execute(); |
93
|
|
|
} |
94
|
4 |
|
if ($mode == Rule::MODE_MUST) { |
95
|
|
|
return $builder |
|
|
|
|
96
|
4 |
|
->select |
97
|
4 |
|
( "d.name" |
98
|
4 |
|
, "f.path as file" |
99
|
4 |
|
, "d.start_line as line" |
100
|
4 |
|
, "src.source" |
101
|
4 |
|
) |
102
|
4 |
|
->from($query->definition_table(), "d") |
103
|
|
|
->join |
104
|
4 |
|
( "d", $query->file_table(), "f" |
105
|
4 |
|
, $b->eq("d.file", "f.id") |
106
|
4 |
|
) |
107
|
|
|
->join |
108
|
4 |
|
( "d", $query->name_table(), "n" |
109
|
4 |
|
, $b->eq("d.name", "n.id") |
110
|
4 |
|
) |
111
|
|
|
->join |
112
|
4 |
|
( "d", $query->source_table(), "src" |
113
|
4 |
|
, $b->andX |
114
|
4 |
|
( $b->eq("src.file", "d.file") |
115
|
4 |
|
, $b->eq("src.line", "d.start_line") |
116
|
4 |
|
) |
117
|
4 |
|
) |
118
|
|
|
->leftJoin |
119
|
4 |
|
( "d", $query->source_table(), "match" |
120
|
4 |
|
, $b->andX |
121
|
4 |
|
( $b->eq("match.file", "d.file") |
122
|
4 |
|
, $b->eq("match.line", "d.start_line") |
123
|
4 |
|
, "match.source REGEXP ?" |
124
|
4 |
|
) |
125
|
4 |
|
) |
126
|
|
|
->where |
127
|
4 |
|
( $checked_on->compile($b, "n") |
128
|
4 |
|
, "match.line IS NULL" |
129
|
4 |
|
) |
130
|
4 |
|
->setParameter(0, $regexp) |
131
|
4 |
|
->execute(); |
132
|
|
|
} |
133
|
|
|
throw new \LogicException("Unknown rule mode: '$mode'"); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritdoc |
138
|
|
|
*/ |
139
|
2 |
|
public function pprint(Rule $rule) { |
140
|
2 |
|
return $this->name().' "'.$rule->argument(0).'"'; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* No listeners required for contains text. |
145
|
|
|
* |
146
|
|
|
* @inheritdoc |
147
|
|
|
*/ |
148
|
10 |
|
public function register_listeners(ListenerRegistry $registry) { |
149
|
10 |
|
} |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
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.