From a920e9e05c604045a690f35c8f2c358297f07eca Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 2 Mar 2021 13:15:27 +0100 Subject: [PATCH] [DeadCode] Do not remove class method if required by parent ocntract --- .../RemoveUnusedPublicMethodRector.php | 17 +++++++++++++++-- .../Fixture/skip_required_by_contract.php.inc | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 rules/dead-code/tests/Rector/ClassMethod/RemoveUnusedPublicMethodRector/Fixture/skip_required_by_contract.php.inc diff --git a/rules/dead-code/src/Rector/ClassMethod/RemoveUnusedPublicMethodRector.php b/rules/dead-code/src/Rector/ClassMethod/RemoveUnusedPublicMethodRector.php index a5223b36e0d0..fd4aa329d218 100644 --- a/rules/dead-code/src/Rector/ClassMethod/RemoveUnusedPublicMethodRector.php +++ b/rules/dead-code/src/Rector/ClassMethod/RemoveUnusedPublicMethodRector.php @@ -14,6 +14,7 @@ use Rector\DeadCode\NodeAnalyzer\DataProviderMethodNamesResolver; use Rector\NodeCollector\ValueObject\ArrayCallable; use Rector\NodeTypeResolver\Node\AttributeKey; +use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnVendorLockResolver; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -32,9 +33,17 @@ final class RemoveUnusedPublicMethodRector extends AbstractRector implements Zer */ private $calls = []; - public function __construct(DataProviderMethodNamesResolver $dataProviderMethodNamesResolver) - { + /** + * @var ClassMethodReturnVendorLockResolver + */ + private $classMethodReturnVendorLockResolver; + + public function __construct( + DataProviderMethodNamesResolver $dataProviderMethodNamesResolver, + ClassMethodReturnVendorLockResolver $classMethodReturnVendorLockResolver + ) { $this->dataProviderMethodNamesResolver = $dataProviderMethodNamesResolver; + $this->classMethodReturnVendorLockResolver = $classMethodReturnVendorLockResolver; } public function getRuleDefinition(): RuleDefinition @@ -128,6 +137,10 @@ private function shouldSkip(ClassMethod $classMethod): bool return true; } + if ($this->classMethodReturnVendorLockResolver->isVendorLocked($classMethod)) { + return true; + } + if ($classMethod->isMagic()) { return true; } diff --git a/rules/dead-code/tests/Rector/ClassMethod/RemoveUnusedPublicMethodRector/Fixture/skip_required_by_contract.php.inc b/rules/dead-code/tests/Rector/ClassMethod/RemoveUnusedPublicMethodRector/Fixture/skip_required_by_contract.php.inc new file mode 100644 index 000000000000..f594ec482b48 --- /dev/null +++ b/rules/dead-code/tests/Rector/ClassMethod/RemoveUnusedPublicMethodRector/Fixture/skip_required_by_contract.php.inc @@ -0,0 +1,17 @@ +