1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace gossi\codegen\generator\builder\parts; |
5
|
|
|
|
6
|
|
|
use gossi\codegen\model\AbstractModel; |
7
|
|
|
use gossi\codegen\model\RoutineInterface; |
8
|
|
|
|
9
|
|
|
trait RoutineBuilderPart { |
10
|
|
|
|
11
|
|
|
use TypeBuilderPart; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param AbstractModel $model |
15
|
|
|
* @return void |
16
|
|
|
*/ |
17
|
|
|
abstract protected function generate(AbstractModel $model): void; |
18
|
|
|
|
19
|
16 |
|
protected function writeFunctionStatement(RoutineInterface $model): void { |
20
|
16 |
|
$this->writer->write('function '); |
21
|
|
|
|
22
|
16 |
|
if ($model->isReferenceReturned()) { |
23
|
2 |
|
$this->writer->write('& '); |
24
|
|
|
} |
25
|
|
|
|
26
|
16 |
|
$this->writer->write($model->getName() . '('); |
27
|
16 |
|
$this->writeParameters($model); |
28
|
16 |
|
$this->writer->write(')'); |
29
|
16 |
|
$this->writeFunctionReturnType($model); |
30
|
16 |
|
} |
31
|
|
|
|
32
|
16 |
|
protected function writeParameters(RoutineInterface $model): void { |
33
|
16 |
|
$first = true; |
34
|
16 |
|
foreach ($model->getParameters() as $parameter) { |
35
|
5 |
|
if (!$first) { |
36
|
3 |
|
$this->writer->write(', '); |
37
|
|
|
} |
38
|
5 |
|
$first = false; |
39
|
|
|
|
40
|
5 |
|
$this->generate($parameter); |
41
|
|
|
} |
42
|
16 |
|
} |
43
|
|
|
|
44
|
16 |
|
protected function writeFunctionReturnType(RoutineInterface $model): void { |
45
|
16 |
|
$type = $this->getType($model, $this->config->getGenerateReturnTypeHints(), $this->config->getGenerateNullableTypes()); |
46
|
16 |
|
if ($type !== null && $this->config->getGenerateReturnTypeHints()) { |
47
|
3 |
|
$this->writer->write(': ')->write($type); |
48
|
|
|
} |
49
|
16 |
|
} |
50
|
|
|
|
51
|
15 |
|
protected function writeBody(RoutineInterface $model): void { |
52
|
15 |
|
$this->writer->writeln(' {')->indent(); |
53
|
15 |
|
$this->writer->writeln(trim($model->getBody())); |
54
|
15 |
|
$this->writer->outdent()->rtrim()->writeln('}'); |
55
|
15 |
|
} |
56
|
|
|
} |
57
|
|
|
|