Completed
Push — master ( e666ea...addc16 )
by Richard
05:13
created

IndexDB::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\Graph;
12
13
use Lechimp\Dicto\Indexer\Insert;
14
use Lechimp\Dicto\Analysis\Index;
15
16
/**
17
 * A database for the indexer based on graph.
18
 */
19
class IndexDB extends Graph implements Insert, Index {
20
    /**
21
     * @var array<string,Node>
22
     */
23
    protected $globals = [];
24
25
    /**
26
     * @var array<string,Node>
27
     */
28
    protected $language_constructs = []; 
29
30
    /**
31
     * @inheritdocs
32
     */
33 39
    public function _file($path, $source) {
34 39
        assert('is_string($path)');
35 39
        assert('is_string($source)');
36
37 39
        return $this->create_node
38 39
            ( "file"
39 39
            ,   [ "path" => $path
40 39
                , "source" => explode("\n", $source)
41 39
                ]
42 39
            );
43
    }
44
45
    /**
46
     * @inheritdocs
47
     */
48 34 View Code Duplication
    public function _class($name, $file, $start_line, $end_line) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
49 34
        assert('is_string($name)');
50 34
        assert('$file->type() == "file"');
51 34
        assert('is_int($start_line)');
52 34
        assert('is_int($end_line)');
53
54 34
        $class = $this->create_node
55 34
            ( "class"
56 34
            ,   [ "name" => $name
57 34
                ]
58 34
            );
59 34
        $this->add_definition($class, $file, $start_line, $end_line);
60 34
        return $class;
61
    }
62
63
    /**
64
     * @inheritdocs
65
     */
66 26
    public function _method($name, $class, $file, $start_line, $end_line) {
67 26
        assert('is_string($name)');
68 26
        assert('$class->type() == "class"');
69 26
        assert('$file->type() == "file"');
70 26
        assert('is_int($start_line)');
71 26
        assert('is_int($end_line)');
72
73 26
        $method = $this->create_node
74 26
            ( "method"
75 26
            ,   [ "name" => $name
76 26
                ]
77 26
            );
78 26
        $this->add_definition($method, $file, $start_line, $end_line);
79 26
        $this->add_relation($method, "contained in", [], $class);
80 26
        $this->add_relation($class, "contains", [], $method);
81
82 26
        return $method;
83
    }
84
85
    /**
86
     * @inheritdocs
87
     */
88 4 View Code Duplication
    public function _function($name, $file, $start_line, $end_line) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
89 4
        assert('is_string($name)');
90 4
        assert('$file->type() == "file"');
91 4
        assert('is_int($start_line)');
92 4
        assert('is_int($end_line)');
93
94 4
        $function = $this->create_node
95 4
            ( "function"
96 4
            ,   [ "name" => $name
97 4
                ]
98 4
            );
99 4
        $this->add_definition($function, $file, $start_line, $end_line);
100
101 4
        return $function;
102
    }
103
104
    /**
105
     * @inheritdocs
106
     */
107 10 View Code Duplication
    public function _global($name) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
108 10
        assert('is_string($name)');
109
110 10
        if (array_key_exists($name, $this->globals)) {
111 1
            return $this->globals[$name];
112
        }
113
114 10
        $global = $this->create_node("global", ["name" => $name]);
115 10
        $this->globals[$name] = $global;
116
117 10
        return $global;
118
    }
119
120
    /**
121
     * @inheritdocs
122
     */
123 3 View Code Duplication
    public function _language_construct($name) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
124 3
        assert('is_string($name)');
125
126 3
        if (array_key_exists($name, $this->language_constructs)) {
127 1
            return $this->language_constructs[$name];
128
        }
129
130 3
        $language_construct = $this->create_node("language construct", ["name" => $name]);
131 3
        $this->language_constructs[$name] = $language_construct;
132
133 3
        return $language_construct;
134
    }
135
136
    /**
137
     * @inheritdocs
138
     */
139 4 View Code Duplication
    public function _method_reference($name, $file, $line) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
140 4
        assert('is_string($name)');
141 4
        assert('$file->type() == "file"');
142 4
        assert('is_int($line)');
143
144 4
        $method = $this->create_node("method reference", ["name" => $name]);
145 4
        $this->add_relation($method, "referenced at", ["line" => $line], $file);
146
147 4
        return $method;
148
    }
149
150
    /**
151
     * @inheritdocs
152
     */
153 7 View Code Duplication
    public function _function_reference($name, $file, $line) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
154 7
        assert('is_string($name)');
155 7
        assert('$file->type() == "file"');
156 7
        assert('is_int($line)');
157
158 7
        $function = $this->create_node("function reference", ["name" => $name]);
159 7
        $this->add_relation($function, "referenced at", ["line" => $line], $file);
160
161 7
        return $function;
162
    }
163
164
    /**
165
     * @inheritdocs
166
     */
167 11
    public function _relation($left_entity, $relation, $right_entity, $file = null, $line = null) {
168 11
        assert('$left_entity instanceof \\Lechimp\\Dicto\\Graph\\Node');
169 11
        assert('$right_entity instanceof \\Lechimp\\Dicto\\Graph\\Node');
170 11
        assert('is_string($relation)');
171
172 11
        $props = [];
173 11
        if ($file !== null) {
174 11
            assert('$file->type() == "file"');
175 11
            $props["file"] = $file;
176 11
        }
177 11
        if ($line !== null) {
178 11
            assert('is_int($line)');
179 11
            $props["line"] = $line;
180 11
        }
181
182 11
        $this->add_relation($left_entity, $relation, $props, $right_entity);
183 11
    }
184
185
    // Helper
186
187 36
    protected function add_definition(Node $n, Node $file, $start_line, $end_line) {
188 36
        $this->add_relation
189 36
            ( $n
190 36
            , "defined in"
191 36
            ,   [ "start_line" => $start_line
192 36
                , "end_line" => $end_line
193 36
                ]
194 36
            , $file
195 36
            );
196 36
    }
197
}
198