mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-29 14:13:30 +00:00
[DowngradePhp72] Skip implements built in interface method on DowngradeParameterTypeWideningRector (#766)
Co-authored-by: Bobab12 <bobab12@users.noreply.github.com>
This commit is contained in:
parent
5b5b94b002
commit
47ad54dfc8
|
@ -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
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
|
||||
|
||||
class SkipNativeInterface implements \SplObserver
|
||||
{
|
||||
public function update(\SplSubject $subject): void
|
||||
{
|
||||
}
|
||||
}
|
43
rules/DowngradePhp72/NodeAnalyzer/BuiltInMethodAnalyzer.php
Normal file
43
rules/DowngradePhp72/NodeAnalyzer/BuiltInMethodAnalyzer.php
Normal 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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user