Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 9 commits into from
Aug 26, 2021
Original file line number Diff line number Diff line change
@@ -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
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

class SkipNativeInterface implements \SplObserver
{
public function update(\SplSubject $subject): void
{
}
}
43 changes: 43 additions & 0 deletions rules/DowngradePhp72/NodeAnalyzer/BuiltInMethodAnalyzer.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand Down Expand Up @@ -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
) {
}

Expand Down Expand Up @@ -140,6 +142,10 @@ public function refactor(Node $node): ?Node
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);
Expand Down