Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ parameters:
- '#::provideMinPhpVersion\(\) never returns \d+ so it can be removed from the return type#'

-
message: '#Call to an undefined method PHPStan\\Type\\Type\:\:getClassReflection\(\)#'
message: '#Cannot call method getName\(\) on PHPStan\\Reflection\\ClassReflection\|null#'
path: rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Source\SomeMockedClass;

final class FillKnownParamTypeObject extends TestCase
{
public function test($value): void
{
$this->createMock(SomeMockedClass::class)
->method('nativeObject')
->willReturnCallback(fn (object $object) => $value);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Source\SomeMockedClass;

final class FillKnownParamTypeObject extends TestCase
{
public function test($value): void
{
$this->createMock(SomeMockedClass::class)
->method('nativeObject')
->willReturnCallback(fn (object $object): object => $value);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ public function someMethod(string $name): int
{
return 100;
}

public function nativeObject(object $object): object
{
return $object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use PHPStan\Type\Type;
use Rector\Enum\ClassName;
use Rector\PHPUnit\CodeQuality\ValueObject\ParamTypesAndReturnType;
Expand Down Expand Up @@ -63,9 +64,9 @@ private function resolveParameterTypes(

$parameterTypes = [];
foreach ($extendedParametersAcceptor->getParameters() as $parameterReflection) {
$parameterType = $parameterReflection->getNativeType();
$parameterType = $this->resolveObjectType($parameterReflection->getNativeType());

if ($parameterType->isObject()->yes() && $currentClassReflection->getName() !== $parameterType->getClassReflection()->getName()) {
if ($parameterType instanceof ObjectType && $currentClassReflection->getName() !== $parameterType->getClassReflection()->getName()) {
return [];
}

Expand All @@ -75,6 +76,19 @@ private function resolveParameterTypes(
return $parameterTypes;
}

private function resolveObjectType(Type $type): ObjectType|Type
{
if ($type instanceof ObjectType) {
return $type;
}

if ($type instanceof StaticType) {
return $type->getStaticObjectType();
}

return $type;
}

private function resolveReturnType(
ExtendedMethodReflection $extendedMethodReflection,
ClassReflection $currentClassReflection
Expand All @@ -83,9 +97,9 @@ private function resolveReturnType(
$extendedMethodReflection->getVariants()
);

$returnType = $extendedParametersAcceptor->getNativeReturnType();
$returnType = $this->resolveObjectType($extendedParametersAcceptor->getNativeReturnType());

if ($returnType->isObject()->yes() && $currentClassReflection->getName() !== $returnType->getClassReflection()->getName()) {
if ($returnType instanceof ObjectType && $currentClassReflection->getName() !== $returnType->getClassReflection()->getName()) {
return new MixedType();
}

Expand Down