|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author stev leibelt <[email protected]> |
|
4
|
|
|
* @since 2014-04-24 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\CodeGenerator; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class MethodGenerator |
|
11
|
|
|
* @package Net\Bazzline\Component\Locator\LocatorGenerator\Generator |
|
12
|
|
|
*/ |
|
13
|
|
|
class MethodGenerator extends AbstractDocumentedGenerator |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param string $name |
|
17
|
|
|
* @param string $defaultValue |
|
18
|
|
|
* @param string $typeHint |
|
19
|
|
|
* @param bool $isReference |
|
20
|
|
|
* @return $this |
|
21
|
|
|
*/ |
|
22
|
|
|
public function addParameter($name, $defaultValue = '', $typeHint = '', $isReference = false) |
|
23
|
|
|
{ |
|
24
|
|
|
$parameter = [ |
|
25
|
|
|
'default_value' => $defaultValue, |
|
26
|
|
|
'name' => $name, |
|
27
|
|
|
'is_reference' => $isReference, |
|
28
|
|
|
'type_hint' => $typeHint |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
$this->addGeneratorProperty('parameters', $parameter); |
|
32
|
|
|
|
|
33
|
|
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param float|string $version |
|
38
|
|
|
* @param string $description |
|
39
|
|
|
* @return $this |
|
40
|
|
|
* @see http://www.phpdoc.org/docs/latest/for-users/phpdoc/tags/since.html |
|
41
|
|
|
*/ |
|
42
|
|
|
public function addSince($version, $description = '') |
|
43
|
|
|
{ |
|
44
|
|
|
$since = [ |
|
45
|
|
|
'description' => $description, |
|
46
|
|
|
'version' => $version |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
$this->addGeneratorProperty('since_versions', $since); |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return null|array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getBody() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->getGeneratorProperty('body', null); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string|array|LineGenerator|BlockGenerator $body |
|
64
|
|
|
* @param array $returnValueTypeHints |
|
65
|
|
|
* @return $this |
|
66
|
|
|
*/ |
|
67
|
|
|
public function setBody($body, $returnValueTypeHints = []) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->addGeneratorProperty('body', $body, false); |
|
70
|
|
|
$this->addGeneratorProperty('has_body', true, false); |
|
71
|
|
|
$this->addGeneratorProperty('body_return_value_type_hints', $returnValueTypeHints, false); |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
|
|
public function markAsAbstract() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->addGeneratorProperty('abstract', true, false); |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return $this |
|
88
|
|
|
*/ |
|
89
|
|
|
public function markAsHasNoBody() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->addGeneratorProperty('has_body', false, false); |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return $this |
|
98
|
|
|
*/ |
|
99
|
|
|
public function markAsFinal() |
|
100
|
|
|
{ |
|
101
|
|
|
$this->addGeneratorProperty('final', true, false); |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return $this |
|
108
|
|
|
*/ |
|
109
|
|
|
public function markAsPrivate() |
|
110
|
|
|
{ |
|
111
|
|
|
$this->addGeneratorProperty('visibility', 'private', false); |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return $this |
|
118
|
|
|
*/ |
|
119
|
|
|
public function markAsProtected() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->addGeneratorProperty('visibility', 'protected', false); |
|
122
|
|
|
|
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
|
|
public function markAsPublic() |
|
130
|
|
|
{ |
|
131
|
|
|
$this->addGeneratorProperty('visibility', 'public', false); |
|
132
|
|
|
|
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return $this |
|
138
|
|
|
*/ |
|
139
|
|
|
public function markAsStatic() |
|
140
|
|
|
{ |
|
141
|
|
|
$this->addGeneratorProperty('static', true, false); |
|
142
|
|
|
|
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param string $name |
|
148
|
|
|
* @return $this |
|
149
|
|
|
*/ |
|
150
|
|
|
public function setName($name) |
|
151
|
|
|
{ |
|
152
|
|
|
$this->addGeneratorProperty('name', (string) $name, false); |
|
153
|
|
|
|
|
154
|
|
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @throws InvalidArgumentException|RuntimeException |
|
159
|
|
|
* @return string |
|
160
|
|
|
* @todo implement exception throwing if mandatory parameter is missing |
|
161
|
|
|
*/ |
|
162
|
|
|
public function generate() |
|
163
|
|
|
{ |
|
164
|
|
|
$this->resetContent(); |
|
165
|
|
|
$this->generateDocumentation(); |
|
166
|
|
|
$this->generateSignature(); |
|
167
|
|
|
$this->generateBody(); |
|
168
|
|
|
|
|
169
|
|
|
return $this->generateStringFromContent(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
private function generateBody() |
|
173
|
|
|
{ |
|
174
|
|
|
$body = $this->getGeneratorProperty('body', ['//@todo implement']); |
|
175
|
|
|
$isAbstract = $this->getGeneratorProperty('abstract', false); |
|
176
|
|
|
$hasBody = $this->getGeneratorProperty('has_body', true); |
|
177
|
|
|
|
|
178
|
|
|
if (!$isAbstract |
|
179
|
|
|
&& $hasBody) { |
|
180
|
|
|
$this->addContent('{'); |
|
181
|
|
|
if (!($body instanceof BlockGenerator) |
|
182
|
|
|
|| !($body instanceof LineGenerator)) { |
|
183
|
|
|
if (!is_array($body)) { |
|
184
|
|
|
$body = [ |
|
185
|
|
|
$body |
|
186
|
|
|
]; |
|
187
|
|
|
} |
|
188
|
|
|
$body = $this->getBlockGenerator($body); |
|
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
$this->addGeneratorAsContent( |
|
191
|
|
|
$body, |
|
192
|
|
|
true |
|
193
|
|
|
); |
|
194
|
|
|
$this->addContent('}'); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
private function generateDocumentation() |
|
199
|
|
|
{ |
|
200
|
|
|
$documentation = $this->getGeneratorProperty('documentation'); |
|
201
|
|
|
|
|
202
|
|
|
if ($documentation instanceof DocumentationGenerator) { |
|
203
|
|
|
if ($this->completeDocumentationAutomatically === true) { |
|
204
|
|
|
$parameters = $this->getGeneratorProperty('parameters', []); |
|
205
|
|
|
$returnTypeHints = $this->getGeneratorProperty('body_return_value_type_hints'); |
|
206
|
|
|
|
|
207
|
|
|
foreach ($parameters as $parameter) { |
|
|
|
|
|
|
208
|
|
|
$documentation->addParameter($parameter['name'], $parameter['type_hint']); |
|
209
|
|
|
} |
|
210
|
|
|
if (is_array($returnTypeHints)) { |
|
211
|
|
|
$documentation->setReturn($returnTypeHints); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
$this->addGeneratorAsContent($documentation); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
private function generateSignature() |
|
219
|
|
|
{ |
|
220
|
|
|
if ($this->canBeGenerated()) { |
|
221
|
|
|
$hasBody = $this->getGeneratorProperty('has_body', true); |
|
222
|
|
|
$isAbstract = $this->getGeneratorProperty('abstract', false); |
|
223
|
|
|
$isFinal = $this->getGeneratorProperty('final', false); |
|
224
|
|
|
$isStatic = $this->getGeneratorProperty('static', false); |
|
225
|
|
|
$name = $this->getGeneratorProperty('name'); |
|
226
|
|
|
$parameters = $this->getGeneratorProperty('parameters', []); |
|
227
|
|
|
$visibility = $this->getGeneratorProperty('visibility'); |
|
228
|
|
|
|
|
229
|
|
|
//@todo refactor the wired usage for line and block generator |
|
230
|
|
|
$line = $this->getLineGenerator(); |
|
231
|
|
|
|
|
232
|
|
|
if ($isAbstract) { |
|
233
|
|
|
$line->add('abstract'); |
|
234
|
|
|
} else if ($isFinal) { |
|
235
|
|
|
$line->add('final'); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
if (!is_null($visibility)) { |
|
239
|
|
|
$line->add($visibility); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
if ($isStatic) { |
|
243
|
|
|
$line->add('static'); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
$parametersLine = $this->getLineGenerator(); |
|
247
|
|
|
$parametersLine->setSeparator(', '); |
|
248
|
|
|
if (is_array($parameters)) { |
|
249
|
|
|
foreach ($parameters as $parameter) { |
|
250
|
|
|
$parameterLine = $this->getLineGenerator(); |
|
251
|
|
|
if ((strlen($parameter['type_hint']) > 0) |
|
252
|
|
|
&& (!in_array($parameter['type_hint'], $this->getNotPrintableTypeHints()))) { |
|
253
|
|
|
$parameterLine->add($parameter['type_hint']); |
|
254
|
|
|
} |
|
255
|
|
|
$parameterLine->add(($parameter['is_reference'] ? '&' : '') . '$' . $parameter['name']); |
|
256
|
|
|
if (strlen((string) $parameter['default_value']) > 0) { |
|
257
|
|
|
$parameterLine->add('= ' . (string) $parameter['default_value']); |
|
258
|
|
|
} |
|
259
|
|
|
$parametersLine->add($parameterLine); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
$line->add('function ' . $name . '(' . $parametersLine->generate() . ')' . ((($isAbstract) || (!$hasBody)) ? ';' : '')); |
|
264
|
|
|
$block = $this->getBlockGenerator($line); |
|
265
|
|
|
$this->addContent($block); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
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: