Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.49% |
55 / 57 |
|
88.89% |
16 / 18 |
CRAP | |
0.00% |
0 / 1 |
FunctionTrait | |
96.49% |
55 / 57 |
|
88.89% |
16 / 18 |
35 | |
0.00% |
0 / 1 |
addArgument | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
5.02 | |||
addArguments | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
hasArgument | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasArguments | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getArgument | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getArguments | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addParameter | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addParameters | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
hasParameter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasParameters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getParameter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getParameters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addReturnType | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
addReturnTypes | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
hasReturnType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasReturnTypes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getReturnTypes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
formatArguments | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
7.02 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Code\Generator\Traits; |
15 | |
16 | use Pop\Code\Generator\DocblockGenerator; |
17 | use InvalidArgumentException; |
18 | |
19 | /** |
20 | * Function trait |
21 | * |
22 | * @category Pop |
23 | * @package Pop\Code |
24 | * @author Nick Sagona, III <dev@nolainteractive.com> |
25 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
26 | * @license http://www.popphp.org/license New BSD License |
27 | * @version 5.0.0 |
28 | */ |
29 | trait FunctionTrait |
30 | { |
31 | |
32 | /** |
33 | * Arguments |
34 | * @var array |
35 | */ |
36 | protected array $arguments = []; |
37 | |
38 | /** |
39 | * Return Types |
40 | * @var array |
41 | */ |
42 | protected array $returnTypes = []; |
43 | |
44 | /** |
45 | * Add an argument |
46 | * |
47 | * @param string $name |
48 | * @param mixed $value |
49 | * @param ?string $type |
50 | * @return static |
51 | */ |
52 | public function addArgument(string $name, mixed $value = null, ?string $type = null): static |
53 | { |
54 | $typeHintsNotAllowed = ['integer']; |
55 | $argType = (!in_array($type, $typeHintsNotAllowed)) ? $type : null; |
56 | $this->arguments[$name] = ['value' => $value, 'type' => $argType]; |
57 | |
58 | if ($this->docblock === null) { |
59 | $this->docblock = new DocblockGenerator(null, $this->indent); |
60 | } |
61 | |
62 | if (!str_starts_with($name, '$')) { |
63 | $name = '$' . $name; |
64 | } |
65 | if ($value == 'null') { |
66 | $type .= '|null'; |
67 | } |
68 | $this->docblock->addParam($type, $name); |
69 | |
70 | return $this; |
71 | } |
72 | |
73 | /** |
74 | * Add arguments |
75 | * |
76 | * @param array $args |
77 | * @throws InvalidArgumentException |
78 | * @return static |
79 | */ |
80 | public function addArguments(array $args): static |
81 | { |
82 | foreach ($args as $arg) { |
83 | if (!isset($arg['name'])) { |
84 | throw new InvalidArgumentException("Error: The 'name' key was not set."); |
85 | } |
86 | $value = (isset($arg['value'])) ? $arg['value'] : null; |
87 | $type = (isset($arg['type'])) ? $arg['type'] : null; |
88 | $this->addArgument($arg['name'], $value, $type); |
89 | } |
90 | return $this; |
91 | } |
92 | |
93 | /** |
94 | * Has an argument |
95 | * |
96 | * @param string $name |
97 | * @return bool |
98 | */ |
99 | public function hasArgument(string $name): bool |
100 | { |
101 | return isset($this->arguments[$name]); |
102 | } |
103 | |
104 | /** |
105 | * Has arguments |
106 | * |
107 | * @return bool |
108 | */ |
109 | public function hasArguments(): bool |
110 | { |
111 | return !empty($this->arguments); |
112 | } |
113 | |
114 | /** |
115 | * Get an argument |
116 | * |
117 | * @param string $name |
118 | * @return array|null |
119 | */ |
120 | public function getArgument(string $name): array|null |
121 | { |
122 | return $this->arguments[$name] ?? null; |
123 | } |
124 | |
125 | /** |
126 | * Get the arguments |
127 | * |
128 | * @return array |
129 | */ |
130 | public function getArguments(): array |
131 | { |
132 | return $this->arguments; |
133 | } |
134 | |
135 | /** |
136 | * Add an argument (alias method for convenience) |
137 | * |
138 | * @param string $name |
139 | * @param mixed $value |
140 | * @param ?string $type |
141 | * @return static |
142 | */ |
143 | public function addParameter(string $name, mixed $value = null, ?string $type = null): static |
144 | { |
145 | $this->addArgument($name, $value, $type); |
146 | return $this; |
147 | } |
148 | |
149 | /** |
150 | * Add arguments (alias method for convenience) |
151 | * |
152 | * @param array $args |
153 | * @return static |
154 | */ |
155 | public function addParameters(array $args): static |
156 | { |
157 | $this->addArguments($args); |
158 | return $this; |
159 | } |
160 | |
161 | /** |
162 | * Has an argument (alias method for convenience) |
163 | * |
164 | * @param string $name |
165 | * @return bool |
166 | */ |
167 | public function hasParameter(string $name): bool |
168 | { |
169 | return $this->hasArgument($name); |
170 | } |
171 | |
172 | /** |
173 | * Has arguments (alias method for convenience) |
174 | * |
175 | * @return bool |
176 | */ |
177 | public function hasParameters(): bool |
178 | { |
179 | return $this->hasArguments(); |
180 | } |
181 | |
182 | /** |
183 | * Get an argument (alias method for convenience) |
184 | * |
185 | * @param string $name |
186 | * @return array|null |
187 | */ |
188 | public function getParameter(string $name): array|null |
189 | { |
190 | return $this->getArgument($name); |
191 | } |
192 | |
193 | /** |
194 | * Get the arguments (alias method for convenience) |
195 | * |
196 | * @return array |
197 | */ |
198 | public function getParameters(): array |
199 | { |
200 | return $this->getArguments(); |
201 | } |
202 | |
203 | /** |
204 | * Add a return type |
205 | * |
206 | * @param string $type |
207 | * @return static |
208 | */ |
209 | public function addReturnType(string $type): static |
210 | { |
211 | $typeHintsNotAllowed = ['integer']; |
212 | if (!in_array($type, $typeHintsNotAllowed)) { |
213 | $this->returnTypes[] = $type; |
214 | |
215 | if ($this->docblock === null) { |
216 | $this->docblock = new DocblockGenerator(null, $this->indent); |
217 | } |
218 | |
219 | $this->docblock->setReturn(implode('|', $this->returnTypes)); |
220 | } |
221 | |
222 | return $this; |
223 | } |
224 | |
225 | /** |
226 | * Add return types |
227 | * |
228 | * @param array $types |
229 | * @return static |
230 | */ |
231 | public function addReturnTypes(array $types): static |
232 | { |
233 | foreach ($types as $type) { |
234 | $this->addReturnType($type); |
235 | } |
236 | return $this; |
237 | } |
238 | |
239 | /** |
240 | * Has return type |
241 | * |
242 | * @param string $type |
243 | * @return bool |
244 | */ |
245 | public function hasReturnType(string $type): bool |
246 | { |
247 | return in_array($type, $this->returnTypes); |
248 | } |
249 | |
250 | /** |
251 | * Has return types |
252 | * |
253 | * @return bool |
254 | */ |
255 | public function hasReturnTypes(): bool |
256 | { |
257 | return !empty($this->returnTypes); |
258 | } |
259 | |
260 | /** |
261 | * Get the return types |
262 | * |
263 | * @return array |
264 | */ |
265 | public function getReturnTypes(): array |
266 | { |
267 | return $this->returnTypes; |
268 | } |
269 | |
270 | /** |
271 | * Format the arguments |
272 | * |
273 | * @return string|null |
274 | */ |
275 | protected function formatArguments(): string|null |
276 | { |
277 | $args = null; |
278 | |
279 | $i = 0; |
280 | foreach ($this->arguments as $name => $arg) { |
281 | $i++; |
282 | if ($arg['type'] !== null) { |
283 | $type = $arg['type']; |
284 | if ($arg['value'] == 'null') { |
285 | $type .= '|null'; |
286 | } |
287 | $args .= $type . ' '; |
288 | } |
289 | $args .= (substr($name, 0, 1) != '$') ? "\$" . $name : $name; |
290 | $args .= ($arg['value'] !== null) ? " = " . $arg['value'] : null; |
291 | if ($i < count($this->arguments)) { |
292 | $args .= ', '; |
293 | } |
294 | } |
295 | |
296 | return $args; |
297 | } |
298 | |
299 | } |