Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ services:
- '../src/DependencyInjection/Loader/*'
- '../src/HttpKernel/*'
- '../src/ValueObject/*'
- '../src/PHPStan/Reflection/Php/*'

# extra services
Rector\Symfony\Rector\Form\Helper\FormTypeStringToTypeProvider: null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
use PHPStan\Reflection\ParameterReflection;
use Rector\CodingStyle\Naming\ClassNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php70\Reflection\CallReflection;
use Rector\Php70\ValueObject\VariableAssignPair;
use Rector\PHPStan\Reflection\CallReflectionResolver;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -44,18 +44,18 @@ final class NonVariableToVariableOnFunctionCallRector extends AbstractRector
private const DEFAULT_VARIABLE_NAME = 'tmp';

/**
* @var CallReflection
* @var CallReflectionResolver
*/
private $callReflection;
private $callReflectionResolver;

/**
* @var ClassNaming
*/
private $classNaming;

public function __construct(CallReflection $callReflection, ClassNaming $classNaming)
public function __construct(CallReflectionResolver $callReflectionResolver, ClassNaming $classNaming)
{
$this->callReflection = $callReflection;
$this->callReflectionResolver = $callReflectionResolver;
$this->classNaming = $classNaming;
}

Expand Down Expand Up @@ -85,7 +85,7 @@ public function refactor(Node $node): ?Node
return null;
}

$arguments = $this->getNonVariableArguments($node, $scope);
$arguments = $this->getNonVariableArguments($node);

if ($arguments === []) {
return null;
Expand All @@ -111,12 +111,21 @@ public function refactor(Node $node): ?Node
*
* @return array<int, Expr>
*/
private function getNonVariableArguments(Node $node, Scope $scope): array
private function getNonVariableArguments(Node $node): array
{
$arguments = [];

$parametersAcceptor = $this->callReflectionResolver->resolveParametersAcceptor(
$this->callReflectionResolver->resolveCall($node),
$node
);

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

/** @var ParameterReflection $parameter */
foreach ($this->callReflection->getParameterReflections($node, $scope) as $key => $parameter) {
foreach ($parametersAcceptor->getParameters() as $key => $parameter) {
// omitted optional parameter
if (! isset($node->args[$key])) {
continue;
Expand Down
110 changes: 0 additions & 110 deletions packages/Php70/src/Reflection/CallReflection.php

This file was deleted.

103 changes: 20 additions & 83 deletions packages/Php71/src/Rector/FuncCall/RemoveExtraParametersRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@

namespace Rector\Php71\Rector\FuncCall;

use function count;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use Rector\PhpParser\Node\Manipulator\CallManipulator;
use PHPStan\Reflection\ParametersAcceptor;
use Rector\PHPStan\Reflection\CallReflectionResolver;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
use ReflectionFunction;
use ReflectionFunctionAbstract;
use ReflectionMethod;

/**
* @see https://www.reddit.com/r/PHP/comments/a1ie7g/is_there_a_linter_for_argumentcounterror_for_php/
Expand All @@ -30,13 +25,13 @@
final class RemoveExtraParametersRector extends AbstractRector
{
/**
* @var CallManipulator
* @var CallReflectionResolver
*/
private $callManipulator;
private $callReflectionResolver;

public function __construct(CallManipulator $callManipulator)
public function __construct(CallReflectionResolver $callReflectionResolver)
{
$this->callManipulator = $callManipulator;
$this->callReflectionResolver = $callReflectionResolver;
}

public function getDefinition(): RectorDefinition
Expand All @@ -63,10 +58,13 @@ public function refactor(Node $node): ?Node
return null;
}

/** @var ReflectionFunction $reflectionFunctionLike */
$reflectionFunctionLike = $this->resolveReflectionFunctionLike($node);
/** @var ParametersAcceptor $parametersAcceptor */
$parametersAcceptor = $this->callReflectionResolver->resolveParametersAcceptor(
$this->callReflectionResolver->resolveCall($node),
$node
);

$numberOfParameters = $reflectionFunctionLike->getNumberOfParameters();
$numberOfParameters = count($parametersAcceptor->getParameters());
$numberOfArguments = count($node->args);

for ($i = $numberOfParameters; $i <= $numberOfArguments; $i++) {
Expand Down Expand Up @@ -95,80 +93,19 @@ private function shouldSkip(Node $node): bool
}
}

$reflectionFunctionLike = $this->resolveReflectionFunctionLike($node);
if ($reflectionFunctionLike === null) {
$parametersAcceptor = $this->callReflectionResolver->resolveParametersAcceptor(
$this->callReflectionResolver->resolveCall($node),
$node
);
if ($parametersAcceptor === null) {
return true;
}

// can be any number of arguments → nothing to limit here
if ($this->callManipulator->isVariadic($reflectionFunctionLike, $node)) {
if ($parametersAcceptor->isVariadic()) {
return true;
}
return $reflectionFunctionLike->getNumberOfParameters() >= count($node->args);
}

/**
* @param FuncCall|MethodCall|StaticCall $node
* @return ReflectionFunction|ReflectionMethod
*/
private function resolveReflectionFunctionLike(Node $node): ?ReflectionFunctionAbstract
{
if ($node instanceof FuncCall) {
$functionName = $this->getName($node);
if ($functionName === null) {
return null;
}

if (! function_exists($functionName)) {
return null;
}

return new ReflectionFunction($functionName);
}

if ($node instanceof StaticCall) {
$objectType = $this->getObjectType($node->class);
} elseif ($node instanceof MethodCall) {
$objectType = $this->getObjectType($node->var);
} else {
return null;
}

// unable to resolve
if (! $objectType instanceof ObjectType && ! $objectType instanceof UnionType) {
return null;
}

$methodName = $this->getName($node);
if ($methodName === null) {
return null;
}

$class = $this->resolveClassOverIntefaceFromType($objectType);
if ($class === null) {
return null;
}

if (! method_exists($class, $methodName)) {
return null;
}

return new ReflectionMethod($class, $methodName);
}

/**
* Give class priority over interface, because it can change the interface signature
* @see https://github.com/rectorphp/rector/issues/1593#issuecomment-502404580
*/
private function resolveClassOverIntefaceFromType(Type $type): ?string
{
$classNames = TypeUtils::getDirectClassNames($type);
foreach ($classNames as $className) {
if (class_exists($className)) {
return $className;
}
}

return null;
return count($parametersAcceptor->getParameters()) >= count($node->args);
}
}
Loading