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

Make sprintf return non-empty-string when possible #8922

Merged
merged 5 commits into from Dec 18, 2022
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions stubs/CoreGenericFunctions.phpstub
Expand Up @@ -1117,6 +1117,9 @@ function preg_quote(string $str, ?string $delimiter = null) : string {}
* @psalm-pure
*
* @param string|int|float $values
* @return ($format is non-empty-string
* ? ($values is non-empty-string|int|float ? non-empty-string : string)
* : string)
*
* @psalm-flow ($format, $values) -> return
*/
Expand Down
1 change: 0 additions & 1 deletion tests/Config/ConfigTest.php
Expand Up @@ -1461,7 +1461,6 @@ public function pluginRegistersScannerAndAnalyzer(int $flags, ?int $expectedExce
FileTypeSelfRegisteringPlugin::$names = $names;
FileTypeSelfRegisteringPlugin::$flags = $flags;

/** @var non-empty-string $xml */
$xml = sprintf(
'<?xml version="1.0"?>
<psalm><plugins><pluginClass class="%s"/></plugins></psalm>',
Expand Down
2 changes: 0 additions & 2 deletions tests/Config/PluginTest.php
Expand Up @@ -801,7 +801,6 @@ public function testAfterAnalysisHooks(): void

public function testPluginFilenameCanBeAbsolute(): void
{
/** @var non-empty-string $xml */
$xml = sprintf(
'<?xml version="1.0"?>
<psalm
Expand All @@ -828,7 +827,6 @@ public function testPluginInvalidAbsoluteFilenameThrowsException(): void
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('does-not-exist/plugins/StringChecker.php');

/** @var non-empty-string $xml */
$xml = sprintf(
'<?xml version="1.0"?>
<psalm
Expand Down
22 changes: 22 additions & 0 deletions tests/CoreStubsTest.php
Expand Up @@ -101,5 +101,27 @@ public function providerValidCodeParse(): iterable
'ignored_issues' => ['RedundantCondition'],
'php_version' => '8.0',
];
yield 'sprintf yields a non-empty-string for non-empty-string value' => [
'code' => '<?php

/**
* @param non-empty-string $foo
* @return non-empty-string
*/
function foo(string $foo): string
{
return sprintf("%s", $foo);
}
',
];
yield 'sprintf yields a string for possible empty string param' => [
'code' => '<?php

$a = sprintf("%s", "");
',
'assertions' => [
'$a===' => 'string',
],
];
}
}