1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace gossi\codegen\parser\visitor; |
5
|
|
|
|
6
|
|
|
use gossi\codegen\model\PhpMethod; |
7
|
|
|
use gossi\codegen\model\PhpParameter; |
8
|
|
|
use gossi\codegen\parser\PrettyPrinter; |
9
|
|
|
use gossi\codegen\parser\visitor\parts\MemberParserPart; |
10
|
|
|
use gossi\codegen\parser\visitor\parts\ValueParserPart; |
11
|
|
|
use gossi\docblock\tags\ParamTag; |
12
|
|
|
use PhpParser\Node\Name; |
13
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
14
|
|
|
|
15
|
|
|
class MethodParserVisitor extends StructParserVisitor { |
16
|
|
|
|
17
|
|
|
use MemberParserPart; |
18
|
8 |
|
use ValueParserPart; |
19
|
8 |
|
|
20
|
8 |
|
public function visitMethod(ClassMethod $node) { |
21
|
8 |
|
$m = new PhpMethod($node->name->name); |
22
|
8 |
|
$m->setAbstract($node->isAbstract()); |
23
|
8 |
|
$m->setFinal($node->isFinal()); |
24
|
8 |
|
$m->setVisibility($this->getVisibility($node)); |
25
|
|
|
$m->setStatic($node->isStatic()); |
26
|
|
|
$m->setReferenceReturned($node->returnsByRef()); |
27
|
8 |
|
|
28
|
3 |
|
$this->parseMemberDocblock($m, $node->getDocComment()); |
29
|
3 |
|
$this->parseParams($m, $node); |
|
|
|
|
30
|
3 |
|
$this->parseType($m, $node); |
|
|
|
|
31
|
3 |
|
$this->parseBody($m, $node); |
32
|
|
|
|
33
|
|
|
$this->struct->setMethod($m); |
34
|
|
|
} |
35
|
8 |
|
|
36
|
8 |
|
private function parseParams(PhpMethod $m, ClassMethod $node) { |
37
|
|
|
$params = $m->getDocblock()->getTags('param'); |
38
|
|
|
foreach ($node->params as $param) { |
39
|
5 |
|
$name = $param->var ? $param->var->name : $param->name; |
40
|
5 |
|
|
41
|
5 |
|
$p = new PhpParameter(); |
42
|
|
|
$p->setName($name); |
43
|
5 |
|
$p->setPassedByReference($param->byRef); |
44
|
1 |
|
|
45
|
5 |
|
if (is_string($param->type)) { |
46
|
2 |
|
$p->setType($param->type); |
47
|
|
|
} else if ($param->type instanceof Name) { |
48
|
|
|
$p->setType(implode('\\', $param->type->parts)); |
49
|
5 |
|
} |
50
|
|
|
|
51
|
5 |
|
$this->parseValue($p, $param); |
52
|
2 |
|
|
53
|
5 |
|
$tag = $params->find($p, function (ParamTag $t, $p) { |
54
|
|
|
return $t->getVariable() == '$' . $p->getName(); |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
if ($tag !== null) { |
58
|
|
|
$p->setType($tag->getType(), $tag->getDescription()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$m->addParameter($p); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function parseType(PhpMethod &$m, ClassMethod $node) { |
|
|
|
|
66
|
|
|
$returns = $m->getDocblock()->getTags('return'); |
67
|
|
|
if ($returns->size() > 0) { |
68
|
|
|
$return = $returns->get(0); |
69
|
|
|
$m->setType($return->getType(), $return->getDescription()); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function parseBody(PhpMethod &$m, ClassMethod $node) { |
74
|
|
|
$stmts = $node->getStmts(); |
75
|
|
|
if (is_array($stmts) && count($stmts)) { |
76
|
|
|
$prettyPrinter = new PrettyPrinter(); |
77
|
|
|
$m->setBody($prettyPrinter->prettyPrint($stmts)); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: