mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-20 23:18:20 +01:00
remove duplicated method
This commit is contained in:
parent
dfb3cc7c6b
commit
f97bd059cb
@ -62,12 +62,7 @@ abstract class AbstractNodeVendorLockResolver
|
||||
$interfaceNames = $this->classManipulator->getClassLikeNodeParentInterfaceNames($classLike);
|
||||
|
||||
foreach ($interfaceNames as $interfaceName) {
|
||||
if (! interface_exists($interfaceName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$interfaceMethods = get_class_methods($interfaceName);
|
||||
if (! in_array($methodName, $interfaceMethods, true)) {
|
||||
if (! $this->hasInterfaceMethod($methodName, $interfaceName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -76,4 +71,15 @@ abstract class AbstractNodeVendorLockResolver
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function hasInterfaceMethod(string $methodName, string $interfaceName): bool
|
||||
{
|
||||
if (! interface_exists($interfaceName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$interfaceMethods = get_class_methods($interfaceName);
|
||||
|
||||
return in_array($methodName, $interfaceMethods, true);
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ namespace Rector\VendorLocker\NodeVendorLocker;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Interface_;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
final class ClassMethodParamVendorLockResolver extends AbstractNodeVendorLockResolver
|
||||
@ -24,10 +23,8 @@ final class ClassMethodParamVendorLockResolver extends AbstractNodeVendorLockRes
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var string $methodName */
|
||||
$methodName = $this->nodeNameResolver->getName($classMethod);
|
||||
if (! is_string($methodName)) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
|
||||
// @todo extract to some "inherited parent method" service
|
||||
/** @var string|null $parentClassName */
|
||||
@ -45,8 +42,7 @@ final class ClassMethodParamVendorLockResolver extends AbstractNodeVendorLockRes
|
||||
return false;
|
||||
}
|
||||
|
||||
$interfaceNames = $this->classManipulator->getClassLikeNodeParentInterfaceNames($classNode);
|
||||
return $this->isInterfaceParamVendorLockin($interfaceNames, $methodName);
|
||||
return $this->isMethodVendorLockedByInterface($classNode, $methodName);
|
||||
}
|
||||
|
||||
private function isParentClassVendorLocking(int $paramPosition, string $parentClassName, string $methodName): ?bool
|
||||
@ -74,24 +70,4 @@ final class ClassMethodParamVendorLockResolver extends AbstractNodeVendorLockRes
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function isInterfaceParamVendorLockin(array $interfaceNames, string $methodName): bool
|
||||
{
|
||||
foreach ($interfaceNames as $interfaceName) {
|
||||
$interface = $this->parsedNodeCollector->findInterface($interfaceName);
|
||||
// parent class method in local scope → it's ok
|
||||
// @todo validate type is conflicting
|
||||
if ($interface !== null && $interface->getMethod($methodName) !== null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (method_exists($interfaceName, $methodName)) {
|
||||
// parent class method in external scope → it's not ok
|
||||
// @todo validate type is conflicting
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ namespace Rector\VendorLocker\NodeVendorLocker;
|
||||
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use PhpParser\Node\Stmt\Interface_;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use ReflectionClass;
|
||||
|
||||
@ -22,12 +22,10 @@ final class ClassMethodVendorLockResolver extends AbstractNodeVendorLockResolver
|
||||
*/
|
||||
public function isRemovalVendorLocked(ClassMethod $classMethod): bool
|
||||
{
|
||||
/** @var string $classMethodName */
|
||||
$classMethodName = $this->nodeNameResolver->getName($classMethod);
|
||||
if (! is_string($classMethodName)) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
|
||||
/** @var Class_|null $class */
|
||||
/** @var Class_|Interface_|null $class */
|
||||
$class = $classMethod->getAttribute(AttributeKey::CLASS_NODE);
|
||||
if ($class === null) {
|
||||
return false;
|
||||
|
@ -60,14 +60,6 @@ final class VendorLockResolver
|
||||
return $this->propertyVendorLockResolver->isVendorLocked($property);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for:
|
||||
* - interface required methods
|
||||
* - abstract classes reqired method
|
||||
*
|
||||
* Prevent:
|
||||
* - removing class methods, that breaks the code
|
||||
*/
|
||||
public function isClassMethodRemovalVendorLocked(ClassMethod $classMethod): bool
|
||||
{
|
||||
return $this->classMethodVendorLockResolver->isRemovalVendorLocked($classMethod);
|
||||
|
Loading…
x
Reference in New Issue
Block a user