Completed
Push — master ( f7852e...e666ea )
by Richard
06:08
created

IndexDB::_language_construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
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 50
    public function _file($path, $source) {
34 50
        assert('is_string($path)');
35 50
        assert('is_string($source)');
36
37 50
        return $this->create_node
38 50
            ( "file"
39 50
            ,   [ "path" => $path
40 50
                , "source" => explode("\n", $source)
41 50
                ]
42 50
            );
43
    }
44
45
    /**
46
     * @inheritdocs
47
     */
48 45 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 45
        assert('is_string($name)');
50 45
        assert('$file->type() == "file"');
51 45
        assert('is_int($start_line)');
52 45
        assert('is_int($end_line)');
53
54 45
        $class = $this->create_node
55 45
            ( "class"
56 45
            ,   [ "name" => $name
57 45
                ]
58 45
            );
59 45
        $this->add_definition($class, $file, $start_line, $end_line);
60 45
        return $class;
61
    }
62
63
    /**
64
     * @inheritdocs
65
     */
66 33
    public function _method($name, $class, $file, $start_line, $end_line) {
67 33
        assert('is_string($name)');
68 33
        assert('$class->type() == "class"');
69 33
        assert('$file->type() == "file"');
70 33
        assert('is_int($start_line)');
71 33
        assert('is_int($end_line)');
72
73 33
        $method = $this->create_node
74 33
            ( "method"
75 33
            ,   [ "name" => $name
76 33
                ]
77 33
            );
78 33
        $this->add_definition($method, $file, $start_line, $end_line);
79 33
        $this->add_relation($method, "contained in", [], $class);
80 33
        $this->add_relation($class, "contains", [], $method);
81
82 33
        return $method;
83
    }
84
85
    /**
86
     * @inheritdocs
87
     */
88 5 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 5
        assert('is_string($name)');
90 5
        assert('$file->type() == "file"');
91 5
        assert('is_int($start_line)');
92 5
        assert('is_int($end_line)');
93
94 5
        $function = $this->create_node
95 5
            ( "function"
96 5
            ,   [ "name" => $name
97 5
                ]
98 5
            );
99 5
        $this->add_definition($function, $file, $start_line, $end_line);
100
101 5
        return $function;
102
    }
103
104
    /**
105
     * @inheritdocs
106
     */
107 13 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 13
        assert('is_string($name)');
109
110 13
        if (array_key_exists($name, $this->globals)) {
111 1
            return $this->globals[$name];
112
        }
113
114 13
        $global = $this->create_node("global", ["name" => $name]);
115 13
        $this->globals[$name] = $global;
116
117 13
        return $global;
118
    }
119
120
    /**
121
     * @inheritdocs
122
     */
123 4 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 4
        assert('is_string($name)');
125
126 4
        if (array_key_exists($name, $this->language_constructs)) {
127 1
            return $this->language_constructs[$name];
128
        }
129
130 4
        $language_construct = $this->create_node("language construct", ["name" => $name]);
131 4
        $this->language_constructs[$name] = $language_construct;
132
133 4
        return $language_construct;
134
    }
135
136
    /**
137
     * @inheritdocs
138
     */
139 6 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 6
        assert('is_string($name)');
141 6
        assert('$file->type() == "file"');
142 6
        assert('is_int($line)');
143
144 6
        $method = $this->create_node("method reference", ["name" => $name]);
145 6
        $this->add_relation($method, "referenced at", ["line" => $line], $file);
146
147 6
        return $method;
148
    }
149
150
    /**
151
     * @inheritdocs
152
     */
153 8 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 8
        assert('is_string($name)');
155 8
        assert('$file->type() == "file"');
156 8
        assert('is_int($line)');
157
158 8
        $function = $this->create_node("function reference", ["name" => $name]);
159 8
        $this->add_relation($function, "referenced at", ["line" => $line], $file);
160
161 8
        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
    /**
186
     * Build a query on the index.
187
     *
188
     * @return  IndexQuery
189
     */
190 54
    public function query() {
191 54
        return new IndexQueryImpl($this);
192
    }
193
194
    // Helper
195
196 47
    protected function add_definition(Node $n, Node $file, $start_line, $end_line) {
197 47
        $this->add_relation
198 47
            ( $n
199 47
            , "defined in"
200 47
            ,   [ "start_line" => $start_line
201 47
                , "end_line" => $end_line
202 47
                ]
203 47
            , $file
204 47
            );
205 47
    }
206
}
207