Skip to content

Commit

Permalink
filter
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Oct 8, 2024
1 parent 645aaa9 commit 66dce9a
Showing 1 changed file with 6 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\DeadCode\NodeAnalyzer\IsClassMethodUsedAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\Reflection\ReflectionResolver;
Expand Down Expand Up @@ -83,7 +84,9 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
}

$filter = static fn (ClassMethod $classMethod): bool => $classMethod->isPrivate();
if (array_filter($classMethods, $filter) === []) {
$privateMethods = array_filter($classMethods, $filter);

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

Expand All @@ -94,11 +97,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
$hasChanged = false;
$classReflection = $this->reflectionResolver->resolveClassReflection($node);

foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof ClassMethod) {
continue;
}

foreach ($privateMethods as $stmt) {
if ($this->shouldSkip($stmt, $classReflection)) {
continue;
}
Expand All @@ -107,7 +106,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
continue;
}

unset($node->stmts[$key]);
unset($node->stmts[$stmt->getAttribute(AttributeKey::STMT_KEY)]);
$hasChanged = true;
}

Expand Down Expand Up @@ -137,10 +136,6 @@ private function shouldSkip(ClassMethod $classMethod, ?ClassReflection $classRef
return true;
}

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

// skip magic methods - @see https://www.php.net/manual/en/language.oop5.magic.php
if ($classMethod->isMagic()) {
return true;
Expand Down

0 comments on commit 66dce9a

Please sign in to comment.