[DowngradePhp72] Skip implements built in interface method on DowngradeParameterTypeWideningRector (#766)

Co-authored-by: Bobab12 <bobab12@users.noreply.github.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-08-26 14:51:06 +07:00 committed by GitHub
parent 5b5b94b002
commit 47ad54dfc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 102 additions and 1 deletions

View File

@ -0,0 +1,42 @@
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
abstract class ParentClass implements \SplObserver
{
public function update(\SplSubject $subject): void
{
}
}
class IndirectImplementsNativeInterface extends ParentClass
{
public function update($subject): void
{
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
abstract class ParentClass implements \SplObserver
{
/**
* @param \SplSubject $subject
*/
public function update($subject): void
{
}
}
class IndirectImplementsNativeInterface extends ParentClass
{
public function update($subject): void
{
}
}
?>

View File

@ -0,0 +1,10 @@
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
class SkipNativeInterface implements \SplObserver
{
public function update(\SplSubject $subject): void
{
}
}

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp72\NodeAnalyzer;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\ClassReflection;
use Rector\FamilyTree\NodeAnalyzer\ClassChildAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
final class BuiltInMethodAnalyzer
{
public function __construct(private NodeNameResolver $nodeNameResolver, private ClassChildAnalyzer $classChildAnalyzer)
{
}
public function isImplementsBuiltInInterface(ClassReflection $classReflection, ClassMethod $classMethod): bool
{
if (! $classReflection->isClass()) {
return false;
}
$methodName = $this->nodeNameResolver->getName($classMethod);
if ($this->classChildAnalyzer->hasChildClassMethod($classReflection, $methodName)) {
return false;
}
foreach ($classReflection->getInterfaces() as $interfaceReflection) {
if (! $interfaceReflection->isBuiltIn()) {
continue;
}
if (! $interfaceReflection->hasMethod($methodName)) {
continue;
}
return true;
}
return false;
}
}

View File

@ -11,6 +11,7 @@ use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\DowngradePhp72\NodeAnalyzer\BuiltInMethodAnalyzer;
use Rector\DowngradePhp72\PhpDoc\NativeParamToPhpDocDecorator;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\TypeDeclaration\NodeAnalyzer\AutowiredClassMethodOrPropertyAnalyzer;
@ -49,7 +50,8 @@ final class DowngradeParameterTypeWideningRector extends AbstractRector implemen
public function __construct(
private NativeParamToPhpDocDecorator $nativeParamToPhpDocDecorator,
private ReflectionProvider $reflectionProvider,
private AutowiredClassMethodOrPropertyAnalyzer $autowiredClassMethodOrPropertyAnalyzer
private AutowiredClassMethodOrPropertyAnalyzer $autowiredClassMethodOrPropertyAnalyzer,
private BuiltInMethodAnalyzer $builtInMethodAnalyzer
) {
}
@ -140,6 +142,10 @@ CODE_SAMPLE
return null;
}
if ($this->builtInMethodAnalyzer->isImplementsBuiltInInterface($classReflection, $node)) {
return null;
}
// Downgrade every scalar parameter, just to be sure
foreach (array_keys($node->params) as $paramPosition) {
$this->removeParamTypeFromMethod($node, $paramPosition);