|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ClassGeneration package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Antonio Spinelli <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ClassGeneration\DocBlock; |
|
13
|
|
|
|
|
14
|
|
|
use ClassGeneration\ArgumentInterface; |
|
15
|
|
|
use ClassGeneration\Collection\ArrayCollection; |
|
16
|
|
|
use ClassGeneration\Element\ElementAbstract; |
|
17
|
|
|
use ClassGeneration\Element\ElementInterface; |
|
18
|
|
|
use ClassGeneration\PropertyInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Antonio Spinelli <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Tag extends ElementAbstract implements TagInterface |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Tag Name. |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $name; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Tag Type. |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $type; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Define a parameter name. |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $variable; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Tag Description. |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $description; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Element Referenced. |
|
52
|
|
|
* @var ElementInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $referenced; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Tags containing type. |
|
58
|
|
|
* @var ArrayCollection |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $tagNeedsType; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Inline documentation, that is to put {} around of the text. |
|
64
|
|
|
* @var boolean |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $isInline = false; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
47 |
|
public function init() |
|
72
|
|
|
{ |
|
73
|
47 |
|
$this->tagNeedsType = new ArrayCollection( |
|
74
|
47 |
|
array('access', 'param', 'property', 'method', 'return', 'throws', 'var') |
|
75
|
47 |
|
); |
|
76
|
47 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
28 |
|
public function getName() |
|
82
|
|
|
{ |
|
83
|
28 |
|
return $this->name; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
25 |
|
public function setName($name) |
|
90
|
|
|
{ |
|
91
|
25 |
|
$this->name = $name; |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
25 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
15 |
|
public function getType() |
|
100
|
|
|
{ |
|
101
|
15 |
|
if ((is_null($this->type) || empty($this->type)) && $this->needsType()) { |
|
102
|
10 |
|
return 'mixed'; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
5 |
|
return $this->type; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
15 |
|
public function setType($type) |
|
112
|
|
|
{ |
|
113
|
15 |
|
$this->type = $type; |
|
114
|
|
|
|
|
115
|
15 |
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritdoc} |
|
120
|
|
|
*/ |
|
121
|
14 |
|
public function getVariable() |
|
122
|
|
|
{ |
|
123
|
14 |
|
return $this->variable; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
*/ |
|
129
|
10 |
|
public function setVariable($variable) |
|
130
|
|
|
{ |
|
131
|
10 |
|
$this->variable = $variable; |
|
132
|
|
|
|
|
133
|
10 |
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* {@inheritdoc} |
|
138
|
|
|
*/ |
|
139
|
18 |
|
public function getDescription() |
|
140
|
|
|
{ |
|
141
|
18 |
|
return $this->description; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* {@inheritdoc} |
|
146
|
|
|
*/ |
|
147
|
20 |
|
public function setDescription($description) |
|
148
|
|
|
{ |
|
149
|
20 |
|
$this->description = $description; |
|
150
|
|
|
|
|
151
|
20 |
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* This tag, has type? |
|
156
|
|
|
* @return bool |
|
157
|
|
|
*/ |
|
158
|
10 |
|
protected function needsType() |
|
159
|
|
|
{ |
|
160
|
10 |
|
return $this->tagNeedsType->contains($this->getName()); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* {@inheritdoc} |
|
165
|
|
|
*/ |
|
166
|
3 |
|
public function getReferenced() |
|
167
|
|
|
{ |
|
168
|
3 |
|
return $this->referenced; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* {@inheritdoc} |
|
173
|
|
|
*/ |
|
174
|
12 |
|
public function setReferenced(ElementInterface $referenced) |
|
175
|
|
|
{ |
|
176
|
12 |
|
$this->referenced = $referenced; |
|
177
|
|
|
|
|
178
|
12 |
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* {@inheritdoc} |
|
183
|
|
|
*/ |
|
184
|
14 |
|
public function isInline() |
|
185
|
|
|
{ |
|
186
|
14 |
|
return $this->isInline; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* {@inheritdoc} |
|
191
|
|
|
*/ |
|
192
|
2 |
|
public function setIsInline($isInline = true) |
|
193
|
|
|
{ |
|
194
|
2 |
|
$this->isInline = (bool)$isInline; |
|
195
|
|
|
|
|
196
|
2 |
|
return $this; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* {@inheritdoc} |
|
201
|
|
|
*/ |
|
202
|
13 |
|
public function toString() |
|
203
|
|
|
{ |
|
204
|
13 |
|
$name = $this->getName(); |
|
205
|
13 |
|
$variable = $this->getVariable(); |
|
206
|
13 |
|
$type = $this->getType(); |
|
207
|
13 |
|
$description = $this->getDescription(); |
|
208
|
|
|
$string = |
|
209
|
|
|
'@' . $name |
|
210
|
13 |
|
. ((!is_null($type) && !empty($type)) ? ' ' . $type : '') |
|
211
|
13 |
|
. ((!is_null($variable) && !empty($variable)) ? ' $' . $variable : '') |
|
212
|
13 |
|
. ((!is_null($description) && !empty($description)) ? ' ' . $description : ''); |
|
213
|
|
|
|
|
214
|
13 |
|
$string = trim($string); |
|
215
|
13 |
|
if ($this->isInline()) { |
|
216
|
1 |
|
return '{' . $string . '}' . PHP_EOL; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
12 |
|
return $string . PHP_EOL; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Creating a Tag from an Argument Object. |
|
224
|
|
|
* |
|
225
|
|
|
* @param ArgumentInterface $argument |
|
226
|
|
|
* |
|
227
|
|
|
* @return TagInterface |
|
228
|
|
|
*/ |
|
229
|
8 |
|
public static function createFromArgument(ArgumentInterface $argument) |
|
230
|
|
|
{ |
|
231
|
8 |
|
$tag = new self(); |
|
232
|
|
|
$tag |
|
233
|
8 |
|
->setName(self::TAG_PARAM) |
|
234
|
8 |
|
->setType($argument->getType()) |
|
235
|
8 |
|
->setVariable($argument->getName()) |
|
236
|
8 |
|
->setDescription($argument->getDescription()) |
|
237
|
8 |
|
->setReferenced($argument); |
|
238
|
|
|
|
|
239
|
8 |
|
return $tag; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Create var tag from property. |
|
244
|
|
|
* |
|
245
|
|
|
* @param PropertyInterface $property |
|
246
|
|
|
* |
|
247
|
|
|
* @return Tag |
|
248
|
|
|
*/ |
|
249
|
3 |
|
public static function createFromProperty(PropertyInterface $property) |
|
250
|
|
|
{ |
|
251
|
3 |
|
$tag = new self(); |
|
252
|
3 |
|
$tag->setName(self::TAG_VAR); |
|
253
|
3 |
|
$tag->setType($property->getType()); |
|
254
|
|
|
|
|
255
|
3 |
|
return $tag; |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.