1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 03.09.16 at 11:30 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonframework\generator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Function generation class. |
10
|
|
|
* |
11
|
|
|
* @author Vitaly Egorov <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class FunctionGenerator extends AbstractGenerator |
14
|
|
|
{ |
15
|
|
|
use CodeTrait; |
16
|
|
|
|
17
|
|
|
/** @var string Function name */ |
18
|
|
|
protected $name; |
19
|
|
|
|
20
|
|
|
/** @var array Collection of function arguments */ |
21
|
|
|
protected $arguments = []; |
22
|
|
|
|
23
|
|
|
/** @var array Collection of function arguments descriptions */ |
24
|
|
|
protected $argumentDescriptions = []; |
25
|
|
|
|
26
|
|
|
/** @var array Collection of function arguments default values */ |
27
|
|
|
protected $argumentDefaults = []; |
28
|
|
|
|
29
|
|
|
/** @var string Return type hint */ |
30
|
|
|
protected $returnType; |
31
|
|
|
|
32
|
|
|
/** @var CommentsGenerator */ |
33
|
|
|
protected $commentGenerator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* FunctionGenerator constructor. |
37
|
|
|
* |
38
|
|
|
* @param string $name Function name |
39
|
|
|
* @param AbstractGenerator $parent Parent Generator |
40
|
|
|
*/ |
41
|
20 |
|
public function __construct(string $name, AbstractGenerator $parent = null) |
42
|
|
|
{ |
43
|
20 |
|
$this->name = $name; |
44
|
|
|
|
45
|
20 |
|
$this->commentGenerator = (new CommentsGenerator($this)) |
46
|
20 |
|
->setIndentation($this->indentation); |
47
|
|
|
|
48
|
20 |
|
parent::__construct($parent); |
49
|
20 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set function argument. |
53
|
|
|
* |
54
|
|
|
* @param string $name Argument name |
55
|
|
|
* @param string|null $type Argument type |
56
|
|
|
* @param string $description Argument description |
57
|
|
|
* @param mixed $defaultValue Argument default value |
58
|
|
|
* |
59
|
|
|
* @return FunctionGenerator |
60
|
|
|
*/ |
61
|
8 |
|
public function defArgument( |
62
|
|
|
string $name, |
63
|
|
|
string $type = null, |
64
|
|
|
string $description = null, |
65
|
|
|
$defaultValue = null |
66
|
|
|
): FunctionGenerator |
67
|
|
|
{ |
|
|
|
|
68
|
8 |
|
$this->arguments[$name] = $type; |
69
|
8 |
|
$this->argumentDescriptions[$name] = $description; |
70
|
8 |
|
$this->argumentDefaults[$name] = $defaultValue; |
71
|
|
|
|
72
|
8 |
|
$this->commentGenerator->defParam($name, $type, $description); |
73
|
|
|
|
74
|
8 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
6 |
|
public function setIndentation(int $indentation): AbstractGenerator |
81
|
|
|
{ |
82
|
|
|
// Set comments indentation |
83
|
6 |
|
$this->commentGenerator->setIndentation($indentation); |
84
|
|
|
|
85
|
6 |
|
return parent::setIndentation($indentation); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set function description comment. |
90
|
|
|
* |
91
|
|
|
* @param array $description Function description |
92
|
|
|
* |
93
|
|
|
* @return FunctionGenerator |
94
|
|
|
*/ |
95
|
|
|
public function defDescription(array $description): FunctionGenerator |
96
|
|
|
{ |
97
|
|
|
// Append description lines |
98
|
|
|
foreach ($description as $line) { |
99
|
|
|
$this->commentGenerator->defLine($line); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Add one empty line at the end |
103
|
|
|
$this->commentGenerator->defLine(''); |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set return value type |
110
|
|
|
* |
111
|
|
|
* @param string|null $type Return type hint |
112
|
|
|
* |
113
|
|
|
* @param string $description Return type description |
114
|
|
|
* |
115
|
|
|
* @return FunctionGenerator |
116
|
|
|
*/ |
117
|
1 |
|
public function defReturnType(string $type, string $description = ''): FunctionGenerator |
118
|
|
|
{ |
119
|
|
|
// Add comment return type |
120
|
1 |
|
$this->commentGenerator->defReturn($type, $description); |
121
|
|
|
|
122
|
|
|
// Set method definition return type, mixed return types are not supported yet |
123
|
1 |
|
$this->returnType = strpos($type, '|') !== false ? '' : $type; |
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
15 |
|
public function code(): string |
132
|
|
|
{ |
133
|
15 |
|
$formattedCode[] = |
|
|
|
|
134
|
15 |
|
$this->indentation($this->indentation) . $this->buildDefinition() |
135
|
15 |
|
. '(' . $this->buildArguments($this->arguments, $this->argumentDefaults) . ')' |
136
|
15 |
|
. ($this->returnType ? ' : ' . $this->returnType : ''); |
137
|
|
|
|
138
|
|
|
// Prepend inner indentation to code |
139
|
15 |
|
$innerIndentation = $this->indentation(1); |
140
|
15 |
|
$formattedCode[] = '{'; |
141
|
15 |
|
foreach ($this->code as $codeLine) { |
142
|
9 |
|
$formattedCode[] = $innerIndentation . $codeLine; |
143
|
|
|
} |
144
|
15 |
|
$formattedCode[] = '}'; |
145
|
|
|
|
146
|
|
|
// Generate function comments |
147
|
15 |
|
$this->commentGenerator->end(); |
148
|
|
|
|
149
|
|
|
// Get function comments code |
150
|
15 |
|
$comments = $this->getNestedCode(CommentsGenerator::class); |
151
|
|
|
|
152
|
15 |
|
return "\n\n" . ($comments !== '' ? $comments . "\n" : '') . implode("\n" . $this->indentation($this->indentation), $formattedCode); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Build function definition. |
157
|
|
|
* |
158
|
|
|
* @return string Function definition |
159
|
|
|
*/ |
160
|
5 |
|
protected function buildDefinition() |
161
|
|
|
{ |
162
|
5 |
|
return 'function ' . $this->name; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
7 |
|
public function defComment(): CommentsGenerator |
169
|
|
|
{ |
170
|
7 |
|
return $this->commentGenerator; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|