Skip to content

Commit d462bab

Browse files
authored
[CodeQuality] Handle crash on native "object" type on TypeWillReturnCallableArrowFunctionRector (#516)
* [CodeQuality] Handle crash on native "object" type on TypeWillReturnCallableArrowFunctionRector * [CodeQuality] Handle crash on native "object" type on TypeWillReturnCallableArrowFunctionRector * Fix
1 parent 5506285 commit d462bab

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ parameters:
5959
- '#::provideMinPhpVersion\(\) never returns \d+ so it can be removed from the return type#'
6060

6161
-
62-
message: '#Call to an undefined method PHPStan\\Type\\Type\:\:getClassReflection\(\)#'
62+
message: '#Cannot call method getName\(\) on PHPStan\\Reflection\\ClassReflection\|null#'
6363
path: rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 FillKnownParamTypeObject extends TestCase
9+
{
10+
public function test($value): void
11+
{
12+
$this->createMock(SomeMockedClass::class)
13+
->method('nativeObject')
14+
->willReturnCallback(fn (object $object) => $value);
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Fixture;
23+
24+
use PHPUnit\Framework\TestCase;
25+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Source\SomeMockedClass;
26+
27+
final class FillKnownParamTypeObject extends TestCase
28+
{
29+
public function test($value): void
30+
{
31+
$this->createMock(SomeMockedClass::class)
32+
->method('nativeObject')
33+
->willReturnCallback(fn (object $object): object => $value);
34+
}
35+
}
36+
37+
?>

rules-tests/CodeQuality/Rector/Class_/TypeWillReturnCallableArrowFunctionRector/Source/SomeMockedClass.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ public function someMethod(string $name): int
99
{
1010
return 100;
1111
}
12+
13+
public function nativeObject(object $object): object
14+
{
15+
return $object;
16+
}
1217
}

rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Type\IntersectionType;
1111
use PHPStan\Type\MixedType;
1212
use PHPStan\Type\ObjectType;
13+
use PHPStan\Type\StaticType;
1314
use PHPStan\Type\Type;
1415
use Rector\Enum\ClassName;
1516
use Rector\PHPUnit\CodeQuality\ValueObject\ParamTypesAndReturnType;
@@ -63,9 +64,9 @@ private function resolveParameterTypes(
6364

6465
$parameterTypes = [];
6566
foreach ($extendedParametersAcceptor->getParameters() as $parameterReflection) {
66-
$parameterType = $parameterReflection->getNativeType();
67+
$parameterType = $this->resolveObjectType($parameterReflection->getNativeType());
6768

68-
if ($parameterType->isObject()->yes() && $currentClassReflection->getName() !== $parameterType->getClassReflection()->getName()) {
69+
if ($parameterType instanceof ObjectType && $currentClassReflection->getName() !== $parameterType->getClassReflection()->getName()) {
6970
return [];
7071
}
7172

@@ -75,6 +76,19 @@ private function resolveParameterTypes(
7576
return $parameterTypes;
7677
}
7778

79+
private function resolveObjectType(Type $type): ObjectType|Type
80+
{
81+
if ($type instanceof ObjectType) {
82+
return $type;
83+
}
84+
85+
if ($type instanceof StaticType) {
86+
return $type->getStaticObjectType();
87+
}
88+
89+
return $type;
90+
}
91+
7892
private function resolveReturnType(
7993
ExtendedMethodReflection $extendedMethodReflection,
8094
ClassReflection $currentClassReflection
@@ -83,9 +97,9 @@ private function resolveReturnType(
8397
$extendedMethodReflection->getVariants()
8498
);
8599

86-
$returnType = $extendedParametersAcceptor->getNativeReturnType();
100+
$returnType = $this->resolveObjectType($extendedParametersAcceptor->getNativeReturnType());
87101

88-
if ($returnType->isObject()->yes() && $currentClassReflection->getName() !== $returnType->getClassReflection()->getName()) {
102+
if ($returnType instanceof ObjectType && $currentClassReflection->getName() !== $returnType->getClassReflection()->getName()) {
89103
return new MixedType();
90104
}
91105

0 commit comments

Comments
 (0)