Skip to content

Commit

Permalink
Drop AttributeKey::SCOPE in ArrayCallableMethodMatcher (#3835)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed May 15, 2023
1 parent 05c4367 commit 95b1789
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
15 changes: 5 additions & 10 deletions packages/NodeCollector/NodeAnalyzer/ArrayCallableMethodMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(
* @see https://github.com/rectorphp/rector-src/pull/908
* @see https://github.com/rectorphp/rector-src/pull/909
*/
public function match(Array_ $array): null | ArrayCallableDynamicMethod | ArrayCallable
public function match(Array_ $array, Scope $scope): null | ArrayCallableDynamicMethod | ArrayCallable
{
if (count($array->items) !== 2) {
return null;
Expand All @@ -65,7 +65,7 @@ public function match(Array_ $array): null | ArrayCallableDynamicMethod | ArrayC
// $this, self, static, FQN
$firstItemValue = $items[0]->value;

$callerType = $this->resolveCallerType($firstItemValue);
$callerType = $this->resolveCallerType($firstItemValue, $scope);
if (! $callerType instanceof TypeWithClassName) {
return null;
}
Expand Down Expand Up @@ -145,7 +145,7 @@ private function isCallbackAtFunctionNames(Array_ $array, array $functionNames):
return $this->nodeNameResolver->isNames($parentParentNode, $functionNames);
}

private function resolveClassConstFetchType(ClassConstFetch $classConstFetch): MixedType | ObjectType
private function resolveClassConstFetchType(ClassConstFetch $classConstFetch, Scope $scope): MixedType | ObjectType
{
$classConstantReference = $this->valueResolver->getValue($classConstFetch);

Expand All @@ -167,11 +167,6 @@ private function resolveClassConstFetchType(ClassConstFetch $classConstFetch): M
return new MixedType();
}

$scope = $classConstFetch->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return new MixedType();
}

$classReflection = $this->reflectionProvider->getClass($classConstantReference);
$hasConstruct = $classReflection->hasMethod(MethodName::CONSTRUCT);

Expand All @@ -193,11 +188,11 @@ private function resolveClassConstFetchType(ClassConstFetch $classConstFetch): M
return new ObjectType($classConstantReference, null, $classReflection);
}

private function resolveCallerType(Expr $expr): Type
private function resolveCallerType(Expr $expr, Scope $scope): Type
{
if ($expr instanceof ClassConstFetch) {
// static ::class reference?
$callerType = $this->resolveClassConstFetchType($expr);
$callerType = $this->resolveClassConstFetchType($expr, $scope);
} else {
$callerType = $this->nodeTypeResolver->getType($expr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function getNodeTypes(): array
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
$arrayCallable = $this->arrayCallableMethodMatcher->match($node);
$arrayCallable = $this->arrayCallableMethodMatcher->match($node, $scope);
if (! $arrayCallable instanceof ArrayCallable) {
return null;
}
Expand Down
9 changes: 5 additions & 4 deletions rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
Expand All @@ -34,7 +35,7 @@ public function __construct(
) {
}

public function isClassMethodUsed(ClassMethod $classMethod): bool
public function isClassMethodUsed(ClassMethod $classMethod, Scope $scope): bool
{
$class = $this->betterNodeFinder->findParentType($classMethod, Class_::class);
if (! $class instanceof Class_) {
Expand All @@ -54,7 +55,7 @@ public function isClassMethodUsed(ClassMethod $classMethod): bool
}

// 3. magic array calls!
if ($this->isClassMethodCalledInLocalArrayCall($class, $classMethod)) {
if ($this->isClassMethodCalledInLocalArrayCall($class, $classMethod, $scope)) {
return true;
}

Expand Down Expand Up @@ -108,7 +109,7 @@ private function isInArrayMap(Class_ $class, Array_ $array): bool
return $class->getMethod($value) instanceof ClassMethod;
}

private function isClassMethodCalledInLocalArrayCall(Class_ $class, ClassMethod $classMethod): bool
private function isClassMethodCalledInLocalArrayCall(Class_ $class, ClassMethod $classMethod, Scope $scope): bool
{
/** @var Array_[] $arrays */
$arrays = $this->betterNodeFinder->findInstanceOf($class, Array_::class);
Expand All @@ -118,7 +119,7 @@ private function isClassMethodCalledInLocalArrayCall(Class_ $class, ClassMethod
return true;
}

$arrayCallable = $this->arrayCallableMethodMatcher->match($array);
$arrayCallable = $this->arrayCallableMethodMatcher->match($array, $scope);
if ($arrayCallable instanceof ArrayCallableDynamicMethod) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\MethodName;
use Rector\DeadCode\NodeAnalyzer\IsClassMethodUsedAnalyzer;
Expand All @@ -20,7 +22,7 @@
/**
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector\RemoveUnusedPrivateMethodRectorTest
*/
final class RemoveUnusedPrivateMethodRector extends AbstractRector
final class RemoveUnusedPrivateMethodRector extends AbstractScopeAwareRector
{
public function __construct(
private readonly IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer,
Expand Down Expand Up @@ -71,13 +73,13 @@ public function getNodeTypes(): array
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}

if ($this->isClassMethodUsedAnalyzer->isClassMethodUsed($node)) {
if ($this->isClassMethodUsedAnalyzer->isClassMethodUsed($node, $scope)) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion rules/Php81/Rector/Array_/FirstClassCallableRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function getNodeTypes(): array
*/
public function refactorWithScope(Node $node, Scope $scope)
{
$arrayCallable = $this->arrayCallableMethodMatcher->match($node);
$arrayCallable = $this->arrayCallableMethodMatcher->match($node, $scope);
if (! $arrayCallable instanceof ArrayCallable) {
return null;
}
Expand Down

0 comments on commit 95b1789

Please sign in to comment.