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

Simplify SprintfFunctionDynamicReturnTypeExtension #3188

Merged
merged 9 commits into from
Jun 26, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Jun 25, 2024

extracted from /pull/3168


if (count($formatType->getConstantStrings()) > 0) {
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 loop is the same as before, just the if statement was unwrapped

@staabm
Copy link
Contributor Author

staabm commented Jun 25, 2024

the new composer/composer errors are valid because the typing before this PR was too precise.

we can't know whether sprintf will return non-empty-string in a scenario where the format-pattern is a non-constant variable.
https://github.com/composer/composer/blob/776ff2ea51bfa83335b14fa9a85fab5fc7d35050/src/Composer/Package/BasePackage.php#L256
https://github.com/composer/composer/blob/776ff2ea51bfa83335b14fa9a85fab5fc7d35050/src/Composer/Package/BasePackage.php#L275

@staabm staabm marked this pull request as ready for review June 25, 2024 13:38
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

}
if (
$functionReflection->getName() === 'vsprintf'
&& count($args) === 2
Copy link
Member

Choose a reason for hiding this comment

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

Please add some tests in case when there is $arg->unpack === true involved.

@@ -48,50 +48,64 @@ public function getTypeFromFunctionCall(
}

$formatType = $scope->getType($args[0]->value);
if (count($args) === 1) {
return null;
Copy link
Member

Choose a reason for hiding this comment

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

We know what would happen so we could be more precise: https://3v4l.org/DeSsG

assertType('string', sprintf($nonFalsey));
assertType("'foo'", sprintf('foo'));
assertType("string", sprintf(...$arr));
assertType("non-falsy-string", sprintf('%s', ...$arr)); // should be 'string'
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 wrong non-falsy-string types will be fixed after we integrated the remaining parts of #3168

@ondrejmirtes ondrejmirtes merged commit 72281be into phpstan:1.11.x Jun 26, 2024
450 of 454 checks passed
@ondrejmirtes
Copy link
Member

Thank you.

@staabm staabm deleted the sprintf2 branch June 26, 2024 12:23
@ondrejmirtes
Copy link
Member

Hey, if I have sprintf($nonEmptyString, $alsoNonEmptyString), why the inferred type isn't non-empty-string?

@herndlm
Copy link
Contributor

herndlm commented Jun 26, 2024

@staabm how do you find the time to work so much on PHPStan. I'm doing something wrong 😅 recently too busy and I do lack motivation to work on it at night..

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.

4 participants