1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-29 20:28:59 +01:00

Merge pull request #8922 from fluffycondor/sprintf-non-empty-string

Make `sprintf` return `non-empty-string` when possible
This commit is contained in:
orklah 2022-12-18 12:35:59 +01:00 committed by GitHub
commit 7d95f15e30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 3 deletions

View File

@ -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
*/

View File

@ -1461,7 +1461,6 @@ class ConfigTest extends TestCase
FileTypeSelfRegisteringPlugin::$names = $names;
FileTypeSelfRegisteringPlugin::$flags = $flags;
/** @var non-empty-string $xml */
$xml = sprintf(
'<?xml version="1.0"?>
<psalm><plugins><pluginClass class="%s"/></plugins></psalm>',

View File

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

View File

@ -101,5 +101,27 @@ class CoreStubsTest extends TestCase
'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',
],
];
}
}