-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
ReturnValueGenerator.php
242 lines (213 loc) · 7.12 KB
/
ReturnValueGenerator.php
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
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Framework\MockObject;
use function array_keys;
use function array_map;
use function explode;
use function in_array;
use function interface_exists;
use function sprintf;
use function str_contains;
use function str_ends_with;
use function str_starts_with;
use function substr;
use PHPUnit\Framework\MockObject\Generator\Generator;
use ReflectionClass;
use stdClass;
use Throwable;
/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class ReturnValueGenerator
{
/**
* @psalm-param class-string $className
* @psalm-param non-empty-string $methodName
* @psalm-param class-string $stubClassName
*
* @throws Exception
*/
public function generate(string $className, string $methodName, string $stubClassName, string $returnType): mixed
{
$intersection = false;
$union = false;
if (str_contains($returnType, '|')) {
$types = explode('|', $returnType);
$union = true;
foreach (array_keys($types) as $key) {
if (str_starts_with($types[$key], '(') && str_ends_with($types[$key], ')')) {
$types[$key] = substr($types[$key], 1, -1);
}
}
} elseif (str_contains($returnType, '&')) {
$types = explode('&', $returnType);
$intersection = true;
} else {
$types = [$returnType];
}
if (!$intersection) {
$lowerTypes = array_map('strtolower', $types);
if (in_array('', $lowerTypes, true) ||
in_array('null', $lowerTypes, true) ||
in_array('mixed', $lowerTypes, true) ||
in_array('void', $lowerTypes, true)) {
return null;
}
if (in_array('true', $lowerTypes, true)) {
return true;
}
if (in_array('false', $lowerTypes, true) ||
in_array('bool', $lowerTypes, true)) {
return false;
}
if (in_array('float', $lowerTypes, true)) {
return 0.0;
}
if (in_array('int', $lowerTypes, true)) {
return 0;
}
if (in_array('string', $lowerTypes, true)) {
return '';
}
if (in_array('array', $lowerTypes, true)) {
return [];
}
if (in_array('static', $lowerTypes, true)) {
return $this->newInstanceOf($stubClassName, $className, $methodName);
}
if (in_array('object', $lowerTypes, true)) {
return new stdClass;
}
if (in_array('callable', $lowerTypes, true) ||
in_array('closure', $lowerTypes, true)) {
return static function (): void
{
};
}
if (in_array('traversable', $lowerTypes, true) ||
in_array('generator', $lowerTypes, true) ||
in_array('iterable', $lowerTypes, true)) {
$generator = static function (): \Generator
{
yield from [];
};
return $generator();
}
if (!$union) {
return $this->testDoubleFor($returnType, $className, $methodName);
}
}
if ($union) {
foreach ($types as $type) {
if (str_contains($type, '&')) {
$_types = explode('&', $type);
if ($this->onlyInterfaces($_types)) {
return $this->testDoubleForIntersectionOfInterfaces($_types, $className, $methodName);
}
}
}
}
if ($intersection && $this->onlyInterfaces($types)) {
return $this->testDoubleForIntersectionOfInterfaces($types, $className, $methodName);
}
$reason = '';
if ($union) {
$reason = ' because the declared return type is a union';
} elseif ($intersection) {
$reason = ' because the declared return type is an intersection';
}
throw new RuntimeException(
sprintf(
'Return value for %s::%s() cannot be generated%s, please configure a return value for this method',
$className,
$methodName,
$reason,
),
);
}
/**
* @psalm-param non-empty-list<string> $types
*/
private function onlyInterfaces(array $types): bool
{
foreach ($types as $type) {
if (!interface_exists($type)) {
return false;
}
}
return true;
}
/**
* @psalm-param class-string $stubClassName
* @psalm-param class-string $className
* @psalm-param non-empty-string $methodName
*
* @throws RuntimeException
*/
private function newInstanceOf(string $stubClassName, string $className, string $methodName): Stub
{
try {
return (new ReflectionClass($stubClassName))->newInstanceWithoutConstructor();
} catch (Throwable $t) {
throw new RuntimeException(
sprintf(
'Return value for %s::%s() cannot be generated: %s',
$className,
$methodName,
$t->getMessage(),
),
);
}
}
/**
* @psalm-param class-string $type
* @psalm-param class-string $className
* @psalm-param non-empty-string $methodName
*
* @throws RuntimeException
*/
private function testDoubleFor(string $type, string $className, string $methodName): Stub
{
try {
return (new Generator)->testDouble($type, false, false, [], [], '', false);
} catch (Throwable $t) {
throw new RuntimeException(
sprintf(
'Return value for %s::%s() cannot be generated: %s',
$className,
$methodName,
$t->getMessage(),
),
);
}
}
/**
* @psalm-param non-empty-list<string> $types
* @psalm-param class-string $className
* @psalm-param non-empty-string $methodName
*
* @throws RuntimeException
*/
private function testDoubleForIntersectionOfInterfaces(array $types, string $className, string $methodName): Stub
{
try {
return (new Generator)->testDoubleForInterfaceIntersection($types, false);
} catch (Throwable $t) {
throw new RuntimeException(
sprintf(
'Return value for %s::%s() cannot be generated: %s',
$className,
$methodName,
$t->getMessage(),
),
);
}
}
}