|
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
|
|
|
use Lechimp\Dicto\Variables\Variable; |
|
14
|
|
|
use PhpParser\Node as N; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Visitor for the AST used by Indexer. |
|
18
|
|
|
*/ |
|
19
|
|
|
class BaseVisitor implements \PhpParser\NodeVisitor { |
|
20
|
|
|
/** |
|
21
|
|
|
* @var LocationImpl |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $location; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Insert |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $insert; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* This is used to exit early in enterNode and to break enterNode apart |
|
32
|
|
|
* into many methods. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array<string,string> |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $jump_labels; |
|
37
|
|
|
|
|
38
|
53 |
|
public function __construct(LocationImpl $location, Insert $insert) { |
|
39
|
53 |
|
$this->location = $location; |
|
40
|
53 |
|
$this->insert = $insert; |
|
41
|
53 |
|
$this->jump_labels = |
|
42
|
|
|
[ N\Stmt\Namespace_::class => "enterNamespace" |
|
|
|
|
|
|
43
|
53 |
|
, N\Stmt\Class_::class => "enterClass" |
|
44
|
53 |
|
, N\Stmt\Interface_::class => "enterInterface" |
|
45
|
53 |
|
, N\Stmt\Trait_::class => "enterTrait" |
|
46
|
53 |
|
, N\Stmt\ClassMethod::class => "enterMethod" |
|
47
|
53 |
|
, N\Stmt\Function_::class => "enterFunction" |
|
48
|
53 |
|
]; |
|
49
|
53 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
// from \PhpParser\NodeVisitor |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
53 |
|
public function beforeTraverse(array $nodes) { |
|
57
|
53 |
|
$handle = $this->insert->_file($this->location->_file_name(), $this->location->_file_content()); |
|
58
|
53 |
|
$this->location->push_entity(Variable::FILE_TYPE, $handle); |
|
59
|
53 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritdoc |
|
63
|
|
|
*/ |
|
64
|
53 |
|
public function afterTraverse(array $nodes) { |
|
65
|
53 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritdoc |
|
69
|
|
|
*/ |
|
70
|
53 |
|
public function enterNode(\PhpParser\Node $node) { |
|
71
|
53 |
|
$cls = get_class($node); |
|
72
|
53 |
|
if (isset($this->jump_labels[$cls])) { |
|
73
|
51 |
|
$this->location->set_current_node($node); |
|
74
|
51 |
|
$start_line = $node->getAttribute("startLine"); |
|
75
|
51 |
|
$end_line = $node->getAttribute("endLine"); |
|
76
|
51 |
|
list($type, $handle) = $this->{$this->jump_labels[$cls]} |
|
77
|
51 |
|
($node, $start_line, $end_line); |
|
78
|
51 |
|
$this->location->push_entity($type, $handle); |
|
79
|
51 |
|
$this->location->flush_current_node(); |
|
80
|
51 |
|
} |
|
81
|
53 |
|
} |
|
82
|
|
|
|
|
83
|
7 |
|
public function enterNamespace(N\Stmt\Namespace_ $node, $start_line, $end_line) { |
|
|
|
|
|
|
84
|
7 |
|
$handle = $this->insert->_namespace |
|
85
|
7 |
|
( "".$node->name // force string representation |
|
86
|
7 |
|
); |
|
87
|
7 |
|
return [Variable::NAMESPACE_TYPE, $handle]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
44 |
View Code Duplication |
public function enterClass(N\Stmt\Class_ $node, $start_line, $end_line) { |
|
|
|
|
|
|
91
|
44 |
|
$handle = $this->insert->_class |
|
92
|
44 |
|
( $node->name |
|
93
|
44 |
|
, $this->location->_file() |
|
94
|
44 |
|
, $start_line |
|
95
|
44 |
|
, $end_line |
|
96
|
44 |
|
, $this->location->_namespace() |
|
97
|
44 |
|
); |
|
98
|
44 |
|
return [Variable::CLASS_TYPE, $handle]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
3 |
View Code Duplication |
public function enterInterface(N\Stmt\Interface_ $node, $start_line, $end_line) { |
|
|
|
|
|
|
102
|
3 |
|
$handle = $this->insert->_interface |
|
103
|
3 |
|
( $node->name |
|
104
|
3 |
|
, $this->location->_file() |
|
105
|
3 |
|
, $start_line |
|
106
|
3 |
|
, $end_line |
|
107
|
3 |
|
, $this->location->_namespace() |
|
108
|
3 |
|
); |
|
109
|
3 |
|
return [Variable::INTERFACE_TYPE, $handle]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
3 |
View Code Duplication |
public function enterTrait(N\Stmt\Trait_ $node, $start_line, $end_line) { |
|
|
|
|
|
|
113
|
3 |
|
$handle = $this->insert->_trait |
|
114
|
3 |
|
( $node->name |
|
115
|
3 |
|
, $this->location->_file() |
|
116
|
3 |
|
, $start_line |
|
117
|
3 |
|
, $end_line |
|
118
|
3 |
|
, $this->location->_namespace() |
|
119
|
3 |
|
); |
|
120
|
3 |
|
return [Variable::INTERFACE_TYPE, $handle]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
38 |
|
public function enterMethod(N\Stmt\ClassMethod $node, $start_line, $end_line) { |
|
124
|
38 |
|
$handle = $this->insert->_method |
|
125
|
38 |
|
( $node->name |
|
126
|
38 |
|
, $this->location->_class_interface_trait() |
|
127
|
38 |
|
, $this->location->_file() |
|
128
|
38 |
|
, $start_line |
|
129
|
38 |
|
, $end_line |
|
130
|
38 |
|
); |
|
131
|
38 |
|
return [Variable::METHOD_TYPE, $handle]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
4 |
|
public function enterFunction(N\Stmt\Function_ $node, $start_line, $end_line) { |
|
135
|
4 |
|
$handle = $this->insert->_function |
|
136
|
4 |
|
( $node->name |
|
137
|
4 |
|
, $this->location->_file() |
|
138
|
4 |
|
, $start_line |
|
139
|
4 |
|
, $end_line |
|
140
|
4 |
|
); |
|
141
|
4 |
|
return [Variable::FUNCTION_TYPE, $handle]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @inheritdoc |
|
146
|
|
|
*/ |
|
147
|
53 |
|
public function leaveNode(\PhpParser\Node $node) { |
|
148
|
|
|
if ( $node instanceof N\Stmt\Namespace_ |
|
149
|
53 |
|
|| $node instanceof N\Stmt\Class_ |
|
150
|
53 |
|
|| $node instanceof N\Stmt\Interface_ |
|
151
|
49 |
|
|| $node instanceof N\Stmt\ClassMethod |
|
152
|
53 |
|
|| $node instanceof N\Stmt\Function_) { |
|
153
|
|
|
|
|
154
|
50 |
|
$this->location->pop_entity(); |
|
155
|
50 |
|
} |
|
156
|
53 |
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..