Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve parameter types only when checkArgumentTypes=true #2106

Merged
merged 1 commit into from Dec 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 35 additions & 36 deletions src/Rules/FunctionCallParametersCheck.php
Expand Up @@ -249,43 +249,42 @@ public function check(
continue;
}

$parameterType = TypeUtils::resolveLateResolvableTypes($parameter->getType());
if (
$this->checkArgumentTypes
&& !$parameter->passedByReference()->createsNewVariable()
&& !$this->ruleLevelHelper->accepts($parameterType, $argumentValueType, $scope->isDeclareStrictTypes())
) {
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $argumentValueType);
$parameterDescription = sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName());
$errors[] = RuleErrorBuilder::message(sprintf(
$messages[6],
$argumentName === null ? sprintf(
'#%d %s',
$i + 1,
$parameterDescription,
) : $parameterDescription,
$parameterType->describe($verbosityLevel),
$argumentValueType->describe($verbosityLevel),
))->line($argumentLine)->build();
}
if ($this->checkArgumentTypes) {
$parameterType = TypeUtils::resolveLateResolvableTypes($parameter->getType());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer this resolving until we realy need the value


if (
$this->checkArgumentTypes
&& $this->checkUnresolvableParameterTypes
&& $originalParameter !== null
&& !$this->unresolvableTypeHelper->containsUnresolvableType($originalParameter->getType())
&& $this->unresolvableTypeHelper->containsUnresolvableType($parameterType)
&& isset($messages[13])
) {
$parameterDescription = sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName());
$errors[] = RuleErrorBuilder::message(sprintf(
$messages[13],
$argumentName === null ? sprintf(
'#%d %s',
$i + 1,
$parameterDescription,
) : $parameterDescription,
))->line($argumentLine)->build();
if (!$parameter->passedByReference()->createsNewVariable()
&& !$this->ruleLevelHelper->accepts($parameterType, $argumentValueType, $scope->isDeclareStrictTypes())
) {
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $argumentValueType);
$parameterDescription = sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName());
$errors[] = RuleErrorBuilder::message(sprintf(
$messages[6],
$argumentName === null ? sprintf(
'#%d %s',
$i + 1,
$parameterDescription,
) : $parameterDescription,
$parameterType->describe($verbosityLevel),
$argumentValueType->describe($verbosityLevel),
))->line($argumentLine)->build();
}

if ($this->checkUnresolvableParameterTypes
&& $originalParameter !== null
&& isset($messages[13])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved the cheap isset($messages[13]) condition before the 2 resolving calls

&& !$this->unresolvableTypeHelper->containsUnresolvableType($originalParameter->getType())
&& $this->unresolvableTypeHelper->containsUnresolvableType($parameterType)
) {
$parameterDescription = sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName());
$errors[] = RuleErrorBuilder::message(sprintf(
$messages[13],
$argumentName === null ? sprintf(
'#%d %s',
$i + 1,
$parameterDescription,
) : $parameterDescription,
))->line($argumentLine)->build();
}
}

if (
Expand Down