Skip to content

Commit 4bb086c

Browse files
authored
[CodeQuality] Skip already has return type on TypeWillReturnCallableArrowFunctionRector (#518)
* [CodeQuality] Skip already has return type on TypeWillReturnCallableArrowFunctionRector * fx
1 parent a5fc689 commit 4bb086c

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Source\SomeMockedClass;
7+
8+
final class SkipAlreadyReturnTyped extends TestCase
9+
{
10+
public function test($value): void
11+
{
12+
$this->createMock(SomeMockedClass::class)
13+
->method('nativeObject')
14+
->willReturnCallback(fn (object $object): \stdClass => $value);
15+
}
16+
}
17+
18+
?>

rules/CodeQuality/Rector/Class_/TypeWillReturnCallableArrowFunctionRector.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,16 @@ public function refactor(Node $node): ?Class_
239239
$hasChanged = true;
240240
}
241241

242-
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
243-
$parameterTypesAndReturnType->getReturnType(),
244-
TypeKind::RETURN
245-
);
242+
if (! $innerArg->returnType instanceof Node) {
243+
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
244+
$parameterTypesAndReturnType->getReturnType(),
245+
TypeKind::RETURN
246+
);
246247

247-
if ($returnTypeNode instanceof Node) {
248-
$innerArg->returnType = $returnTypeNode;
249-
$hasChanged = true;
248+
if ($returnTypeNode instanceof Node) {
249+
$innerArg->returnType = $returnTypeNode;
250+
$hasChanged = true;
251+
}
250252
}
251253
});
252254

rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ private function resolveParameterTypes(
6767
$parameterType = $this->resolveObjectType($parameterReflection->getNativeType());
6868

6969
if ($parameterType instanceof ObjectType && $currentClassReflection->getName() !== $parameterType->getClassReflection()->getName()) {
70-
return [];
70+
$parameterTypes[] = new MixedType();
71+
continue;
7172
}
7273

7374
$parameterTypes[] = $parameterType;

0 commit comments

Comments
 (0)