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 license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Analysis; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Rules\Ruleset; |
14
|
|
|
use Lechimp\Dicto\Variables\Variable; |
15
|
|
|
use Psr\Log\LoggerInterface as Log; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Performs the actual analysis of a ruleset over a query-object |
19
|
|
|
* using a specific rules to sql compiler. |
20
|
|
|
*/ |
21
|
|
|
class Analyzer { |
22
|
|
|
/** |
23
|
|
|
* @var Log |
24
|
|
|
*/ |
25
|
|
|
protected $log; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Ruleset |
29
|
|
|
*/ |
30
|
|
|
protected $ruleset; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Index |
34
|
|
|
*/ |
35
|
|
|
protected $index; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ReportGenerator |
39
|
|
|
*/ |
40
|
|
|
protected $generator; |
41
|
|
|
|
42
|
23 |
|
public function __construct |
43
|
|
|
( Log $log |
44
|
|
|
, Ruleset $ruleset |
45
|
|
|
, Index $index |
46
|
|
|
, ReportGenerator $generator |
47
|
|
|
) { |
48
|
23 |
|
$this->log = $log; |
49
|
23 |
|
$this->ruleset = $ruleset; |
50
|
23 |
|
$this->index = $index; |
51
|
23 |
|
$this->generator = $generator; |
52
|
23 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Run the analysis. |
56
|
|
|
* |
57
|
|
|
* @return null |
58
|
|
|
*/ |
59
|
23 |
|
public function run() { |
60
|
23 |
|
$this->generator->begin_ruleset($this->ruleset); |
61
|
23 |
|
foreach ($this->ruleset->rules() as $rule) { |
62
|
23 |
|
$this->log->info("checking: ".$rule->pprint()); |
63
|
23 |
|
$this->generator->begin_rule($rule); |
64
|
23 |
|
$query = $rule->compile($this->index); |
65
|
23 |
|
$results = $query->run(["rule" => $rule]); |
66
|
23 |
|
foreach ($results as $row) { |
67
|
9 |
|
$this->generator->report_violation($this->build_violation($row)); |
68
|
23 |
|
} |
69
|
23 |
|
$this->generator->end_rule($rule); |
70
|
23 |
|
} |
71
|
23 |
|
$this->generator->end_ruleset($this->ruleset); |
72
|
23 |
|
} |
73
|
|
|
|
74
|
9 |
|
public function build_violation($info) { |
75
|
9 |
|
assert('array_key_exists("rule", $info)'); |
76
|
9 |
|
assert('$info["rule"] instanceof \\Lechimp\\Dicto\\Rules\\Rule'); |
77
|
9 |
|
assert('array_key_exists("file", $info)'); |
78
|
9 |
|
assert('is_string($info["file"])'); |
79
|
9 |
|
assert('array_key_exists("line", $info)'); |
80
|
9 |
|
assert('is_int($info["line"])'); |
81
|
9 |
|
assert('array_key_exists("source", $info)'); |
82
|
9 |
|
assert('is_string($info["source"])'); |
83
|
|
|
return new Violation |
84
|
9 |
|
( $info["rule"] |
85
|
9 |
|
, $info["file"] |
86
|
9 |
|
, $info["line"] |
87
|
9 |
|
, $info["source"] |
88
|
9 |
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|