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\App; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Indexer\InsertTwice; |
14
|
|
|
use Lechimp\Dicto\Indexer\IndexerFactory; |
15
|
|
|
use Lechimp\Dicto\Analysis\ReportGenerator; |
16
|
|
|
use Lechimp\Dicto\Analysis\AnalyzerFactory; |
17
|
|
|
use Lechimp\Dicto\Analysis\Index; |
18
|
|
|
use Lechimp\Dicto\Indexer\Insert; |
19
|
|
|
use Lechimp\Dicto\Graph; |
20
|
|
|
use Psr\Log\LoggerInterface as Log; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The Engine of the App drives the analysis process. |
24
|
|
|
*/ |
25
|
|
|
class Engine { |
26
|
|
|
/** |
27
|
|
|
* @var Log |
28
|
|
|
*/ |
29
|
|
|
protected $log; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Config |
33
|
|
|
*/ |
34
|
|
|
protected $config; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var DBFactory |
38
|
|
|
*/ |
39
|
|
|
protected $db_factory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var IndexerFactory |
43
|
|
|
*/ |
44
|
|
|
protected $indexer_factory; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var AnalyzerFactory |
48
|
|
|
*/ |
49
|
|
|
protected $analyzer_factory; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ReportGenerator |
53
|
|
|
*/ |
54
|
|
|
protected $report_generator; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var SourceStatus |
58
|
|
|
*/ |
59
|
|
|
protected $source_status; |
60
|
|
|
|
61
|
9 |
|
public function __construct( Log $log |
62
|
|
|
, Config $config |
63
|
|
|
, DBFactory $db_factory |
64
|
|
|
, IndexerFactory $indexer_factory |
65
|
|
|
, AnalyzerFactory $analyzer_factory |
66
|
|
|
, ReportGenerator $report_generator |
67
|
|
|
, SourceStatus $source_status |
68
|
|
|
) { |
69
|
9 |
|
$this->log = $log; |
70
|
9 |
|
$this->config = $config; |
71
|
9 |
|
$this->db_factory = $db_factory; |
72
|
9 |
|
$this->indexer_factory = $indexer_factory; |
73
|
9 |
|
$this->analyzer_factory = $analyzer_factory; |
74
|
9 |
|
$this->report_generator = $report_generator; |
75
|
9 |
|
$this->source_status = $source_status; |
76
|
9 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Run the analysis. |
80
|
|
|
* |
81
|
|
|
* @return null |
82
|
|
|
*/ |
83
|
8 |
|
public function run() { |
84
|
8 |
|
$index_db_path = $this->index_database_path(); |
85
|
8 |
|
if (!$this->db_factory->index_db_exists($index_db_path)) { |
86
|
7 |
|
$index = $this->build_index(); |
87
|
7 |
|
$this->run_indexing($index); |
88
|
7 |
|
if ($this->config->analysis_store_index()) { |
89
|
1 |
|
$index->second()->write_cached_inserts(); |
|
|
|
|
90
|
1 |
|
$index = $index->first(); |
|
|
|
|
91
|
1 |
|
} |
92
|
7 |
|
} |
93
|
|
|
else { |
94
|
1 |
|
$index_db = $this->db_factory->load_index_db($index_db_path); |
95
|
1 |
|
$this->log->notice("Reading index from database '$index_db_path'..."); |
96
|
1 |
|
$index = $this->read_index_from($index_db); |
|
|
|
|
97
|
|
|
} |
98
|
8 |
|
$this->run_analysis($index); |
99
|
8 |
|
} |
100
|
|
|
|
101
|
8 |
|
protected function index_database_path() { |
102
|
8 |
|
$commit_hash = $this->source_status->commit_hash(); |
103
|
8 |
|
return $this->config->project_storage()."/$commit_hash.sqlite"; |
104
|
|
|
} |
105
|
|
|
|
106
|
7 |
|
protected function run_indexing(Insert $index) { |
107
|
7 |
|
$this->log->notice("Building index..."); |
108
|
7 |
|
$indexer = $this->indexer_factory->build($index); |
109
|
7 |
|
$this->with_time_measurement |
110
|
|
|
( function ($s) { return "Indexing took $s seconds to run."; } |
111
|
|
|
, function () use ($indexer) { |
112
|
7 |
|
$indexer->index_directory |
113
|
7 |
|
( $this->config->project_root() |
114
|
7 |
|
, $this->config->analysis_ignore() |
115
|
7 |
|
); |
116
|
7 |
|
}); |
117
|
7 |
|
} |
118
|
|
|
|
119
|
8 |
|
protected function run_analysis(Index $index) { |
120
|
8 |
|
$this->log->notice("Running analysis..."); |
121
|
8 |
|
$commit_hash = $this->source_status->commit_hash(); |
122
|
8 |
|
$this->report_generator->begin_run($commit_hash); |
123
|
8 |
|
$analyzer = $this->analyzer_factory->build($index, $this->report_generator); |
124
|
8 |
|
$this->with_time_measurement |
125
|
|
|
( function ($s) { return "Analysis took $s seconds to run."; } |
126
|
|
|
, function () use ($analyzer) { |
127
|
8 |
|
$analyzer->run(); |
128
|
8 |
|
}); |
129
|
8 |
|
$this->report_generator->end_run(); |
130
|
8 |
|
} |
131
|
|
|
|
132
|
7 |
|
protected function build_index() { |
133
|
7 |
|
if ($this->config->analysis_store_index()) { |
134
|
1 |
|
$index_db_path = $this->index_database_path(); |
135
|
1 |
|
$index_db = $this->db_factory->build_index_db($index_db_path); |
136
|
1 |
|
$this->log->notice("Writing index to database '$index_db_path'..."); |
137
|
1 |
|
return new InsertTwice(new Graph\IndexDB, $index_db); |
138
|
|
|
} |
139
|
|
|
|
140
|
6 |
|
return new Graph\IndexDB; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function write_index_to(Graph\IndexDB $index, IndexDB $db) { |
144
|
|
|
$db->write_index($index); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
protected function read_index_from(IndexDB $db) { |
148
|
|
|
$res = null; |
149
|
|
|
$this->with_time_measurement |
150
|
|
|
( function ($s) { return "Loading the index took $s seconds."; } |
151
|
|
|
, function () use ($db, &$res) { |
152
|
|
|
$res = $db->to_graph_index(); |
153
|
|
|
}); |
154
|
|
|
return $res; |
155
|
|
|
} |
156
|
|
|
|
157
|
8 |
|
protected function with_time_measurement(\Closure $message, \Closure $what) { |
158
|
8 |
|
$start_time = microtime(true); |
159
|
8 |
|
$what(); |
160
|
8 |
|
$time_elapsed_secs = microtime(true) - $start_time; |
161
|
8 |
|
$this->log->notice($message($time_elapsed_secs)); |
162
|
8 |
|
} |
163
|
|
|
} |
164
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: