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 licence along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Rules; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Definition as Def; |
14
|
|
|
use Lechimp\Dicto\Analysis\Query; |
15
|
|
|
use Lechimp\Dicto\Analysis\Violation; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This checks wheather there is some text in the definition of an entity. |
19
|
|
|
*/ |
20
|
|
|
class ContainText extends Property { |
21
|
|
|
/** |
22
|
|
|
* @inheritdoc |
23
|
|
|
*/ |
24
|
287 |
|
public function name() { |
25
|
287 |
|
return "contain_text"; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
50 |
|
public function check_arguments(array $arguments) { |
32
|
50 |
|
if (count($arguments) != 1) { |
33
|
|
|
throw new \InvalidArgumentException( |
34
|
|
|
"One argument is required when using a contain text."); |
35
|
|
|
} |
36
|
50 |
|
$regexp = $arguments[0]; |
37
|
50 |
View Code Duplication |
if (!is_string($regexp) || @preg_match("%$regexp%", "") === false) { |
|
|
|
|
38
|
1 |
|
throw new \InvalidArgumentException( |
39
|
1 |
|
"Invalid regexp '$regexp' when using contain text."); |
40
|
|
|
} |
41
|
49 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
12 |
|
public function compile(Query $query, Rule $rule) { |
47
|
12 |
|
$builder = $query->builder(); |
48
|
12 |
|
$mode = $rule->mode(); |
49
|
12 |
|
$checked_on = $rule->checked_on(); |
50
|
12 |
|
$regexp = $rule->argument(0); |
51
|
12 |
View Code Duplication |
if ($mode == Rule::MODE_CANNOT || $mode == Rule::MODE_ONLY_CAN) { |
|
|
|
|
52
|
|
|
return $builder |
53
|
8 |
|
->select |
54
|
8 |
|
( "id as entity_id" |
55
|
8 |
|
, "file" |
56
|
8 |
|
, "source" |
57
|
8 |
|
) |
58
|
8 |
|
->from($query->entity_table()) |
59
|
|
|
->where |
60
|
8 |
|
( $query->compile_var($query->entity_table(), $checked_on) |
61
|
8 |
|
, "source REGEXP ?" |
62
|
8 |
|
) |
63
|
8 |
|
->setParameter(0, $regexp) |
64
|
8 |
|
->execute(); |
65
|
|
|
} |
66
|
4 |
View Code Duplication |
if ($mode == Rule::MODE_MUST) { |
|
|
|
|
67
|
|
|
return $builder |
68
|
4 |
|
->select |
69
|
4 |
|
( "id as entity_id" |
70
|
4 |
|
, "file" |
71
|
4 |
|
, "source" |
72
|
4 |
|
) |
73
|
4 |
|
->from($query->entity_table()) |
74
|
|
|
->where |
75
|
4 |
|
( $query->compile_var($query->entity_table(), $checked_on) |
76
|
4 |
|
, "source NOT REGEXP ?" |
77
|
4 |
|
) |
78
|
4 |
|
->setParameter(0, $regexp) |
79
|
4 |
|
->execute(); |
80
|
|
|
} |
81
|
|
|
throw new \LogicException("Unknown rule mode: '$mode'"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritdoc |
86
|
|
|
*/ |
87
|
1 |
|
public function to_violation(Rule $rule, array $row) { |
88
|
1 |
|
$line_no = 0; |
89
|
1 |
|
$line = null; |
90
|
1 |
|
$lines = explode("\n", $row["source"]); |
91
|
1 |
|
$pattern = $rule->argument(0); |
92
|
1 |
|
foreach ($lines as $l) { |
93
|
1 |
|
$line_no++; |
94
|
1 |
|
if (preg_match("%$pattern%", $l) > 0) { |
95
|
1 |
|
$line = $l; |
96
|
1 |
|
break; |
97
|
|
|
} |
98
|
1 |
|
} |
99
|
1 |
|
if ($line === null) { |
100
|
|
|
throw new \LogicException( |
101
|
|
|
"Found '$pattern' with SQL query but not in postprocessing..."); |
102
|
|
|
} |
103
|
|
|
return new Violation |
104
|
1 |
|
( $rule |
105
|
1 |
|
, $row["file"] |
106
|
1 |
|
, $line_no |
107
|
1 |
|
, $line |
108
|
1 |
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritdoc |
113
|
|
|
*/ |
114
|
|
|
public function pprint(Rule $rule) { |
115
|
|
|
return $this->printable_name().' "'.$rule->argument(0).'"'; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.