Skip to content

Commit

Permalink
[DowngradePhp72] Skip implements built in interface method on Downgra…
Browse files Browse the repository at this point in the history
…deParameterTypeWideningRector (#766)

Co-authored-by: Bobab12 <bobab12@users.noreply.github.com>
  • Loading branch information
samsonasik and bobab12 committed Aug 26, 2021
1 parent 5b5b94b commit 47ad54d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
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

0 comments on commit 47ad54d

Please sign in to comment.