Skip to content

Commit

Permalink
Merge pull request #2937 from rectorphp/skip-public-abstract
Browse files Browse the repository at this point in the history
skip open source class in remove unused param in open-source
  • Loading branch information
TomasVotruba authored Feb 26, 2020
2 parents c221392 + 1a4bb38 commit 37cd497
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public function refactor(Node $node): ?Node
);

$nodesToRemove = $this->resolveNodesToRemove($assignedVariableNames, $nodesByTypeAndPosition);
if ($nodesToRemove === []) {
return null;
}

$this->removeNodes($nodesToRemove);

return $node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @see https://www.php.net/manual/en/function.compact.php
*
* @see \Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedParameterRector\RemoveUnusedParameterRectorTest
* @see \Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedParameterRector\OpenSourceRectorTest
*/
final class RemoveUnusedParameterRector extends AbstractRector
{
Expand Down Expand Up @@ -127,6 +128,10 @@ public function refactor(Node $node): ?Node
$childrenOfClass = $this->classLikeParsedNodesFinder->findChildrenOfClass($className);
$unusedParameters = $this->getUnusedParameters($node, $methodName, $childrenOfClass);

if ($unusedParameters === []) {
return null;
}

foreach ($childrenOfClass as $childClassNode) {
$methodOfChild = $childClassNode->getMethod($methodName);
if ($methodOfChild !== null) {
Expand Down Expand Up @@ -211,19 +216,35 @@ private function shouldSkip(ClassMethod $classMethod): bool
return true;
}

// skip as possible contract for 3rd party
$projetType = $this->parameterProvider->provideParameter(Option::PROJECT_TYPE);
if ($classMethod->isAbstract() && $projetType === Option::PROJECT_TYPE_OPEN_SOURCE) {
return true;
}

$class = $classMethod->getAttribute(AttributeKey::CLASS_NODE);

// skip interfaces and traits
if (! $class instanceof Class_) {
return true;
}

if ($this->shouldSkipOpenSourceAbstract($classMethod, $class)) {
return true;
}

return $this->isAnonymousClass($class);
}

private function shouldSkipOpenSourceAbstract(ClassMethod $classMethod, Class_ $class): bool
{
// skip as possible contract for 3rd party
$projectType = $this->parameterProvider->provideParameter(Option::PROJECT_TYPE);
if ($projectType !== Option::PROJECT_TYPE_OPEN_SOURCE) {
return false;
}

if ($classMethod->isAbstract()) {
return true;
}

if (! $class->isAbstract()) {
return false;
}

return $classMethod->isPublic();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedParameterRector\FixtureOpenSource;

abstract class SkipAbstractClass
{
// free abstract method
public function foo(string $foo, string $bar)
{

}
}

class UsingAbstractClass extends SkipAbstractClass
{
}

0 comments on commit 37cd497

Please sign in to comment.