1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Prometee\SwaggerClientGenerator\Base\Generator\Object\Method; |
6
|
|
|
|
7
|
|
|
use Prometee\SwaggerClientGenerator\Base\Generator\Object\Other\UsesGeneratorInterface; |
8
|
|
|
use Prometee\SwaggerClientGenerator\Base\Generator\PhpDoc\PhpDocGeneratorInterface; |
9
|
|
|
|
10
|
|
|
class MethodGenerator implements MethodGeneratorInterface |
11
|
|
|
{ |
12
|
|
|
/** @var PhpDocGeneratorInterface */ |
13
|
|
|
protected $phpDocBuilder; |
14
|
|
|
/** @var UsesGeneratorInterface */ |
15
|
|
|
protected $usesBuilder; |
16
|
|
|
/** @var MethodParameterGeneratorInterface */ |
17
|
|
|
protected $methodParameterBuilderSkel; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $scope = ''; |
21
|
|
|
/** @var string */ |
22
|
|
|
protected $name = ''; |
23
|
|
|
/** @var string[] */ |
24
|
|
|
protected $returnTypes = []; |
25
|
|
|
/** @var bool */ |
26
|
|
|
protected $static = false; |
27
|
|
|
/** @var string */ |
28
|
|
|
protected $description = ''; |
29
|
|
|
/** @var MethodParameterGeneratorInterface[] */ |
30
|
|
|
protected $parameters = []; |
31
|
|
|
/** @var string[] */ |
32
|
|
|
protected $lines = []; |
33
|
|
|
/** @var bool */ |
34
|
|
|
protected $hasAlreadyBeenGenerated = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param UsesGeneratorInterface $usesBuilder |
38
|
|
|
* @param PhpDocGeneratorInterface $phpDocBuilder |
39
|
|
|
* @param MethodParameterGeneratorInterface $methodParameterBuilderSkel |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
UsesGeneratorInterface $usesBuilder, |
43
|
|
|
PhpDocGeneratorInterface $phpDocBuilder, |
44
|
|
|
MethodParameterGeneratorInterface $methodParameterBuilderSkel |
45
|
|
|
) |
46
|
|
|
{ |
47
|
|
|
$this->usesBuilder = $usesBuilder; |
48
|
|
|
$this->phpDocBuilder = $phpDocBuilder; |
49
|
|
|
$this->methodParameterBuilderSkel = $methodParameterBuilderSkel; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function configure( |
56
|
|
|
string $scope, |
57
|
|
|
string $name, |
58
|
|
|
array $returnTypes = [], |
59
|
|
|
bool $static = false, |
60
|
|
|
string $description = '' |
61
|
|
|
) |
62
|
|
|
{ |
63
|
|
|
$this->setScope($scope); |
64
|
|
|
$this->setName($name); |
65
|
|
|
$this->setReturnTypes($returnTypes); |
66
|
|
|
$this->setStatic($static); |
67
|
|
|
$this->setDescription($description); |
68
|
|
|
$this->setParameters([]); |
69
|
|
|
$this->setLines([]); |
70
|
|
|
|
71
|
|
|
$this->phpDocBuilder->configure(); |
72
|
|
|
$this->hasAlreadyBeenGenerated = false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function generate(string $indent = null): ?string |
79
|
|
|
{ |
80
|
|
|
if (count($this->lines) === 0) { |
81
|
|
|
return ''; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$content = "\n"; |
85
|
|
|
|
86
|
|
|
$this->configurePhpDocBuilder(); |
87
|
|
|
$content .= $this->phpDocBuilder->generate($indent); |
88
|
|
|
|
89
|
|
|
$content .= $this->buildMethodSignature($indent); |
90
|
|
|
$content .= "\n"; |
91
|
|
|
|
92
|
|
|
$content .= $indent . '{' . "\n"; |
93
|
|
|
$content .= $this->buildMethodBody($indent); |
94
|
|
|
$content .= $indent . '}' . "\n"; |
95
|
|
|
|
96
|
|
|
return $content; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function buildMethodBody(string $indent = null): string |
103
|
|
|
{ |
104
|
|
|
$content = ''; |
105
|
|
|
foreach ($this->lines as $line) { |
106
|
|
|
foreach (explode("\n", $line) as $innerLine) { |
107
|
|
|
$content .= $indent . $indent . $innerLine . "\n"; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
return $content; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function buildMethodSignature(string $indent = null): string |
117
|
|
|
{ |
118
|
|
|
$static = ($this->static) ? ' static ' : ''; |
119
|
|
|
|
120
|
|
|
return sprintf('%s%s%s function %s (%s)%s', |
121
|
|
|
$indent, |
122
|
|
|
$this->scope, |
123
|
|
|
$static, |
124
|
|
|
$this->name, |
125
|
|
|
$this->buildMethodParameters($indent), |
|
|
|
|
126
|
|
|
$this->buildReturnType($indent) |
|
|
|
|
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
public function buildMethodParameters(string $indent): string |
134
|
|
|
{ |
135
|
|
|
$parameters = []; |
136
|
|
|
|
137
|
|
|
foreach ($this->parameters as $methodParameterBuilder) { |
138
|
|
|
$parameters[] = $methodParameterBuilder->generate($indent); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$parametersStr = implode(',%1$s', $parameters); |
142
|
|
|
|
143
|
|
|
$parameterStart = ''; |
144
|
|
|
$additionalIndentation = ' '; |
145
|
|
|
$parameterEnd = ''; |
146
|
|
|
if (strlen($parametersStr) > $this->phpDocBuilder->getWrapOn()*0.75) { |
147
|
|
|
$additionalIndentation = "\n" . $indent . $indent; |
148
|
|
|
$parameterStart = $additionalIndentation; |
149
|
|
|
$parameterEnd = "\n" . $indent; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
$content = $parameterStart; |
154
|
|
|
$content .= sprintf( |
155
|
|
|
$parametersStr, |
156
|
|
|
$additionalIndentation |
157
|
|
|
); |
158
|
|
|
$content .= $parameterEnd; |
159
|
|
|
|
160
|
|
|
return $content; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
|
|
public function buildReturnType(string $indent): string |
167
|
|
|
{ |
168
|
|
|
if (empty($this->returnTypes)) { |
169
|
|
|
return ''; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (in_array('mixed', $this->returnTypes)) { |
173
|
|
|
return ''; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return sprintf (': %s', $this->getPhpReturnType()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
|
|
public function configurePhpDocBuilder(): void |
183
|
|
|
{ |
184
|
|
|
if ($this->hasAlreadyBeenGenerated) { |
185
|
|
|
return; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if (!empty($this->getDescription())) { |
189
|
|
|
$this->phpDocBuilder->addDescriptionLine($this->getDescription()); |
190
|
|
|
} |
191
|
|
|
foreach ($this->parameters as $parameter) { |
192
|
|
|
$type = $this->phpDocBuilder::getPossibleTypesFromTypeNames([$parameter->getType(), $parameter->getValueType()]); |
193
|
|
|
$this->phpDocBuilder->addParamLine($parameter->getPhpName(), $type, $parameter->getDescription()); |
194
|
|
|
} |
195
|
|
|
if (!empty($this->returnTypes) && !in_array('void', $this->returnTypes)) { |
196
|
|
|
$type = $this->phpDocBuilder::getPossibleTypesFromTypeNames($this->returnTypes); |
197
|
|
|
$this->phpDocBuilder->addReturnLine($type); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$this->hasAlreadyBeenGenerated = true; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritdoc} |
205
|
|
|
*/ |
206
|
|
|
public function getReturnTypes(): array |
207
|
|
|
{ |
208
|
|
|
return $this->returnTypes; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritdoc} |
213
|
|
|
*/ |
214
|
|
|
public function getPhpReturnType(): ?string |
215
|
|
|
{ |
216
|
|
|
if (empty($this->returnTypes)) { |
217
|
|
|
return null; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$phpReturnType = ''; |
221
|
|
|
if (in_array('null', $this->returnTypes)) { |
222
|
|
|
$phpReturnType = '?'; |
223
|
|
|
} |
224
|
|
|
foreach ($this->returnTypes as $type) { |
225
|
|
|
if (preg_match('#\[\]$#', $type)) { |
226
|
|
|
$phpReturnType .= 'array'; |
227
|
|
|
break; |
228
|
|
|
} |
229
|
|
|
if ($type !== 'null') { |
230
|
|
|
$phpReturnType .= $type; |
231
|
|
|
break; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $phpReturnType; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* {@inheritDoc} |
240
|
|
|
*/ |
241
|
|
|
public function setReturnTypes(array $returnTypes): void |
242
|
|
|
{ |
243
|
|
|
$this->returnTypes = []; |
244
|
|
|
foreach ($returnTypes as $returnType) { |
245
|
|
|
$this->addReturnType($returnType); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* {@inheritDoc} |
251
|
|
|
*/ |
252
|
|
|
public function addReturnType(string $returnType): void |
253
|
|
|
{ |
254
|
|
|
$returnType = $this->usesBuilder->guessUseOrReturnType($returnType); |
255
|
|
|
if (false === $this->hasReturnType($returnType)) { |
256
|
|
|
$this->returnTypes[] = $returnType; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* {@inheritDoc} |
262
|
|
|
*/ |
263
|
|
|
public function hasReturnType(string $returnType): bool |
264
|
|
|
{ |
265
|
|
|
return false !== array_search($returnType, $this->returnTypes); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* {@inheritdoc} |
270
|
|
|
*/ |
271
|
|
|
public function addParameter(MethodParameterGeneratorInterface $methodParameterBuilder): void |
272
|
|
|
{ |
273
|
|
|
if (!$this->hasParameter($methodParameterBuilder)) { |
274
|
|
|
$this->setParameter($methodParameterBuilder); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* {@inheritdoc} |
280
|
|
|
*/ |
281
|
|
|
public function hasParameter(MethodParameterGeneratorInterface $methodParameterBuilder): bool |
282
|
|
|
{ |
283
|
|
|
return isset($this->parameters[$methodParameterBuilder->getName()]); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* {@inheritdoc} |
288
|
|
|
*/ |
289
|
|
|
public function setParameter(MethodParameterGeneratorInterface $methodParameterBuilder): void |
290
|
|
|
{ |
291
|
|
|
$this->parameters[$methodParameterBuilder->getName()] = $methodParameterBuilder; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* {@inheritdoc} |
296
|
|
|
*/ |
297
|
|
|
public function addLine(string $line): void |
298
|
|
|
{ |
299
|
|
|
$this->lines[] = $line; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* {@inheritdoc} |
304
|
|
|
*/ |
305
|
|
|
public function getScope(): string |
306
|
|
|
{ |
307
|
|
|
return $this->scope; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* {@inheritdoc} |
312
|
|
|
*/ |
313
|
|
|
public function setScope(string $scope): void |
314
|
|
|
{ |
315
|
|
|
$this->scope = $scope; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* {@inheritdoc} |
320
|
|
|
*/ |
321
|
|
|
public function isStatic(): bool |
322
|
|
|
{ |
323
|
|
|
return $this->static; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* {@inheritdoc} |
328
|
|
|
*/ |
329
|
|
|
public function setStatic(bool $static): void |
330
|
|
|
{ |
331
|
|
|
$this->static = $static; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* {@inheritdoc} |
336
|
|
|
*/ |
337
|
|
|
public function getName(): string |
338
|
|
|
{ |
339
|
|
|
return $this->name; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* {@inheritdoc} |
344
|
|
|
*/ |
345
|
|
|
public function setName(string $name): void |
346
|
|
|
{ |
347
|
|
|
$this->name = $name; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* {@inheritdoc} |
352
|
|
|
*/ |
353
|
|
|
public function getDescription(): string |
354
|
|
|
{ |
355
|
|
|
return $this->description; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* {@inheritdoc} |
360
|
|
|
*/ |
361
|
|
|
public function setDescription(string $description): void |
362
|
|
|
{ |
363
|
|
|
$this->description = $description; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* {@inheritdoc} |
368
|
|
|
*/ |
369
|
|
|
public function getParameters(): array |
370
|
|
|
{ |
371
|
|
|
return $this->parameters; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* {@inheritdoc} |
376
|
|
|
*/ |
377
|
|
|
public function setParameters(array $parameters): void |
378
|
|
|
{ |
379
|
|
|
$this->parameters = $parameters; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* {@inheritdoc} |
384
|
|
|
*/ |
385
|
|
|
public function getLines(): array |
386
|
|
|
{ |
387
|
|
|
return $this->lines; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* {@inheritdoc} |
392
|
|
|
*/ |
393
|
|
|
public function setLines(array $lines): void |
394
|
|
|
{ |
395
|
|
|
$this->lines = $lines; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* {@inheritdoc} |
400
|
|
|
*/ |
401
|
|
|
public function getPhpDocBuilder(): PhpDocGeneratorInterface |
402
|
|
|
{ |
403
|
|
|
return $this->phpDocBuilder; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* {@inheritdoc} |
408
|
|
|
*/ |
409
|
|
|
public function setPhpDocBuilder(PhpDocGeneratorInterface $phpDocBuilder): void |
410
|
|
|
{ |
411
|
|
|
$this->phpDocBuilder = $phpDocBuilder; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* {@inheritdoc} |
416
|
|
|
*/ |
417
|
|
|
public function getUsesBuilder(): UsesGeneratorInterface |
418
|
|
|
{ |
419
|
|
|
return $this->usesBuilder; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* {@inheritdoc} |
424
|
|
|
*/ |
425
|
|
|
public function setUsesBuilder(UsesGeneratorInterface $usesBuilder): void |
426
|
|
|
{ |
427
|
|
|
$this->usesBuilder = $usesBuilder; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* {@inheritdoc} |
432
|
|
|
*/ |
433
|
|
|
public function getMethodParameterBuilderSkel(): MethodParameterGeneratorInterface |
434
|
|
|
{ |
435
|
|
|
return $this->methodParameterBuilderSkel; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* {@inheritdoc} |
440
|
|
|
*/ |
441
|
|
|
public function setMethodParameterBuilderSkel(MethodParameterGeneratorInterface $methodParameterBuilderSkel): void |
442
|
|
|
{ |
443
|
|
|
$this->methodParameterBuilderSkel = $methodParameterBuilderSkel; |
444
|
|
|
} |
445
|
|
|
} |
446
|
|
|
|