1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/* |
5
|
|
|
* Go! AOP framework |
6
|
|
|
* |
7
|
|
|
* @copyright Copyright 2018, Lisachenko Alexander <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This source file is subject to the license that is bundled |
10
|
|
|
* with this source code in the file LICENSE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Go\Proxy\Part; |
14
|
|
|
|
15
|
|
|
use Laminas\Code\Generator\AbstractGenerator; |
16
|
|
|
use ReflectionFunctionAbstract; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Prepares the function call argument list |
20
|
|
|
*/ |
21
|
|
|
final class FunctionCallArgumentListGenerator extends AbstractGenerator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* List of function arguments |
25
|
|
|
* |
26
|
|
|
* @var string[] |
27
|
|
|
*/ |
28
|
|
|
private array $arguments = []; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* If function contains optional arguments |
32
|
|
|
*/ |
33
|
|
|
private bool $hasOptionals = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Definition of variadic argument or null if function is not variadic |
37
|
|
|
*/ |
38
|
|
|
private ?string $variadicArgument = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* FunctionCallArgumentList constructor. |
42
|
|
|
* |
43
|
|
|
* @param ReflectionFunctionAbstract $functionLike Instance of function or method to call |
44
|
|
|
*/ |
45
|
|
|
public function __construct(ReflectionFunctionAbstract $functionLike) |
46
|
17 |
|
{ |
47
|
|
|
parent::__construct(); |
48
|
17 |
|
|
49
|
|
|
foreach ($functionLike->getParameters() as $parameter) { |
50
|
17 |
|
$byReference = ($parameter->isPassedByReference() && !$parameter->isVariadic()) ? '&' : ''; |
51
|
12 |
|
$this->hasOptionals = $this->hasOptionals || $parameter->isOptional(); |
52
|
12 |
|
$this->arguments[] = $byReference . '$' . $parameter->name; |
53
|
12 |
|
} |
54
|
|
|
if ($functionLike->isVariadic()) { |
55
|
17 |
|
// Variadic argument is last and should be handled separately |
56
|
|
|
$this->variadicArgument = array_pop($this->arguments); |
57
|
3 |
|
} |
58
|
|
|
} |
59
|
17 |
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
|
|
public function generate(): string |
64
|
17 |
|
{ |
65
|
|
|
$argumentsPart = []; |
66
|
17 |
|
if ($this->variadicArgument !== null) { |
67
|
17 |
|
$argumentsPart[] = $this->variadicArgument; |
68
|
3 |
|
} |
69
|
|
|
if (!empty($this->arguments)) { |
70
|
17 |
|
$argumentLine = '[' . implode(', ', $this->arguments) . ']'; |
71
|
11 |
|
if ($this->hasOptionals) { |
72
|
11 |
|
$argumentLine = "\\array_slice($argumentLine, 0, \\func_num_args())"; |
73
|
5 |
|
} |
74
|
|
|
array_unshift($argumentsPart, $argumentLine); |
75
|
11 |
|
} |
76
|
|
|
|
77
|
|
|
return implode(', ', $argumentsPart); |
78
|
17 |
|
} |
79
|
|
|
} |
80
|
|
|
|