Skip to content

Commit

Permalink
Fix PrivatesAccessor: use selectFromArgs() instead (#3923)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed May 22, 2023
1 parent 6c3f2cd commit c5196d4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions rules/CodingStyle/NodeAnalyzer/SpreadVariablesCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\ParametersAcceptorSelector;
use Rector\NodeTypeResolver\Node\AttributeKey;

Expand All @@ -16,11 +17,10 @@ final class SpreadVariablesCollector
/**
* @return array<int, ParameterReflection>
*/
public function resolveFromMethodReflection(MethodReflection $methodReflection): array
public function resolveFromParametersAcceptor(ParametersAcceptor $parametersAcceptor): array
{
$spreadParameterReflections = [];

$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
foreach ($parametersAcceptor->getParameters() as $key => $parameterReflection) {
if (! $parameterReflection->isVariadic()) {
continue;
Expand Down
16 changes: 10 additions & 6 deletions rules/CodingStyle/Rector/ClassMethod/UnSpreadOperatorRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use Rector\CodingStyle\NodeAnalyzer\SpreadVariablesCollector;
use Rector\CodingStyle\Reflection\VendorLocationDetector;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\FamilyTree\NodeAnalyzer\ClassChildAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -23,7 +26,7 @@
/**
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\UnSpreadOperatorRector\UnSpreadOperatorRectorTest
*/
final class UnSpreadOperatorRector extends AbstractRector
final class UnSpreadOperatorRector extends AbstractScopeAwareRector
{
public function __construct(
private readonly SpreadVariablesCollector $spreadVariablesCollector,
Expand Down Expand Up @@ -79,13 +82,13 @@ public function getNodeTypes(): array
/**
* @param ClassMethod|MethodCall $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if ($node instanceof ClassMethod) {
return $this->refactorClassMethod($node);
}

return $this->refactorMethodCall($node);
return $this->refactorMethodCall($node, $scope);
}

private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod
Expand Down Expand Up @@ -118,7 +121,7 @@ private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod
return $classMethod;
}

private function refactorMethodCall(MethodCall $methodCall): ?MethodCall
private function refactorMethodCall(MethodCall $methodCall, Scope $scope): ?MethodCall
{
$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromMethodCall($methodCall);
if (! $methodReflection instanceof MethodReflection) {
Expand All @@ -130,8 +133,9 @@ private function refactorMethodCall(MethodCall $methodCall): ?MethodCall
return null;
}

$spreadParameterReflections = $this->spreadVariablesCollector->resolveFromMethodReflection(
$methodReflection
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants());
$spreadParameterReflections = $this->spreadVariablesCollector->resolveFromParametersAcceptor(
$parametersAcceptor
);

if ($spreadParameterReflections === []) {
Expand Down

0 comments on commit c5196d4

Please sign in to comment.