public function run()
{
if (!file_exists($this->config->projects_root . '/projects/' . $this->config->project)) {
throw new NoSuchProject($this->config->project);
}
$res = $this->gremlin->query('g.V().hasLabel("Project").values("fullcode")');
if ($res->results[0] !== $this->config->project) {
throw new NotProjectInGraph($this->config->project, $res->results[0]);
}
// move this to .dump.sqlite then rename at the end, or any imtermediate time
// Mention that some are not yet arrived in the snitch
$this->sqliteFile = $this->config->projects_root . '/projects/' . $this->config->project . '/.dump.sqlite';
$this->sqliteFileFinal = $this->config->projects_root . '/projects/' . $this->config->project . '/dump.sqlite';
if (file_exists($this->sqliteFile)) {
unlink($this->sqliteFile);
display('Removing old .dump.sqlite');
}
$this->addSnitch();
Analyzer::initDocs();
Analyzer::$gremlinStatic = $this->gremlin;
if ($this->config->update === true) {
copy($this->sqliteFileFinal, $this->sqliteFile);
$sqlite = new \Sqlite3($this->sqliteFile);
} else {
$sqlite = new \Sqlite3($this->sqliteFile);
$this->getAtomCounts($sqlite);
$this->collectStructures($sqlite);
$sqlite->query('CREATE TABLE themas ( id INTEGER PRIMARY KEY AUTOINCREMENT,
thema STRING
)');
$sqlite->query('CREATE TABLE results ( id INTEGER PRIMARY KEY AUTOINCREMENT,
fullcode STRING,
file STRING,
line INTEGER,
namespace STRING,
class STRING,
function STRING,
analyzer STRING,
severity STRING
)');
$sqlite->query('CREATE TABLE resultsCounts ( id INTEGER PRIMARY KEY AUTOINCREMENT,
analyzer STRING,
count INTEGER DEFAULT -6)');
display('Inited tables');
}
$sqlQuery = <<<SQL
DELETE FROM results WHERE analyzer = :analyzer
SQL;
$this->cleanResults = $sqlite->prepare($sqlQuery);
$sqlQuery = <<<SQL
REPLACE INTO results ("id", "fullcode", "file", "line", "namespace", "class", "function", "analyzer", "severity")
VALUES ( NULL, :fullcode, :file, :line, :namespace, :class, :function, :analyzer, :severity )
SQL;
$this->stmtResults = $sqlite->prepare($sqlQuery);
$sqlQuery = <<<SQL
REPLACE INTO resultsCounts ("id", "analyzer", "count") VALUES (NULL, :class, :count )
SQL;
$this->stmtResultsCounts = $sqlite->prepare($sqlQuery);
$themes = array();
if ($this->config->thema !== null) {
$thema = $this->config->thema;
$themes = Analyzer::getThemeAnalyzers($thema);
if (empty($themes)) {
$r = Analyzer::getSuggestionThema($thema);
if (count($r) > 0) {
echo 'did you mean : ', implode(', ', str_replace('_', '/', $r)), "\n";
}
throw new NoSuchThema($thema);
}
display('Processing thema : ' . $thema);
} elseif ($this->config->program !== null) {
$analyzer = $this->config->program;
if (!Analyzer::getClass($analyzer)) {
$r = Analyzer::getSuggestionClass($analyzer);
if (count($r) > 0) {
echo 'did you mean : ', implode(', ', str_replace('_', '/', $r)), "\n";
}
throw new NoSuchAnalyzer($analyzer);
}
$themes = array($analyzer);
display('Processing one analyzer : ' . $analyzer);
} else {
display('No analysis dump requested (-T <thema> | -P <Analyzer>)');
$this->finish();
return;
}
/*
$res = $sqlite->query('SELECT COUNT(*) FROM themas WHERE thema="'.$thema.'"');
$count = $res->fetchArray(\SQLITE3_NUM);
if ($count === 1) {
display("$thema was already run\n");
} else {
display("$thema was not already run\n");
}
die();
print_r($themes);
*/
$sqlitePath = $this->config->projects_root . '/projects/' . $this->config->project . '/datastore.sqlite';
$counts = array();
$datastore = new \Sqlite3($sqlitePath, \SQLITE3_OPEN_READONLY);
$datastore->busyTimeout(5000);
$res = $datastore->query('SELECT * FROM analyzed');
while ($row = $res->fetchArray(\SQLITE3_ASSOC)) {
$counts[$row['analyzer']] = $row['counts'];
}
$this->log->log('count analyzed : ' . count($counts) . "\n");
$this->log->log('counts ' . implode(', ', $counts) . "\n");
$datastore->close();
unset($datastore);
foreach ($themes as $id => $thema) {
if (isset($counts[$thema])) {
display($thema . ' : ' . ($counts[$thema] >= 0 ? 'Yes' : 'N/A') . "\n");
$this->processResults($thema, $counts[$thema]);
unset($themes[$id]);
} else {
display($thema . " : No\n");
}
}
$this->log->log('Still ' . count($themes) . " to be processed\n");
display('Still ' . count($themes) . " to be processed\n");
if (count($themes) === 0) {
if ($this->config->thema !== null) {
$sqlite->query('INSERT INTO themas ("id", "thema") VALUES ( NULL, "' . $this->config->thema . '")');
}
}
$this->finish();
}