-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathFunctionLike.php
More file actions
163 lines (122 loc) · 2.99 KB
/
FunctionLike.php
File metadata and controls
163 lines (122 loc) · 2.99 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
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\PhpGenerator;
use Nette;
use Nette\Utils\Type;
/**
* GlobalFunction/Closure/Method description.
*
* @property-deprecated string $body
*/
abstract class FunctionLike
{
private string $body = '';
/** @var Parameter[] */
private array $parameters = [];
private bool $variadic = false;
private ?string $returnType = null;
private bool $returnReference = false;
private bool $returnNullable = false;
/** @param ?mixed[] $args */
public function setBody(string $code, ?array $args = null): static
{
$this->body = $args === null
? $code
: (new Dumper)->format($code, ...$args);
return $this;
}
public function getBody(): string
{
return $this->body;
}
/** @param ?mixed[] $args */
public function addBody(string $code, ?array $args = null): static
{
$this->body .= ($args === null ? $code : (new Dumper)->format($code, ...$args)) . "\n";
return $this;
}
/**
* @param Parameter[] $val
*/
public function setParameters(array $val): static
{
(function (Parameter ...$val) {})(...$val);
$this->parameters = [];
foreach ($val as $v) {
$this->parameters[$v->getName()] = $v;
}
return $this;
}
/** @return Parameter[] */
public function getParameters(): array
{
return $this->parameters;
}
/**
* @param string $name without $
*/
public function addParameter(string $name, mixed $defaultValue = null): Parameter
{
$param = new Parameter($name);
if (func_num_args() > 1) {
$param->setDefaultValue($defaultValue);
}
return $this->parameters[$name] = $param;
}
/**
* @param string $name without $
*/
public function removeParameter(string $name): static
{
unset($this->parameters[$name]);
return $this;
}
public function setVariadic(bool $state = true): static
{
$this->variadic = $state;
return $this;
}
public function isVariadic(): bool
{
return $this->variadic;
}
public function setReturnType(?string $type): static
{
$this->returnType = Nette\PhpGenerator\Helpers::validateType($type, $this->returnNullable);
return $this;
}
public function getReturnType(bool $asObject = false): Type|string|null
{
return $asObject && $this->returnType
? Type::fromString($this->returnType)
: $this->returnType;
}
public function setReturnReference(bool $state = true): static
{
$this->returnReference = $state;
return $this;
}
public function getReturnReference(): bool
{
return $this->returnReference;
}
public function setReturnNullable(bool $state = true): static
{
$this->returnNullable = $state;
return $this;
}
public function isReturnNullable(): bool
{
return $this->returnNullable;
}
/** @deprecated use isReturnNullable() */
public function getReturnNullable(): bool
{
trigger_error(__METHOD__ . '() is deprecated, use isReturnNullable().', E_USER_DEPRECATED);
return $this->returnNullable;
}
}