[TypeDeclaration] Fix param type change in class method (#577)

This commit is contained in:
Tomas Votruba 2021-08-02 21:12:49 +02:00 committed by GitHub
parent b3480900f3
commit d936944aed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 1 deletions

View File

@ -0,0 +1,19 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\Fixture;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\Source\GoOneWayInterface;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\Source\SomeTypedService;
final class SkipParentGuardedMethod implements GoOneWayInterface
{
public function __construct(
private SomeTypedService $someTypedService
) {
}
public function go($value)
{
$this->someTypedService->run($value);
}
}

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\Source;
interface GoOneWayInterface
{
public function go($value);
}

View File

@ -72,6 +72,11 @@ final class ParentClassMethodTypeOverrideGuard
return ! str_contains($fileName, '/vendor/');
}
public function hasParentClassMethod(ClassMethod $classMethod): bool
{
return $this->getParentClassMethod($classMethod) instanceof MethodReflection;
}
private function getParentClassMethod(ClassMethod $classMethod): ?MethodReflection
{
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);

View File

@ -19,6 +19,7 @@ use PhpParser\Node\UnionType;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Rector\AbstractRector;
use Rector\Defluent\ConflictGuard\ParentClassMethodTypeOverrideGuard;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\TypeDeclaration\NodeAnalyzer\CallerParamMatcher;
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
@ -32,7 +33,8 @@ final class ParamTypeByMethodCallTypeRector extends AbstractRector
{
public function __construct(
private CallerParamMatcher $callerParamMatcher,
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard
) {
}
@ -142,6 +144,10 @@ CODE_SAMPLE
return true;
}
if ($this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod)) {
return true;
}
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return true;