Skip to content

Commit

Permalink
Merge pull request #8922 from fluffycondor/sprintf-non-empty-string
Browse files Browse the repository at this point in the history
Make `sprintf` return `non-empty-string` when possible
  • Loading branch information
orklah committed Dec 18, 2022
2 parents a76a1d1 + 9a22d68 commit 7d95f15
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
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',
],
];
}
}

0 comments on commit 7d95f15

Please sign in to comment.