1
|
|
|
<?php |
2
|
|
|
namespace gossi\codegen\parser\visitor; |
3
|
|
|
|
4
|
|
|
use gossi\codegen\model\AbstractPhpMember; |
5
|
|
|
use gossi\codegen\model\AbstractPhpStruct; |
6
|
|
|
use gossi\codegen\model\PhpConstant; |
7
|
|
|
use gossi\codegen\model\PhpMethod; |
8
|
|
|
use gossi\codegen\model\PhpParameter; |
9
|
|
|
use gossi\codegen\model\PhpProperty; |
10
|
|
|
use PhpParser\Node; |
11
|
|
|
use PhpParser\Node\Const_; |
12
|
|
|
use PhpParser\Node\Expr\ConstFetch; |
13
|
|
|
use PhpParser\Node\Name; |
14
|
|
|
use PhpParser\Node\Param; |
15
|
|
|
use PhpParser\Node\Scalar\String_; |
16
|
|
|
use PhpParser\Node\Stmt\ClassLike; |
17
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
18
|
|
|
use PhpParser\Node\Stmt\Namespace_; |
19
|
|
|
use PhpParser\Node\Stmt\Property; |
20
|
|
|
use PhpParser\Node\Stmt\TraitUse; |
21
|
|
|
use PhpParser\Node\Stmt\UseUse; |
22
|
|
|
use PhpParser\NodeTraverser; |
23
|
|
|
use PhpParser\NodeVisitorAbstract; |
24
|
|
|
use PhpParser\PrettyPrinter\Standard; |
25
|
|
|
use PhpParser\Node\Stmt\Class_; |
26
|
|
|
use PhpParser\Node\Stmt\Interface_; |
27
|
|
|
use PhpParser\Node\Stmt\Trait_; |
28
|
|
|
use PhpParser\Node\Stmt\ClassConst; |
29
|
|
|
use PhpParser\Comment\Doc; |
30
|
|
|
use gossi\docblock\tags\ParamTag; |
31
|
|
|
|
32
|
|
|
abstract class AbstractPhpStructVisitor extends NodeVisitorAbstract { |
33
|
|
|
|
34
|
|
|
private $constMap = [ |
35
|
|
|
'false' => false, |
36
|
|
|
'true' => true |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
protected $struct; |
40
|
|
|
|
41
|
7 |
|
public function __construct(AbstractPhpStruct $struct) { |
42
|
7 |
|
$this->struct = $struct; |
43
|
7 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return AbstractPhpStruct |
47
|
|
|
*/ |
48
|
7 |
|
public function getStruct() { |
49
|
7 |
|
return $this->struct; |
50
|
|
|
} |
51
|
|
|
|
52
|
7 |
|
public function enterNode(Node $node) { |
53
|
7 |
|
switch ($node->getType()) { |
54
|
7 |
|
case 'Stmt_Namespace': |
55
|
7 |
|
$this->visitNamespace($node); |
|
|
|
|
56
|
7 |
|
break; |
57
|
|
|
|
58
|
7 |
|
case 'Stmt_UseUse': |
59
|
2 |
|
$this->visitUseStatement($node); |
|
|
|
|
60
|
2 |
|
break; |
61
|
|
|
|
62
|
7 |
|
case 'Stmt_Class': |
63
|
5 |
|
$this->visitStruct($node); |
|
|
|
|
64
|
5 |
|
$this->visitClass($node); |
|
|
|
|
65
|
5 |
|
break; |
66
|
|
|
|
67
|
7 |
|
case 'Stmt_Interface': |
68
|
1 |
|
$this->visitStruct($node); |
|
|
|
|
69
|
1 |
|
$this->visitInterface($node); |
|
|
|
|
70
|
1 |
|
break; |
71
|
|
|
|
72
|
7 |
|
case 'Stmt_Trait': |
73
|
1 |
|
$this->visitStruct($node); |
|
|
|
|
74
|
1 |
|
$this->visitTrait($node); |
|
|
|
|
75
|
1 |
|
break; |
76
|
|
|
|
77
|
7 |
|
case 'Stmt_TraitUse': |
78
|
2 |
|
$this->visitTraitUse($node); |
|
|
|
|
79
|
2 |
|
break; |
80
|
|
|
|
81
|
7 |
|
case 'Stmt_ClassConst': |
82
|
2 |
|
$this->visitConstants($node); |
|
|
|
|
83
|
2 |
|
break; |
84
|
|
|
|
85
|
7 |
|
case 'Stmt_Property': |
86
|
3 |
|
$this->visitProperty($node); |
|
|
|
|
87
|
3 |
|
break; |
88
|
|
|
|
89
|
7 |
|
case 'Stmt_ClassMethod': |
90
|
5 |
|
$this->visitMethod($node); |
|
|
|
|
91
|
5 |
|
break; |
92
|
7 |
|
} |
93
|
|
|
// echo $node->getType() . "\n"; |
|
|
|
|
94
|
|
|
|
95
|
|
|
// return NodeTraverser::DONT_TRAVERSE_CHILDREN; |
|
|
|
|
96
|
7 |
|
} |
97
|
|
|
|
98
|
7 |
|
protected function visitStruct(ClassLike $node) { |
99
|
7 |
|
$this->struct->setName($node->name); |
100
|
|
|
|
101
|
7 |
|
if (($doc = $node->getDocComment()) !== null) { |
102
|
4 |
|
$this->struct->setDocblock($doc->getReformattedText()); |
103
|
4 |
|
$docblock = $this->struct->getDocblock(); |
104
|
4 |
|
$this->struct->setDescription($docblock->getShortDescription()); |
105
|
4 |
|
$this->struct->setLongDescription($docblock->getLongDescription()); |
106
|
4 |
|
} |
107
|
7 |
|
} |
108
|
|
|
|
109
|
|
|
protected function visitClass(Class_ $node) {} |
110
|
|
|
|
111
|
|
|
protected function visitInterface(Interface_ $node) {} |
112
|
|
|
|
113
|
|
|
protected function visitTrait(Trait_ $node) {} |
|
|
|
|
114
|
|
|
|
115
|
2 |
|
protected function visitTraitUse(TraitUse $node) { |
116
|
2 |
|
foreach ($node->traits as $trait) { |
117
|
2 |
|
$this->struct->addTrait(implode('\\', $trait->parts)); |
|
|
|
|
118
|
2 |
|
} |
119
|
2 |
|
} |
120
|
|
|
|
121
|
2 |
|
protected function visitConstants(ClassConst $node) { |
122
|
2 |
|
$doc = $node->getDocComment(); |
123
|
2 |
|
foreach ($node->consts as $const) { |
124
|
2 |
|
$this->visitConstant($const, $doc); |
125
|
2 |
|
} |
126
|
2 |
|
} |
127
|
|
|
|
128
|
2 |
|
protected function visitConstant(Const_ $node, Doc $doc = null) { |
129
|
2 |
|
$const = new PhpConstant($node->name, $this->getValue($node)); |
130
|
2 |
|
$const->setValue($this->getValue($node->value)); |
131
|
|
|
|
132
|
2 |
|
$this->parseMemberDocblock($const, $doc); |
133
|
|
|
|
134
|
2 |
|
$this->struct->setConstant($const); |
|
|
|
|
135
|
2 |
|
} |
136
|
|
|
|
137
|
3 |
|
protected function visitProperty(Property $node) { |
138
|
3 |
|
$this->struct->setProperty($this->getProperty($node)); |
|
|
|
|
139
|
3 |
|
} |
140
|
|
|
|
141
|
7 |
|
protected function visitNamespace(Namespace_ $node) { |
142
|
7 |
|
if ($node->name !== null) { |
143
|
7 |
|
$this->struct->setNamespace(implode('\\', $node->name->parts)); |
144
|
7 |
|
} |
145
|
7 |
|
} |
146
|
|
|
|
147
|
2 |
|
protected function visitUseStatement(UseUse $node) { |
148
|
2 |
|
$name = implode('\\', $node->name->parts); |
|
|
|
|
149
|
2 |
|
$alias = !empty($node->alias) ? $node->alias : null; |
150
|
|
|
|
151
|
2 |
|
$this->struct->addUseStatement($name, $alias); |
152
|
2 |
|
} |
153
|
|
|
|
154
|
5 |
|
protected function visitMethod(ClassMethod $node) { |
155
|
5 |
|
$m = new PhpMethod($node->name); |
156
|
5 |
|
$m->setAbstract($node->isAbstract()); |
157
|
5 |
|
$m->setFinal($node->isFinal()); |
158
|
5 |
|
$m->setVisibility($this->getVisibility($node)); |
159
|
5 |
|
$m->setStatic($node->isStatic()); |
160
|
5 |
|
$m->setReferenceReturned($node->returnsByRef()); |
161
|
|
|
|
162
|
|
|
// docblock |
163
|
5 |
View Code Duplication |
if (($doc = $node->getDocComment()) !== null) { |
|
|
|
|
164
|
3 |
|
$m->setDocblock($doc->getReformattedText()); |
165
|
3 |
|
$docblock = $m->getDocblock(); |
166
|
3 |
|
$m->setDescription($docblock->getShortDescription()); |
167
|
3 |
|
$m->setLongDescription($docblock->getLongDescription()); |
168
|
3 |
|
} |
169
|
|
|
|
170
|
|
|
// params |
171
|
5 |
|
$params = $m->getDocblock()->getTags('param'); |
172
|
5 |
|
foreach ($node->params as $param) { |
173
|
|
|
/* @var $param Param */ |
174
|
|
|
|
175
|
2 |
|
$p = new PhpParameter(); |
176
|
2 |
|
$p->setName($param->name); |
177
|
2 |
|
$p->setPassedByReference($param->byRef); |
178
|
|
|
|
179
|
2 |
|
if (is_string($param->type)) { |
180
|
1 |
|
$p->setType($param->type); |
181
|
2 |
|
} else if ($param->type instanceof Name) { |
182
|
1 |
|
$p->setType(implode('\\', $param->type->parts)); |
183
|
1 |
|
} |
184
|
|
|
|
185
|
2 |
|
$default = $param->default; |
186
|
2 |
|
if ($default !== null) { |
187
|
2 |
|
$p->setDefaultValue($this->getValue($default)); |
188
|
2 |
|
} |
189
|
|
|
|
190
|
2 |
|
$tag = $params->find($p, function (ParamTag $t, $p) { |
191
|
2 |
|
return $t->getVariable() == '$' . $p->getName(); |
192
|
2 |
|
}); |
193
|
|
|
|
194
|
2 |
|
if ($tag !== null) { |
195
|
2 |
|
$p->setType($tag->getType(), $tag->getDescription()); |
196
|
2 |
|
} |
197
|
|
|
|
198
|
2 |
|
$m->addParameter($p); |
199
|
5 |
|
} |
200
|
|
|
|
201
|
|
|
// return type and description |
202
|
5 |
|
$returns = $m->getDocblock()->getTags('return'); |
203
|
5 |
View Code Duplication |
if ($returns->size() > 0) { |
|
|
|
|
204
|
1 |
|
$return = $returns->get(0); |
205
|
1 |
|
$m->setType($return->getType(), $return->getDescription()); |
206
|
1 |
|
} |
207
|
|
|
|
208
|
|
|
// body |
209
|
5 |
|
$stmts = $node->getStmts(); |
210
|
5 |
|
if (is_array($stmts) && count($stmts)) { |
211
|
1 |
|
$prettyPrinter = new Standard(); |
212
|
1 |
|
$m->setBody($prettyPrinter->prettyPrint($stmts)); |
213
|
1 |
|
} |
214
|
|
|
|
215
|
5 |
|
$this->struct->setMethod($m); |
216
|
5 |
|
} |
217
|
|
|
|
218
|
3 |
|
protected function getProperty(Property $node) { |
219
|
3 |
|
$prop = $node->props[0]; |
220
|
|
|
|
221
|
3 |
|
$p = new PhpProperty($prop->name); |
222
|
|
|
|
223
|
3 |
|
$default = $prop->default; |
224
|
3 |
|
if ($default !== null) { |
225
|
2 |
|
$p->setDefaultValue($this->getValue($default)); |
226
|
2 |
|
} |
227
|
|
|
|
228
|
3 |
|
$p->setStatic($node->isStatic()); |
229
|
3 |
|
$p->setVisibility($this->getVisibility($node)); |
230
|
|
|
|
231
|
3 |
|
$this->parseMemberDocblock($p, $node->getDocComment()); |
232
|
|
|
|
233
|
3 |
|
return $p; |
234
|
|
|
} |
235
|
|
|
|
236
|
4 |
|
private function parseMemberDocblock($member, Doc $doc = null) { |
237
|
4 |
|
if ($doc !== null) { |
238
|
2 |
|
$member->setDocblock($doc->getReformattedText()); |
239
|
2 |
|
$docblock = $member->getDocblock(); |
240
|
2 |
|
$member->setDescription($docblock->getShortDescription()); |
241
|
2 |
|
$member->setLongDescription($docblock->getLongDescription()); |
242
|
|
|
|
243
|
2 |
|
$vars = $docblock->getTags('var'); |
244
|
2 |
|
if ($vars->size() > 0) { |
245
|
2 |
|
$var = $vars->get(0); |
246
|
2 |
|
$member->setType($var->getType(), $var->getDescription()); |
247
|
2 |
|
} |
248
|
2 |
|
} |
249
|
4 |
|
} |
250
|
|
|
|
251
|
3 |
|
private function getValue(Node $node) { |
|
|
|
|
252
|
3 |
|
if ($node instanceof ConstFetch) { |
253
|
1 |
|
$const = $node->name->parts[0]; |
254
|
1 |
|
if (isset($this->constMap[$const])) { |
255
|
1 |
|
return $this->constMap[$const]; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $const; |
259
|
|
|
} |
260
|
|
|
|
261
|
3 |
|
if ($node instanceof String_) { |
262
|
3 |
|
return $node->value; |
263
|
|
|
} |
264
|
2 |
|
} |
265
|
|
|
|
266
|
5 |
|
private function getVisibility(Node $node) { |
|
|
|
|
267
|
5 |
|
if ($node->isPrivate()) { |
|
|
|
|
268
|
3 |
|
return AbstractPhpMember::VISIBILITY_PRIVATE; |
269
|
|
|
} |
270
|
|
|
|
271
|
5 |
|
if ($node->isProtected()) { |
|
|
|
|
272
|
1 |
|
return AbstractPhpMember::VISIBILITY_PROTECTED; |
273
|
|
|
} |
274
|
|
|
|
275
|
5 |
|
return AbstractPhpMember::VISIBILITY_PUBLIC; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.