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

Improved sprintf() inference #3232

Merged
merged 7 commits into from
Jul 14, 2024
Merged

Improved sprintf() inference #3232

merged 7 commits into from
Jul 14, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Jul 13, 2024

another small improvement while thinking thru more details of #3192

it fixes a bug regressed with #3167

@staabm staabm marked this pull request as draft July 13, 2024 07:41
Comment on lines +49 to +56
$s = sprintf('%20s', 'abc');
assertType("' abc'", $s);

$s = sprintf('%20s', true);
assertType("' 1'", $s);

$s = sprintf('%20s', returnsBool());
assertType("non-falsy-string", $s);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

regressions from #3167 which made sprintf 1:1 return constant values from arguments (bypassing sprintf formatting rules)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

//cc @pilif

Comment on lines +55 to +58
assertType('non-falsy-string&numeric-string', sprintf('%2$14s', $mixed, $posInt));
assertType('non-falsy-string&numeric-string', sprintf('%2$14s', $mixed, $negInt));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

improvements because of the added toString() handling

@staabm staabm marked this pull request as ready for review July 13, 2024 08:00
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@@ -115,19 +119,19 @@ public function getTypeFromFunctionCall(
$returnType = new StringType();
}

return $this->getConstantType($args, $returnType, $functionReflection, $scope);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

instead of building the general return type first and pass it as a default-return into getConstantType() we now invoke getConstantType() first and build the general return type only if we were not able to infer a constant type in the first place

@staabm staabm marked this pull request as draft July 13, 2024 09:37
@staabm staabm marked this pull request as ready for review July 13, 2024 11:52
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@ondrejmirtes ondrejmirtes merged commit 8713b14 into phpstan:1.11.x Jul 14, 2024
452 of 455 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the sprintf5 branch July 14, 2024 07:01
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.

3 participants