Skip to content

Improve sprintf support #3310

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

Merged
merged 1 commit into from
Aug 9, 2024

Conversation

VincentLanglet
Copy link
Contributor

@phpstan-bot
Copy link
Collaborator

You've opened the pull request against the latest branch 1.12.x. If your code is relevant on 1.11.x and you want it to be released sooner, please rebase your pull request and change its target to 1.11.x.

@VincentLanglet VincentLanglet changed the base branch from 1.12.x to 1.11.x August 9, 2024 10:37
Copy link
Contributor Author

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

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

Since you made #3192 you might want to review @staabm

@@ -125,32 +125,45 @@ public function getTypeFromFunctionCall(
return $singlePlaceholderEarlyReturn;
}

if ($allPatternsNonFalsy) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Early return to avoid the following computation.

$isNonEmpty = $allPatternsNonEmpty;
if (
count($formatStrings) === 0
!$isNonEmpty
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If it's already known as non empty, we don't have to compute this.

&& $functionReflection->getName() === 'sprintf'
&& count($args) === 2
&& count($args) >= 2
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There was a special case for sprintf($nonEmpty, $nonEmpty, $nonEmpty) but this can be extended to sprintf($nonEmpty, $nonEmpty, $nonEmpty) and so on.

continue;
}

if (!$scope->getType($arg->value)->toString()->isNonEmptyString()->yes()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The toString was added for things like IntegerRange which need to be casted before the isNonEmptyString check.

@ondrejmirtes ondrejmirtes merged commit 14fd384 into phpstan:1.11.x Aug 9, 2024
444 of 464 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sprintf with non-empty-string arguments returns as string
3 participants