forked from stefna/php-code-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhpMethod.php
More file actions
263 lines (228 loc) · 6.27 KB
/
PhpMethod.php
File metadata and controls
263 lines (228 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php declare(strict_types=1);
namespace Stefna\PhpCodeBuilder;
use Stefna\PhpCodeBuilder\ValueObject\Type;
/**
* Class that represents the source code for a method in php
*
* @author Fredrik Wallgren <fredrik.wallgren@gmail.com>
* @author Andreas Sundqvist <andreas@stefna.is>
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
class PhpMethod extends PhpFunction
{
use AttributeAware;
public const PRIVATE_ACCESS = 'private';
public const PROTECTED_ACCESS = 'protected';
public const PUBLIC_ACCESS = 'public';
protected bool $final = false;
protected bool $static = false;
protected bool $abstract = false;
protected bool $constructor = false;
protected bool $constructorAutoAssign = false;
protected PhpTrait|PhpClass|PhpInterface|null $parent = null;
/**
* @param PhpParam[] $params
* @param array<array-key, string|string[]> $source
*/
public static function constructor(array $params, array $source, bool $autoAssign = false): self
{
$self = new self(
self::PUBLIC_ACCESS,
'__construct',
[],
$source,
Type::empty(),
);
$self->constructor = true;
$self->constructorAutoAssign = $autoAssign;
foreach ($params as $param) {
$self->addParam($param);
}
return $self;
}
/**
* @param array<array-key, string|string[]> $source
*/
public static function setter(
PhpVariable $var,
array $source = [],
bool $fluent = false,
bool $immutable = false,
): self {
$thisName = '$this';
$setterName = 'set' . ucfirst($var->getIdentifier()->toString());
$setterReturnType = Type::fromString('void');
if ($immutable) {
$source[] = '$self = clone $this;';
$thisName = '$self';
$setterName = 'with' . ucfirst($var->getIdentifier()->toString());
$setterReturnType = Type::fromString('self');
}
$source[] = $thisName . '->' . $var->getIdentifier()->toString() . ' = $' . $var->getIdentifier()->toString() . ';';
if ($fluent || $immutable) {
$source[] = 'return ' . $thisName . ';';
}
$valueParam = PhpParam::fromVariable($var);
$valueParam->setType(clone $var->getType());
$self = new self(self::PUBLIC_ACCESS, $setterName, [
$valueParam,
], $source, $setterReturnType);
$var->setSetter($self);
return $self;
}
/**
* @param array<array-key, string|string[]> $source
*/
public static function getter(PhpVariable $var, array $source = []): self
{
$type = $var->getType();
$prefix = 'get';
$aliasPrefix = null;
if ($type->is('bool')) {
$prefix = 'is';
$aliasPrefix = 'has';
}
$methodName = $identifier = $var->getIdentifier()->toString();
if (str_starts_with($identifier, $prefix)) {
$methodName = substr($methodName, strlen($prefix));
}
elseif ($aliasPrefix && str_starts_with($identifier, $aliasPrefix)) {
$methodName = substr($methodName, strlen($aliasPrefix));
$prefix = $aliasPrefix;
}
$source[] = 'return $this->' . $identifier . ';';
$self = self::public($prefix . ucfirst($methodName), [], $source, $var->getType());
$var->setGetter($self);
return $self;
}
/**
* @param PhpParam[] $params
* @param array<array-key, string|string[]> $source
*/
public static function public(string $identifier, array $params, array $source, ?Type $type = null): self
{
return new self(self::PUBLIC_ACCESS, $identifier, $params, $source, $type ?? Type::empty());
}
/**
* @param PhpParam[] $params
* @param array<array-key, string|string[]> $source
*/
public static function private(string $identifier, array $params, array $source, ?Type $type = null): self
{
return new self(self::PRIVATE_ACCESS, $identifier, $params, $source, $type ?? Type::empty());
}
/**
* @param PhpParam[] $params
* @param array<array-key, string|string[]> $source
*/
public static function protected(string $identifier, array $params, array $source, ?Type $type = null): self
{
return new self(self::PROTECTED_ACCESS, $identifier, $params, $source, $type ?? Type::empty());
}
/**
* @param PhpParam[] $params
* @param array<array-key, string|string[]> $source
*/
public function __construct(
protected string $access,
string $identifier,
array $params,
array $source,
?Type $returnTypeHint = null,
?PhpDocComment $comment = null,
) {
parent::__construct(
$identifier,
$params,
$source,
$returnTypeHint ?? Type::empty(),
$comment,
);
}
public function setFinal(): static
{
if ($this->abstract) {
throw new \BadMethodCallException('Can\'t mark method "final" already marked "abstract"');
}
if ($this->access === self::PRIVATE_ACCESS) {
throw new \BadMethodCallException('Can\'t mark method "final" because it\'s marked as private');
}
$this->final = true;
return $this;
}
public function setStatic(): static
{
$this->static = true;
return $this;
}
public function setAbstract(bool $abstract = true): static
{
if ($abstract && $this->access === self::PRIVATE_ACCESS) {
throw new \BadMethodCallException('Can\'t mark "private" methods as abstract');
}
$this->abstract = $abstract;
return $this;
}
public function setAccess(string $access): static
{
$this->access = $access;
return $this;
}
public function getAccess(): string
{
return $this->access;
}
public function isAbstract(): bool
{
return $this->abstract;
}
public function isFinal(): bool
{
return $this->final;
}
public function isStatic(): bool
{
return $this->static;
}
public function setParent(PhpTrait|PhpInterface|PhpClass $parent): static
{
$this->parent = $parent;
if ($this->constructorAutoAssign) {
foreach ($this->params as $param) {
$var = $param->getVariable();
if (
$var &&
!$parent instanceof PhpInterface &&
!$parent->hasVariable($var->getIdentifier())
) {
$parent->addVariable($var);
}
}
}
return $this;
}
public function getParent(): PhpTrait|PhpInterface|PhpClass|null
{
return $this->parent;
}
public function isConstructor(): bool
{
return $this->constructor;
}
public function doConstructorAutoAssign(): bool
{
return $this->constructorAutoAssign;
}
public function addParam(PhpParam $param): static
{
$var = $param->getVariable();
if ($var && $this->parent && !$this->parent->hasVariable($var->getIdentifier())) {
$this->parent->addVariable($var);
}
return parent::addParam($param);
}
public function __clone()
{
parent::__clone();
}
}