forked from Respect/Stringifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallableStringifier.php
More file actions
210 lines (173 loc) · 6.23 KB
/
CallableStringifier.php
File metadata and controls
210 lines (173 loc) · 6.23 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
<?php
/*
* This file is part of Respect/Stringifier.
* Copyright (c) Henrique Moody <henriquemoody@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Stringifier\Stringifiers;
use Closure;
use ReflectionFunction;
use ReflectionFunctionAbstract;
use ReflectionIntersectionType;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionParameter;
use ReflectionType;
use ReflectionUnionType;
use Respect\Stringifier\Helpers\ObjectHelper;
use Respect\Stringifier\Quoter;
use Respect\Stringifier\Stringifier;
use function array_keys;
use function array_map;
use function count;
use function implode;
use function is_array;
use function is_callable;
use function is_object;
use function is_string;
use function sprintf;
use function str_contains;
use function strrchr;
use function strstr;
use function substr;
final class CallableStringifier implements Stringifier
{
use ObjectHelper;
public function __construct(
private readonly Stringifier $stringifier,
private readonly Quoter $quoter,
private readonly bool $closureOnly = true,
) {
}
public function stringify(mixed $raw, int $depth): string|null
{
if ($raw instanceof Closure) {
return $this->buildFunction(new ReflectionFunction($raw), $depth);
}
if ($this->closureOnly || !is_callable($raw)) {
return null;
}
if (is_object($raw)) {
return $this->buildMethod(new ReflectionMethod($raw, '__invoke'), $raw, $depth);
}
if (is_array($raw) && isset($raw[0], $raw[1]) && is_object($raw[0]) && is_string($raw[1])) {
return $this->buildMethod(new ReflectionMethod($raw[0], $raw[1]), $raw[0], $depth);
}
if (is_array($raw) && isset($raw[0], $raw[1]) && is_string($raw[0]) && is_string($raw[1])) {
return $this->buildStaticMethod(new ReflectionMethod($raw[0], $raw[1]), $depth);
}
if (!is_string($raw)) {
return null;
}
if (str_contains($raw, ':')) {
/** @var class-string $class */
$class = (string) strstr($raw, ':', true);
$method = substr((string) strrchr($raw, ':'), 1);
return $this->buildStaticMethod(new ReflectionMethod($class, $method), $depth);
}
return $this->buildFunction(new ReflectionFunction($raw), $depth);
}
public function buildFunction(ReflectionFunction $raw, int $depth): string
{
return $this->quoter->quote($this->buildSignature($raw, $depth), $depth);
}
private function buildMethod(ReflectionMethod $reflection, object $object, int $depth): string
{
return $this->quoter->quote(
sprintf('%s->%s', $this->getName($object), $this->buildSignature($reflection, $depth)),
$depth,
);
}
private function buildStaticMethod(ReflectionMethod $reflection, int $depth): string
{
return $this->quoter->quote(
sprintf('%s::%s', $reflection->class, $this->buildSignature($reflection, $depth)),
$depth,
);
}
private function buildSignature(ReflectionFunctionAbstract $function, int $depth): string
{
$signature = sprintf(
'(%s)',
implode(
', ',
array_map(
fn(ReflectionParameter $parameter): string => $this->buildParameter(
$parameter,
$depth + 1,
),
$function->getParameters(),
),
),
);
$closureUsedVariables = $function->getClosureUsedVariables();
if (count($closureUsedVariables)) {
$signature .= sprintf(
' use ($%s)',
implode(
', $',
array_keys($closureUsedVariables),
),
);
}
$returnType = $function->getReturnType();
if ($returnType !== null) {
$signature .= ': ' . $this->buildType($returnType, $depth);
}
if ($function->isClosure()) {
return sprintf('Closure { %sfn%s }', $function->isStatic() ? 'static ' : '', $signature);
}
return $function->getName() . $signature;
}
private function buildParameter(ReflectionParameter $reflectionParameter, int $depth): string
{
$parameter = '';
$type = $reflectionParameter->getType();
if ($type !== null) {
$parameter .= $this->buildType($type, $depth);
}
if ($reflectionParameter->isVariadic()) {
return $parameter . ' ...$' . $reflectionParameter->getName();
}
$parameter .= $reflectionParameter->isPassedByReference() ? ' &' : ' ';
$parameter .= '$' . $reflectionParameter->getName();
if ($reflectionParameter->isOptional()) {
$parameter .= ' = ' . $this->buildValue($reflectionParameter, $depth);
}
return $parameter;
}
private function buildValue(ReflectionParameter $reflectionParameter, int $depth): string|null
{
if (!$reflectionParameter->isDefaultValueAvailable()) {
return $this->stringifier->stringify(null, $depth);
}
if ($reflectionParameter->isDefaultValueConstant()) {
return $reflectionParameter->getDefaultValueConstantName();
}
return $this->stringifier->stringify($reflectionParameter->getDefaultValue(), $depth);
}
private function buildType(ReflectionType $raw, int $depth): string
{
if ($raw instanceof ReflectionUnionType) {
return implode(
'|',
array_map(fn(ReflectionType $type) => $this->buildType($type, $depth), $raw->getTypes()),
);
}
if ($raw instanceof ReflectionIntersectionType) {
return implode(
'&',
array_map(fn(ReflectionType $type) => $this->buildType($type, $depth), $raw->getTypes()),
);
}
if ($raw instanceof ReflectionNamedType) {
$type = $raw->getName();
if ($raw->allowsNull()) {
$type = sprintf('?%s', $type);
}
return $type;
}
return '';
}
}