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

LocationImpl::count_in_entity()   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, 2015 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\Indexer;
12
13
/**
14
 * TODO: This is not the correct name for this thing. It also holds
15
 * information about the file.
16
 */
17
class LocationImpl implements Location {
18
    /**
19
     * @var string
20
     */
21
    protected $file;
22
23
    /**
24
     * @var string
25
     */
26
    protected $file_content;
27
28
    /**
29
     * @var int[]|null
30
     */
31
    protected $running_line_length = null;
32
33
    /**
34
     * This contains the stack of ids were currently in, i.e. the nesting of
35
     * known code blocks we are in.
36
     *
37
     * @var array   contains ($definition_type, $definition_id) tuples
38
     */
39
    protected $definition_stack;
40
41
    /**
42
     * @var \PhpParser\Node|null
43
     */
44
    protected $current_node = null;
45
46 50
    public function __construct($file_name, $file_content) {
47 50
        assert('is_string($file_name)');
48 50
        assert('is_string($file_content)');
49 50
        $this->file_name = $file_name;
0 ignored issues
show
Bug introduced by
The property file_name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50 50
        $this->file_content = $file_content;
51 50
        $this->definition_stack = [];
52 50
    }
53
54
    /**
55
     * @return  mixed
56
     */
57 23
    public function file() {
58 23
        assert('count($this->definition_stack) > 0');
59 23
        assert('$this->definition_stack[0][0] === \\Lechimp\\Dicto\\Variables\\Variable::FILE_TYPE');
60 23
        return $this->definition_stack[0][1];
61
    }
62
63
    /**
64
     * @return  string
65
     */
66 43
    public function file_name() {
67 43
        return $this->file_name;
68
    }
69
70
    /**
71
     * @return  string
72
     */
73 43
    public function file_content() {
74 43
        return $this->file_content;
75
    }
76
77
    /**
78
     * @return  int
79
     */
80 25
    public function line() {
81 25
        assert('$this->current_node !== null');
82 25
        return $this->current_node->getAttribute("startLine");
83
    }
84
85
    /**
86
     * @return  int
87
     */
88 12
    public function column() {
89 12
        assert('$this->current_node !== null');
90 12
        if ($this->running_line_length === null) {
91 12
            $this->init_running_line_length();
92 12
        }
93 12
        $start_pos = $this->current_node->getAttribute("startFilePos");
94 12
        $length_before = $this->running_line_length[$this->line() - 1];
95 12
        return $start_pos - $length_before + 1;
96
    }
97
98
    /**
99
     * @return  array[]     List of ($type, $handle)
100
     */
101 25
    public function in_entities() {
102 25
        return $this->definition_stack;
103
    }
104
105
    /**
106
     * @param   int     $pos
107
     * @return  array       ($type, $handle)
108
     */
109 41
    public function in_entity($pos) {
110 41
        assert('is_int($pos)');
111 41
        assert('$pos >= 0');
112 41
        assert('$pos < count($this->definition_stack)');
113 41
        return $this->definition_stack[$pos];
114
    }
115
116
    /**
117
     * @return   int
118
     */
119 41
    public function count_in_entity() {
120 41
        return count($this->definition_stack);
121
    }
122
123
    /**
124
     * Push an entity on the stack.
125
     *
126
     * @param   string  $type
127
     * @param   mixed   $handle
128
     * @return  null
129
     */
130 43
    public function push_entity($type, $handle) {
131 43
        assert('\\Lechimp\\Dicto\\Variables\\Variable::is_type($type)');
132 43
        $this->definition_stack[] = [$type, $handle];
133 43
    }
134
135
    /**
136
     * Pop an entity from the stach
137
     *
138
     * @return null
139
     */
140 41
    public function pop_entity() {
141 41
        array_pop($this->definition_stack);
142 41
    }
143
144
    /**
145
     * @param   \PhpParser\Node $node
146
     * @return  null
147
     */
148 45
    public function set_current_node(\PhpParser\Node $node) {
149 45
        assert('$this->current_node === null');
150 45
        $this->current_node = $node;
151 45
    }
152
153
    /**
154
     * @return  \PhpParser\Node $node
155
     */
156 43
    public function current_node() {
157 43
        assert('$this->current_node !== null');
158 43
        return $this->current_node;
159
    }
160
161
    /**
162
     * @return  null
163
     */
164 42
    public function flush_current_node() {
165 42
        assert('$this->current_node !== null');
166 42
        $this->current_node = null;
167 42
    }
168
169 13
    protected function init_running_line_length() {
170 13
        $pos = 0;
171 13
        $count = 0;
172 13
        while (true) {
173 13
            $this->running_line_length[] = $count;
174 13
            $count = strpos($this->file_content, "\n", $pos);
175 13
            if (!$count) {
176 13
                break;
177
            }
178 13
            $count++; // for actual linebreak
179 13
            $pos = $count;
180 13
        }
181 13
    }
182
}
183
184