Completed
Push — master ( 9b2a73...93cace )
by Richard
05:06
created

IndexDB   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 234
Duplicated Lines 47.44 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 98.51%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 17
lcom 1
cbo 1
dl 111
loc 234
ccs 132
cts 134
cp 0.9851
rs 10
c 3
b 0
f 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
A _file() 0 14 1
A _class() 14 14 1
A _interface() 14 14 1
A _method() 0 18 1
A _function() 15 15 1
A _global() 12 12 2
A _language_construct() 12 12 2
A _method_reference() 22 22 2
A _function_reference() 22 22 2
A _relation() 0 17 3
A add_definition() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
     * @var array<string,Node>
32
     */
33
    protected $method_references = [];
34
35
    /**
36
     * @var array<string,Node>
37
     */
38
    protected $function_references = [];
39
40
    /**
41
     * @inheritdocs
42
     */
43 50
    public function _file($path, $source) {
44 50
        assert('is_string($path)');
45 50
        assert('is_string($source)');
46
47 50
        $parts = explode("/", $path);
48 50
        $name = array_pop($parts);
49 50
        return $this->create_node
50 50
            ( "file"
51 50
            ,   [ "path" => $path
52 50
                , "name" => $name
53 50
                , "source" => explode("\n", $source)
54 50
                ]
55 50
            );
56
    }
57
58
    /**
59
     * @inheritdocs
60
     */
61 42 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...
62 42
        assert('is_string($name)');
63 42
        assert('$file->type() == "file"');
64 42
        assert('is_int($start_line)');
65 42
        assert('is_int($end_line)');
66
67 42
        $class = $this->create_node
68 42
            ( "class"
69 42
            ,   [ "name" => $name
70 42
                ]
71 42
            );
72 42
        $this->add_definition($class, $file, $start_line, $end_line);
73 42
        return $class;
74
    }
75
76
    /**
77
     * @inheritdocs
78
     */
79 2 View Code Duplication
    public function _interface($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...
80 2
        assert('is_string($name)');
81 2
        assert('$file->type() == "file"');
82 2
        assert('is_int($start_line)');
83 2
        assert('is_int($end_line)');
84
85 2
        $interface = $this->create_node
86 2
            ( "interface"
87 2
            ,   [ "name" => $name
88 2
                ]
89 2
            );
90 2
        $this->add_definition($interface, $file, $start_line, $end_line);
91 2
        return $interface;
92
    }
93
94
    /**
95
     * @inheritdocs
96
     */
97 35
    public function _method($name, $class, $file, $start_line, $end_line) {
98 35
        assert('is_string($name)');
99 35
        assert('in_array($class->type(), ["class", "interface"])');
100 35
        assert('$file->type() == "file"');
101 35
        assert('is_int($start_line)');
102 35
        assert('is_int($end_line)');
103
104 35
        $method = $this->create_node
105 35
            ( "method"
106 35
            ,   [ "name" => $name
107 35
                ]
108 35
            );
109 35
        $this->add_definition($method, $file, $start_line, $end_line);
110 35
        $this->add_relation($method, "contained in", [], $class);
111 35
        $this->add_relation($class, "contains", [], $method);
112
113 35
        return $method;
114
    }
115
116
    /**
117
     * @inheritdocs
118
     */
119 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...
120 5
        assert('is_string($name)');
121 5
        assert('$file->type() == "file"');
122 5
        assert('is_int($start_line)');
123 5
        assert('is_int($end_line)');
124
125 5
        $function = $this->create_node
126 5
            ( "function"
127 5
            ,   [ "name" => $name
128 5
                ]
129 5
            );
130 5
        $this->add_definition($function, $file, $start_line, $end_line);
131
132 5
        return $function;
133
    }
134
135
    /**
136
     * @inheritdocs
137
     */
138 11 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...
139 11
        assert('is_string($name)');
140
141 11
        if (array_key_exists($name, $this->globals)) {
142 1
            return $this->globals[$name];
143
        }
144
145 11
        $global = $this->create_node("global", ["name" => $name]);
146 11
        $this->globals[$name] = $global;
147
148 11
        return $global;
149
    }
150
151
    /**
152
     * @inheritdocs
153
     */
154 7 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...
155 7
        assert('is_string($name)');
156
157 7
        if (array_key_exists($name, $this->language_constructs)) {
158 1
            return $this->language_constructs[$name];
159
        }
160
161 7
        $language_construct = $this->create_node("language construct", ["name" => $name]);
162 7
        $this->language_constructs[$name] = $language_construct;
163
164 7
        return $language_construct;
165
    }
166
167
    /**
168
     * @inheritdocs
169
     */
170 5 View Code Duplication
    public function _method_reference($name, $file, $line, $column) {
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...
171 5
        assert('is_string($name)');
172 5
        assert('$file->type() == "file"');
173 5
        assert('is_int($line)');
174
175 5
        $key = $name."_".$file->property("path")."_".$line."_".$column;
176
177 5
        if (array_key_exists($key, $this->method_references)) {
178
            return $this->method_references[$key];
179
        }
180
181 5
        $method = $this->create_node("method reference", ["name" => $name]);
182 5
        $this->add_relation
183 5
            ( $method
184 5
            , "referenced at"
185 5
            , ["line" => $line, "column" => $column]
186 5
            , $file
187 5
            );
188
189 5
        $this->method_references[$key] = $method;
190 5
        return $method;
191
    }
192
193
    /**
194
     * @inheritdocs
195
     */
196 9 View Code Duplication
    public function _function_reference($name, $file, $line, $column) {
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...
197 9
        assert('is_string($name)');
198 9
        assert('$file->type() == "file"');
199 9
        assert('is_int($line)');
200
201 9
        $key = $name."_".$file->property("path")."_".$line."_".$column;
202
        
203 9
        if (array_key_exists($key, $this->function_references)) {
204
            return $this->function_references[$key];
205
        }
206
207 9
        $function = $this->create_node("function reference", ["name" => $name]);
208 9
        $this->add_relation
209 9
            ( $function
210 9
            , "referenced at"
211 9
            , ["line" => $line, "column" => $column]
212 9
            , $file
213 9
            );
214
215 9
        $this->function_references[$key] = $function;
216 9
        return $function;
217
    }
218
219
    /**
220
     * @inheritdocs
221
     */
222 16
    public function _relation($left_entity, $relation, $right_entity, $file, $line) {
223 16
        assert('$left_entity instanceof \\Lechimp\\Dicto\\Graph\\Node');
224 16
        assert('$right_entity instanceof \\Lechimp\\Dicto\\Graph\\Node');
225 16
        assert('is_string($relation)');
226
227 16
        $props = [];
228 16
        if ($file !== null) {
229 16
            assert('$file->type() == "file"');
230 16
            $props["file"] = $file;
231 16
        }
232 16
        if ($line !== null) {
233 16
            assert('is_int($line)');
234 16
            $props["line"] = $line;
235 16
        }
236
237 16
        $this->add_relation($left_entity, $relation, $props, $right_entity);
238 16
    }
239
240
    // Helper
241
242 46
    protected function add_definition(Node $n, Node $file, $start_line, $end_line) {
243 46
        $this->add_relation
244 46
            ( $n
245 46
            , "defined in"
246 46
            ,   [ "start_line" => $start_line
247 46
                , "end_line" => $end_line
248 46
                ]
249 46
            , $file
250 46
            );
251 46
    }
252
}
253