Completed
Push — master ( 605cf4...2dcac2 )
by Richard
05:16
created

Engine::run_analysis()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 1
crap 1
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();
0 ignored issues
show
Bug introduced by
The method second does only exist in Lechimp\Dicto\Indexer\InsertTwice, but not in Lechimp\Dicto\Graph\IndexDB.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
90 1
                $index = $index->first();
0 ignored issues
show
Bug introduced by
The method first does only exist in Lechimp\Dicto\Indexer\InsertTwice, but not in Lechimp\Dicto\Graph\IndexDB.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $index is correct as $this->read_index_from($index_db) (which targets Lechimp\Dicto\App\Engine::read_index_from()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method write_index() does not seem to exist on object<Lechimp\Dicto\App\IndexDB>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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