1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padawan\Parser; |
4
|
|
|
|
5
|
|
|
use phpDocumentor\Reflection\DocBlock; |
6
|
|
|
use phpDocumentor\Reflection\DocBlock\Tag; |
7
|
|
|
use phpDocumentor\Reflection\DocBlock\Context; |
8
|
|
|
use Padawan\Domain\Project\Node\Comment; |
9
|
|
|
use Padawan\Domain\Project\Node\MethodParam; |
10
|
|
|
use Padawan\Domain\Project\Node\Variable; |
11
|
|
|
use Padawan\Domain\Project\Node\ClassProperty; |
12
|
|
|
|
13
|
|
|
class CommentParser |
14
|
|
|
{ |
15
|
|
|
public function __construct(UseParser $useParser) { |
16
|
|
|
$this->useParser = $useParser; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Parses DocComment block |
21
|
|
|
* |
22
|
|
|
* @param string $doc |
23
|
|
|
* @return Comment |
24
|
|
|
*/ |
25
|
|
|
public function parse($doc) { |
26
|
|
|
$text = $doc; |
27
|
|
|
if (is_array($doc)) { |
28
|
|
|
$doc = array_shift($doc); |
29
|
|
|
$text = $doc->getText(); |
30
|
|
|
} |
31
|
|
|
$comment = new Comment( |
32
|
|
|
$this->trimComment($text) |
33
|
|
|
); |
34
|
|
|
$this->parseDoc($comment, $text); |
35
|
|
|
|
36
|
|
|
return $comment; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Parses doc comment and populates comment entity |
41
|
|
|
* |
42
|
|
|
* @param string $text |
43
|
|
|
*/ |
44
|
|
|
protected function parseDoc(Comment $comment, $text) { |
45
|
|
|
$context = $this->getContext(); |
46
|
|
|
try { |
47
|
|
|
$block = new DocBlock($text, $context); |
48
|
|
|
foreach ($block->getTags() AS $tag) { |
49
|
|
|
switch ($tag->getName()) { |
50
|
|
|
case "param": |
51
|
|
|
$comment->addVar( |
52
|
|
|
$this->createMethodParam($tag) |
53
|
|
|
); |
54
|
|
|
break; |
55
|
|
|
case "var": |
56
|
|
|
$comment->addVar( |
57
|
|
|
$this->createVar($tag) |
58
|
|
|
); |
59
|
|
|
break; |
60
|
|
|
case "return": |
61
|
|
|
$comment->setReturn( |
62
|
|
|
$this->getFQCN($tag->getType()) |
|
|
|
|
63
|
|
|
); |
64
|
|
|
break; |
65
|
|
|
case "property": |
66
|
|
|
case "property-read": |
67
|
|
|
case "property-write": |
68
|
|
|
$comment->addProperty( |
69
|
|
|
$this->createProperty($tag) |
70
|
|
|
); |
71
|
|
|
break; |
72
|
|
|
case "inheritdoc": |
73
|
|
|
$comment->markInheritDoc(); |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
catch (\Exception $e) { |
|
|
|
|
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
View Code Duplication |
protected function createMethodParam(Tag $tag) { |
|
|
|
|
83
|
|
|
$name = trim($tag->getVariableName(), '$'); |
|
|
|
|
84
|
|
|
$param = new MethodParam($name); |
85
|
|
|
$param->setType($this->getFQCN($tag->getType())); |
|
|
|
|
86
|
|
|
return $param; |
87
|
|
|
} |
88
|
|
View Code Duplication |
protected function createVar(Tag $tag) { |
|
|
|
|
89
|
|
|
$name = trim($tag->getVariableName(), '$'); |
|
|
|
|
90
|
|
|
$param = new Variable($name); |
91
|
|
|
$param->setType($this->getFQCN($tag->getType())); |
|
|
|
|
92
|
|
|
return $param; |
93
|
|
|
} |
94
|
|
View Code Duplication |
protected function createProperty(Tag $tag) { |
|
|
|
|
95
|
|
|
$name = trim($tag->getVariableName(), '$'); |
|
|
|
|
96
|
|
|
$prop = new ClassProperty; |
97
|
|
|
$prop->name = $name; |
98
|
|
|
$prop->setType($this->getFQCN($tag->getType())); |
|
|
|
|
99
|
|
|
return $prop; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Creates FQN by type string |
104
|
|
|
* |
105
|
|
|
* @param string $type |
106
|
|
|
* @return \Padawan\Domain\Project\FQCN |
107
|
|
|
*/ |
108
|
|
|
protected function getFQCN($type) { |
109
|
|
|
return $this->useParser->parseType($type); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function trimComment($comment) { |
116
|
|
|
$lines = explode("\n", $comment); |
117
|
|
|
foreach ($lines AS $key => $line) { |
118
|
|
|
$lines[$key] = preg_replace([ |
119
|
|
|
"/^\/\**/", |
120
|
|
|
"/^ *\* */", |
121
|
|
|
"/\**\/$/" |
122
|
|
|
], "", $line); |
123
|
|
|
} |
124
|
|
|
$text = implode("\n", $lines); |
125
|
|
|
return utf8_encode($text); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return Context |
130
|
|
|
*/ |
131
|
|
|
protected function getContext() { |
132
|
|
|
$uses = $this->useParser->getUses(); |
133
|
|
|
$namespace = $uses->getFQCN()->toString(); |
134
|
|
|
$aliases = array_map(function($fqcn) { |
135
|
|
|
return $fqcn->toString(); |
136
|
|
|
}, $uses->all()); |
137
|
|
|
return new Context($namespace, $aliases); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** @property UseParser */ |
141
|
|
|
private $useParser; |
142
|
|
|
} |
143
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: