Skip to content

Commit

Permalink
Use methods directly on PhpFunctionFromParserNodeReflection instead o…
Browse files Browse the repository at this point in the history
…f `selectSingle()` when analysing function body in rules
  • Loading branch information
ondrejmirtes committed Sep 26, 2024
1 parent 1bea5c7 commit 41916ba
Show file tree
Hide file tree
Showing 32 changed files with 41 additions and 91 deletions.
2 changes: 1 addition & 1 deletion src/Reflection/Php/PhpFunctionFromParserNodeReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function getVariants(): array
$this->isVariadic(),
$this->getReturnType(),
$this->getPhpDocReturnType(),
$this->realReturnType,
$this->getNativeReturnType(),
),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\Node\MethodReturnStatementsNode;
use PHPStan\Reflection\ParametersAcceptorSelector;
use function count;
use function strtolower;

Expand Down Expand Up @@ -40,8 +39,7 @@ public function processNode(Node $node, Scope $scope)
return null;
}

$variant = ParametersAcceptorSelector::selectSingle($method->getVariants());
foreach ($variant->getParameters() as $parameter) {
foreach ($method->getParameters() as $parameter) {
if (!$parameter->passedByReference()->createsNewVariable()) {
continue;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Rules/DeadCode/FunctionWithoutImpurePointsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\Node\FunctionReturnStatementsNode;
use PHPStan\Reflection\ParametersAcceptorSelector;
use function count;

/**
Expand Down Expand Up @@ -38,8 +37,7 @@ public function processNode(Node $node, Scope $scope)
return null;
}

$variant = ParametersAcceptorSelector::selectSingle($function->getVariants());
foreach ($variant->getParameters() as $parameter) {
foreach ($function->getParameters() as $parameter) {
if (!$parameter->passedByReference()->createsNewVariable()) {
continue;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Rules/DeadCode/MethodWithoutImpurePointsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\Node\MethodReturnStatementsNode;
use PHPStan\Reflection\ParametersAcceptorSelector;
use function count;

/**
Expand Down Expand Up @@ -38,8 +37,7 @@ public function processNode(Node $node, Scope $scope)
return null;
}

$variant = ParametersAcceptorSelector::selectSingle($method->getVariants());
foreach ($variant->getParameters() as $parameter) {
foreach ($method->getParameters() as $parameter) {
if (!$parameter->passedByReference()->createsNewVariable()) {
continue;
}
Expand Down
14 changes: 4 additions & 10 deletions src/Rules/FunctionDefinitionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\NodeTypePrinter;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParameterReflectionWithPhpDocs;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\Reflection\Php\PhpFunctionFromParserNodeReflection;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
Expand Down Expand Up @@ -65,7 +64,7 @@ public function __construct(
*/
public function checkFunction(
Function_ $function,
FunctionReflection $functionReflection,
PhpFunctionFromParserNodeReflection $functionReflection,
string $parameterMessage,
string $returnMessage,
string $unionTypesMessage,
Expand All @@ -74,10 +73,8 @@ public function checkFunction(
string $unresolvableReturnTypeMessage,
): array
{
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants());

return $this->checkParametersAcceptor(
$parametersAcceptor,
$functionReflection,
$function,
$parameterMessage,
$returnMessage,
Expand Down Expand Up @@ -259,11 +256,8 @@ public function checkClassMethod(
string $selfOutMessage,
): array
{
/** @var ParametersAcceptorWithPhpDocs $parametersAcceptor */
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

$errors = $this->checkParametersAcceptor(
$parametersAcceptor,
$methodReflection,
$methodNode,
$parameterMessage,
$returnMessage,
Expand Down
5 changes: 1 addition & 4 deletions src/Rules/Functions/IncompatibleDefaultParameterTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InFunctionNode;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
Expand All @@ -28,8 +27,6 @@ public function getNodeType(): string
public function processNode(Node $node, Scope $scope): array
{
$function = $node->getFunctionReflection();
$parameters = ParametersAcceptorSelector::selectSingle($function->getVariants());

$errors = [];
foreach ($node->getOriginalNode()->getParams() as $paramI => $param) {
if ($param->default === null) {
Expand All @@ -43,7 +40,7 @@ public function processNode(Node $node, Scope $scope): array
}

$defaultValueType = $scope->getType($param->default);
$parameterType = $parameters->getParameters()[$paramI]->getType();
$parameterType = $function->getParameters()[$paramI]->getType();
$parameterType = TemplateTypeHelper::resolveToBounds($parameterType);

$accepts = $parameterType->acceptsWithReason($defaultValueType, true);
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Functions/MissingFunctionParameterTypehintRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\InFunctionNode;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\MissingTypehintCheck;
use PHPStan\Rules\Rule;
Expand Down Expand Up @@ -40,7 +39,7 @@ public function processNode(Node $node, Scope $scope): array
$functionReflection = $node->getFunctionReflection();
$messages = [];

foreach (ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getParameters() as $parameterReflection) {
foreach ($functionReflection->getParameters() as $parameterReflection) {
foreach ($this->checkFunctionParameter($functionReflection, sprintf('parameter $%s', $parameterReflection->getName()), $parameterReflection->getType()) as $parameterMessage) {
$messages[] = $parameterMessage;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Functions/MissingFunctionReturnTypehintRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InFunctionNode;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\MissingTypehintCheck;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand Down Expand Up @@ -34,7 +33,7 @@ public function getNodeType(): string
public function processNode(Node $node, Scope $scope): array
{
$functionReflection = $node->getFunctionReflection();
$returnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
$returnType = $functionReflection->getReturnType();

if ($returnType instanceof MixedType && !$returnType->isExplicitMixed()) {
return [
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Functions/ReturnTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\FunctionReturnTypeCheck;
use PHPStan\Rules\Rule;
use function sprintf;
Expand Down Expand Up @@ -45,7 +44,7 @@ public function processNode(Node $node, Scope $scope): array

return $this->returnTypeCheck->checkReturnType(
$scope,
ParametersAcceptorSelector::selectSingle($function->getVariants())->getReturnType(),
$function->getReturnType(),
$node->expr,
$node,
sprintf(
Expand Down
5 changes: 2 additions & 3 deletions src/Rules/Generators/YieldFromTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PhpParser\Node;
use PhpParser\Node\Expr\YieldFrom;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
Expand Down Expand Up @@ -69,7 +68,7 @@ public function processNode(Node $node, Scope $scope): array
if ($anonymousFunctionReturnType !== null) {
$returnType = $anonymousFunctionReturnType;
} elseif ($scopeFunction !== null) {
$returnType = ParametersAcceptorSelector::selectSingle($scopeFunction->getVariants())->getReturnType();
$returnType = $scopeFunction->getReturnType();
} else {
return []; // already reported by YieldInGeneratorRule
}
Expand Down Expand Up @@ -112,7 +111,7 @@ public function processNode(Node $node, Scope $scope): array
return $messages;
}

$currentReturnType = ParametersAcceptorSelector::selectSingle($scopeFunction->getVariants())->getReturnType();
$currentReturnType = $scopeFunction->getReturnType();
$exprSendType = $exprType->getTemplateType(Generator::class, 'TSend');
$thisSendType = $currentReturnType->getTemplateType(Generator::class, 'TSend');
if ($exprSendType instanceof ErrorType || $thisSendType instanceof ErrorType) {
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Generators/YieldInGeneratorRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\TrinaryLogic;
Expand Down Expand Up @@ -38,7 +37,7 @@ public function processNode(Node $node, Scope $scope): array
if ($anonymousFunctionReturnType !== null) {
$returnType = $anonymousFunctionReturnType;
} elseif ($scopeFunction !== null) {
$returnType = ParametersAcceptorSelector::selectSingle($scopeFunction->getVariants())->getReturnType();
$returnType = $scopeFunction->getReturnType();
} else {
return [
RuleErrorBuilder::message('Yield can be used only inside a function.')
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Generators/YieldTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
Expand Down Expand Up @@ -38,7 +37,7 @@ public function processNode(Node $node, Scope $scope): array
if ($anonymousFunctionReturnType !== null) {
$returnType = $anonymousFunctionReturnType;
} elseif ($scopeFunction !== null) {
$returnType = ParametersAcceptorSelector::selectSingle($scopeFunction->getVariants())->getReturnType();
$returnType = $scopeFunction->getReturnType();
} else {
return []; // already reported by YieldInGeneratorRule
}
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Generics/FunctionSignatureVarianceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Node\InFunctionNode;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\Rule;
use function sprintf;

Expand All @@ -31,7 +30,7 @@ public function processNode(Node $node, Scope $scope): array
$functionName = $functionReflection->getName();

return $this->varianceCheck->checkParametersAcceptor(
ParametersAcceptorSelector::selectSingle($functionReflection->getVariants()),
$functionReflection,
sprintf('in parameter %%s of function %s()', SprintfHelper::escapeFormatString($functionName)),
sprintf('in param-out type of parameter %%s of function %s()', SprintfHelper::escapeFormatString($functionName)),
sprintf('in return type of function %s()', $functionName),
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Generics/MethodSignatureVarianceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\Rule;
use function sprintf;

Expand All @@ -30,7 +29,7 @@ public function processNode(Node $node, Scope $scope): array
$method = $node->getMethodReflection();

return $this->varianceCheck->checkParametersAcceptor(
ParametersAcceptorSelector::selectSingle($method->getVariants()),
$method,
sprintf('in parameter %%s of method %s::%s()', SprintfHelper::escapeFormatString($method->getDeclaringClass()->getDisplayName()), SprintfHelper::escapeFormatString($method->getName())),
sprintf('in param-out type of parameter %%s of method %s::%s()', SprintfHelper::escapeFormatString($method->getDeclaringClass()->getDisplayName()), SprintfHelper::escapeFormatString($method->getName())),
sprintf('in return type of method %s::%s()', $method->getDeclaringClass()->getDisplayName(), $method->getName()),
Expand Down
5 changes: 1 addition & 4 deletions src/Rules/Methods/IncompatibleDefaultParameterTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
Expand All @@ -28,8 +27,6 @@ public function getNodeType(): string
public function processNode(Node $node, Scope $scope): array
{
$method = $node->getMethodReflection();
$parameters = ParametersAcceptorSelector::selectSingle($method->getVariants());

$errors = [];
foreach ($node->getOriginalNode()->getParams() as $paramI => $param) {
if ($param->default === null) {
Expand All @@ -43,7 +40,7 @@ public function processNode(Node $node, Scope $scope): array
}

$defaultValueType = $scope->getType($param->default);
$parameter = $parameters->getParameters()[$paramI];
$parameter = $method->getParameters()[$paramI];
$parameterType = $parameter->getType();
$parameterType = TemplateTypeHelper::resolveToBounds($parameterType);

Expand Down
4 changes: 1 addition & 3 deletions src/Rules/Methods/MethodParameterComparisonHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\RuleErrorBuilder;
Expand Down Expand Up @@ -37,8 +36,7 @@ public function compare(ExtendedMethodReflection $prototype, ClassReflection $pr
$messages = [];
$prototypeVariant = $prototype->getVariants()[0];

$methodVariant = ParametersAcceptorSelector::selectSingle($method->getVariants());
$methodParameters = $methodVariant->getParameters();
$methodParameters = $method->getParameters();

$prototypeAfterVariadic = false;
foreach ($prototypeVariant->getParameters() as $i => $prototypeParameter) {
Expand Down
8 changes: 3 additions & 5 deletions src/Rules/Methods/MethodSignatureRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public function processNode(Node $node, Scope $scope): array
if ($method->isPrivate()) {
return [];
}
$parameters = ParametersAcceptorSelector::selectSingle($method->getVariants());

$errors = [];
$declaringClass = $method->getDeclaringClass();
foreach ($this->collectParentMethods($methodName, $method->getDeclaringClass()) as [$parentMethod, $parentMethodDeclaringClass]) {
Expand All @@ -77,7 +75,7 @@ public function processNode(Node $node, Scope $scope): array
continue;
}
$parentParameters = ParametersAcceptorSelector::selectSingle($parentVariants);
[$returnTypeCompatibility, $returnType, $parentReturnType] = $this->checkReturnTypeCompatibility($declaringClass, $parameters, $parentParameters);
[$returnTypeCompatibility, $returnType, $parentReturnType] = $this->checkReturnTypeCompatibility($declaringClass, $method, $parentParameters);
if ($returnTypeCompatibility->no() || (!$returnTypeCompatibility->yes() && $this->reportMaybes)) {
$builder = RuleErrorBuilder::message(sprintf(
'Return type (%s) of method %s::%s() should be %s with return type (%s) of method %s::%s()',
Expand Down Expand Up @@ -116,15 +114,15 @@ public function processNode(Node $node, Scope $scope): array
$errors[] = $builder->build();
}

$parameterResults = $this->checkParameterTypeCompatibility($declaringClass, $parameters->getParameters(), $parentParameters->getParameters());
$parameterResults = $this->checkParameterTypeCompatibility($declaringClass, $method->getParameters(), $parentParameters->getParameters());
foreach ($parameterResults as $parameterIndex => [$parameterResult, $parameterType, $parentParameterType]) {
if ($parameterResult->yes()) {
continue;
}
if (!$parameterResult->no() && !$this->reportMaybes) {
continue;
}
$parameter = $parameters->getParameters()[$parameterIndex];
$parameter = $method->getParameters()[$parameterIndex];
$parentParameter = $parentParameters->getParameters()[$parameterIndex];
$errors[] = RuleErrorBuilder::message(sprintf(
'Parameter #%d $%s (%s) of method %s::%s() should be %s with parameter $%s (%s) of method %s::%s()',
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Methods/MissingMethodParameterTypehintRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\MissingTypehintCheck;
use PHPStan\Rules\Rule;
Expand Down Expand Up @@ -40,7 +39,7 @@ public function processNode(Node $node, Scope $scope): array
$methodReflection = $node->getMethodReflection();
$messages = [];

foreach (ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getParameters() as $parameterReflection) {
foreach ($methodReflection->getParameters() as $parameterReflection) {
foreach ($this->checkMethodParameter($methodReflection, sprintf('parameter $%s', $parameterReflection->getName()), $parameterReflection->getType()) as $parameterMessage) {
$messages[] = $parameterMessage;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Methods/MissingMethodReturnTypehintRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\MissingTypehintCheck;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand Down Expand Up @@ -39,7 +38,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}
}
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
$returnType = $methodReflection->getReturnType();

if ($returnType instanceof MixedType && !$returnType->isExplicitMixed()) {
return [
Expand Down
Loading

0 comments on commit 41916ba

Please sign in to comment.