12
12
use const DIRECTORY_SEPARATOR ;
13
13
use function implode ;
14
14
use function is_string ;
15
+ use function method_exists ;
15
16
use function preg_match ;
16
17
use function preg_replace ;
17
18
use function sprintf ;
19
+ use function str_replace ;
18
20
use function substr_count ;
19
21
use function trim ;
20
22
use function var_export ;
21
23
use ReflectionException ;
22
24
use ReflectionMethod ;
23
25
use ReflectionNamedType ;
26
+ use ReflectionType ;
24
27
use SebastianBergmann \Type \ObjectType ;
25
28
use SebastianBergmann \Type \Type ;
26
29
use SebastianBergmann \Type \UnknownType ;
@@ -152,7 +155,7 @@ public static function fromName(string $fullClassName, string $methodName, bool
152
155
'' ,
153
156
false ,
154
157
false ,
155
- null ,
158
+ null
156
159
);
157
160
}
158
161
@@ -208,13 +211,23 @@ public function generateCode(): string
208
211
$ deprecation = $ deprecationTemplate ->render ();
209
212
}
210
213
214
+ /**
215
+ * This is required as the version of sebastian/type used
216
+ * by PHPUnit 8.5 does now know about the mixed type.
217
+ */
218
+ $ returnTypeDeclaration = str_replace (
219
+ '?mixed ' ,
220
+ 'mixed ' ,
221
+ $ this ->returnType ->getReturnTypeDeclaration ()
222
+ );
223
+
211
224
$ template = $ this ->getTemplate ($ templateFile );
212
225
213
226
$ template ->setVar (
214
227
[
215
228
'arguments_decl ' => $ this ->argumentsForDeclaration ,
216
229
'arguments_call ' => $ this ->argumentsForCall ,
217
- 'return_declaration ' => $ this -> returnType -> getReturnTypeDeclaration () ,
230
+ 'return_declaration ' => $ returnTypeDeclaration ,
218
231
'arguments_count ' => !empty ($ this ->argumentsForCall ) ? substr_count ($ this ->argumentsForCall , ', ' ) + 1 : 0 ,
219
232
'class_name ' => $ this ->className ,
220
233
'method_name ' => $ this ->methodName ,
@@ -343,19 +356,19 @@ private static function getMethodParametersForCall(ReflectionMethod $method): st
343
356
344
357
private static function deriveReturnType (ReflectionMethod $ method ): Type
345
358
{
346
- $ returnType = $ method-> getReturnType ( );
359
+ $ returnType = self :: reflectionMethodGetReturnType ( $ method );
347
360
348
361
if ($ returnType === null ) {
349
362
return new UnknownType ;
350
363
}
351
364
352
365
// @see https://bugs.php.net/bug.php?id=70722
353
- if ($ returnType ->getName () === 'self ' ) {
366
+ if ($ returnType instanceof ReflectionNamedType && $ returnType ->getName () === 'self ' ) {
354
367
return ObjectType::fromName ($ method ->getDeclaringClass ()->getName (), $ returnType ->allowsNull ());
355
368
}
356
369
357
370
// @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/406
358
- if ($ returnType ->getName () === 'parent ' ) {
371
+ if ($ returnType instanceof ReflectionNamedType && $ returnType ->getName () === 'parent ' ) {
359
372
$ parentClass = $ method ->getDeclaringClass ()->getParentClass ();
360
373
361
374
if ($ parentClass === false ) {
@@ -374,4 +387,17 @@ private static function deriveReturnType(ReflectionMethod $method): Type
374
387
375
388
return Type::fromName ($ returnType ->getName (), $ returnType ->allowsNull ());
376
389
}
390
+
391
+ private static function reflectionMethodGetReturnType (ReflectionMethod $ method ): ?ReflectionType
392
+ {
393
+ if ($ method ->hasReturnType ()) {
394
+ return $ method ->getReturnType ();
395
+ }
396
+
397
+ if (!method_exists ($ method , 'getTentativeReturnType ' )) {
398
+ return null ;
399
+ }
400
+
401
+ return $ method ->getTentativeReturnType ();
402
+ }
377
403
}
0 commit comments