Skip to content

Commit 757f003

Browse files
Closes #4694
1 parent f60f6d1 commit 757f003

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

ChangeLog-8.5.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ All notable changes of the PHPUnit 8.5 release series are documented in this fil
1313
* [#4663](https://github.com/sebastianbergmann/phpunit/issues/4663): `TestCase::expectError()` works on PHP 7.3, but not on PHP >= 7.4
1414
* [#4678](https://github.com/sebastianbergmann/phpunit/pull/4678): Stubbed methods with `iterable` return types should return empty array by default
1515
* [#4692](https://github.com/sebastianbergmann/phpunit/issues/4692): Annotations in single-line doc-comments are not handled correctly
16+
* [#4694](https://github.com/sebastianbergmann/phpunit/issues/4694): `TestCase::getMockFromWsdl()` does not work with PHP 8.1-dev
1617

1718
## [8.5.15] - 2021-03-17
1819

src/Framework/MockObject/MockMethod.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212
use const DIRECTORY_SEPARATOR;
1313
use function implode;
1414
use function is_string;
15+
use function method_exists;
1516
use function preg_match;
1617
use function preg_replace;
1718
use function sprintf;
19+
use function str_replace;
1820
use function substr_count;
1921
use function trim;
2022
use function var_export;
2123
use ReflectionException;
2224
use ReflectionMethod;
2325
use ReflectionNamedType;
26+
use ReflectionType;
2427
use SebastianBergmann\Type\ObjectType;
2528
use SebastianBergmann\Type\Type;
2629
use SebastianBergmann\Type\UnknownType;
@@ -152,7 +155,7 @@ public static function fromName(string $fullClassName, string $methodName, bool
152155
'',
153156
false,
154157
false,
155-
null,
158+
null
156159
);
157160
}
158161

@@ -208,13 +211,23 @@ public function generateCode(): string
208211
$deprecation = $deprecationTemplate->render();
209212
}
210213

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+
211224
$template = $this->getTemplate($templateFile);
212225

213226
$template->setVar(
214227
[
215228
'arguments_decl' => $this->argumentsForDeclaration,
216229
'arguments_call' => $this->argumentsForCall,
217-
'return_declaration' => $this->returnType->getReturnTypeDeclaration(),
230+
'return_declaration' => $returnTypeDeclaration,
218231
'arguments_count' => !empty($this->argumentsForCall) ? substr_count($this->argumentsForCall, ',') + 1 : 0,
219232
'class_name' => $this->className,
220233
'method_name' => $this->methodName,
@@ -343,19 +356,19 @@ private static function getMethodParametersForCall(ReflectionMethod $method): st
343356

344357
private static function deriveReturnType(ReflectionMethod $method): Type
345358
{
346-
$returnType = $method->getReturnType();
359+
$returnType = self::reflectionMethodGetReturnType($method);
347360

348361
if ($returnType === null) {
349362
return new UnknownType;
350363
}
351364

352365
// @see https://bugs.php.net/bug.php?id=70722
353-
if ($returnType->getName() === 'self') {
366+
if ($returnType instanceof ReflectionNamedType && $returnType->getName() === 'self') {
354367
return ObjectType::fromName($method->getDeclaringClass()->getName(), $returnType->allowsNull());
355368
}
356369

357370
// @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/406
358-
if ($returnType->getName() === 'parent') {
371+
if ($returnType instanceof ReflectionNamedType && $returnType->getName() === 'parent') {
359372
$parentClass = $method->getDeclaringClass()->getParentClass();
360373

361374
if ($parentClass === false) {
@@ -374,4 +387,17 @@ private static function deriveReturnType(ReflectionMethod $method): Type
374387

375388
return Type::fromName($returnType->getName(), $returnType->allowsNull());
376389
}
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+
}
377403
}

0 commit comments

Comments
 (0)