1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Murtukov\PHPCodeGenerator; |
6
|
|
|
|
7
|
|
|
use function implode; |
8
|
|
|
|
9
|
|
|
class Signature extends DependencyAwareGenerator |
10
|
|
|
{ |
11
|
|
|
use DocBlockTrait; |
12
|
|
|
|
13
|
|
|
public string $name; |
14
|
|
|
public bool $isStatic = false; |
15
|
|
|
public string $modifier; |
16
|
|
|
|
17
|
|
|
protected string $returnType = ''; |
18
|
|
|
protected array $args = []; |
19
|
|
|
protected array $uses = []; // variables of parent scope |
20
|
|
|
protected string $qualifier; |
21
|
|
|
|
22
|
12 |
|
public function __construct( |
23
|
|
|
string $name = '', |
24
|
|
|
string $modifier = Modifier::NONE, |
25
|
|
|
string $returnType = '', |
26
|
|
|
string $qualifier = 'function ' |
27
|
|
|
) { |
28
|
12 |
|
$this->name = $name; |
29
|
12 |
|
$this->modifier = $modifier; |
30
|
12 |
|
$this->returnType = $this->resolveQualifier($returnType); |
31
|
12 |
|
$this->qualifier = $qualifier; |
32
|
|
|
|
33
|
12 |
|
$this->dependencyAwareChildren = [&$this->args]; |
34
|
12 |
|
} |
35
|
|
|
|
36
|
1 |
|
public function getReturnType(): string |
37
|
|
|
{ |
38
|
1 |
|
return $this->returnType; |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
public function setReturnType(string $returnType): self |
42
|
|
|
{ |
43
|
2 |
|
$this->returnType = $this->resolveQualifier($returnType); |
44
|
|
|
|
45
|
2 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Some arguments are stored as simple strings for better performance. |
50
|
|
|
* If they are requested, they are first converted into objects then |
51
|
|
|
* returned back. |
52
|
|
|
* |
53
|
|
|
* @return Argument |
54
|
|
|
*/ |
55
|
3 |
|
public function getArgument(int $index = 1): ?Argument |
56
|
|
|
{ |
57
|
3 |
|
if ($index-- < 1) { |
58
|
1 |
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
if (isset($this->args[$index])) { |
62
|
2 |
|
$arg = $this->args[$index]; |
63
|
|
|
|
64
|
2 |
|
if (is_string($arg)) { |
65
|
1 |
|
return $this->args[$index] = new Argument($arg); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return $arg; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
public function removeArgument(int $index): self |
75
|
|
|
{ |
76
|
1 |
|
unset($this->args[--$index]); |
77
|
|
|
|
78
|
1 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function removeArguments() |
82
|
|
|
{ |
83
|
1 |
|
$this->args = []; |
84
|
|
|
|
85
|
1 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
public function createArgument(string $name, string $type = '', $defaultValue = Argument::NO_PARAM): Argument |
89
|
|
|
{ |
90
|
3 |
|
return $this->args[] = new Argument($name, $type, $defaultValue); |
91
|
|
|
} |
92
|
|
|
|
93
|
4 |
|
public function addArgument(string $name, string $type = '', $defaultValue = Argument::NO_PARAM): self |
94
|
|
|
{ |
95
|
4 |
|
if (1 === func_num_args()) { |
96
|
1 |
|
$this->args[] = "$$name"; |
97
|
|
|
} else { |
98
|
3 |
|
$this->args[] = new Argument($name, $type, $defaultValue); |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
public function addArguments(string ...$names) |
105
|
|
|
{ |
106
|
1 |
|
foreach ($names as $name) { |
107
|
1 |
|
$this->addArgument($name); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
3 |
|
public function add(FunctionMemberInterface $member): self |
114
|
|
|
{ |
115
|
3 |
|
if ($member instanceof Argument) { |
116
|
3 |
|
$this->args[] = $member; |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
public function bindVar(string $name, bool $isByReference = false): self |
123
|
|
|
{ |
124
|
1 |
|
$name = ltrim($name, '$'); |
125
|
|
|
|
126
|
1 |
|
$this->uses[] = $isByReference ? "&$$name" : "$$name"; |
127
|
|
|
|
128
|
1 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
public function bindVars(string ...$names) |
132
|
|
|
{ |
133
|
1 |
|
foreach ($names as $name) { |
134
|
1 |
|
$this->bindVar($name); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
23 |
|
public function generate(bool $withDocBlock = true): string |
141
|
|
|
{ |
142
|
23 |
|
$args = join(', ', $this->args); |
143
|
23 |
|
$uses = ''; |
144
|
23 |
|
$isStatic = $this->isStatic ? 'static ' : ''; |
145
|
23 |
|
$modifier = $this->modifier ? "$this->modifier " : ''; |
146
|
23 |
|
$returnType = ''; |
147
|
|
|
|
148
|
23 |
|
if (!empty($this->uses)) { |
149
|
2 |
|
$uses = ' use ('.implode(', ', $this->uses).')'; |
150
|
|
|
} |
151
|
|
|
|
152
|
23 |
|
if ('' !== $this->returnType) { |
153
|
16 |
|
$returnType = ": $this->returnType"; |
154
|
|
|
} |
155
|
|
|
|
156
|
23 |
|
$docBlock = ''; |
157
|
23 |
|
if ($withDocBlock) { |
158
|
9 |
|
$docBlock = $this->buildDocBlock(); |
159
|
|
|
} |
160
|
|
|
|
161
|
23 |
|
return "{$docBlock}{$modifier}{$isStatic}{$this->qualifier}{$this->name}($args){$uses}{$returnType}"; |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
public function removeBindVars() |
165
|
|
|
{ |
166
|
1 |
|
$this->uses = []; |
167
|
1 |
|
} |
168
|
|
|
} |
169
|
|
|
|