Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use function count;
use function in_array;
use function intval;
use function is_array;
use function is_string;
use function preg_match;
use function sprintf;
Expand Down Expand Up @@ -69,7 +70,7 @@ public function getTypeFromFunctionCall(
static fn (Type $type): bool => $type->toString()->isLowercaseString()->yes()
);

$singlePlaceholderEarlyReturn = null;
$singlePlaceholderEarlyReturn = [];
$allPatternsNonEmpty = count($formatStrings) !== 0;
$allPatternsNonFalsy = count($formatStrings) !== 0;
foreach ($formatStrings as $constantString) {
Expand All @@ -93,8 +94,11 @@ public function getTypeFromFunctionCall(
$allPatternsNonFalsy = false;
}

// The printf format is %[argnum$][flags][width][.precision]specifier.
if (preg_match('/^%(?P<argnum>[0-9]*\$)?(?P<width>[0-9]*)\.?[0-9]*(?P<specifier>[sbdeEfFgGhHouxX])$/', $constantString->getValue(), $matches) === 1) {
if (
is_array($singlePlaceholderEarlyReturn)
// The printf format is %[argnum$][flags][width][.precision]specifier.
&& preg_match('/^%(?P<argnum>[0-9]*\$)?(?P<width>[0-9]*)\.?[0-9]*(?P<specifier>[sbdeEfFgGhHouxX])$/', $constantString->getValue(), $matches) === 1
) {
if ($matches['argnum'] !== '') {
// invalid positional argument
if ($matches['argnum'] === '0$') {
Expand Down Expand Up @@ -122,24 +126,22 @@ public function getTypeFromFunctionCall(
$constArgTypes = $checkArgType->getConstantScalarTypes();
}
if ($constArgTypes !== []) {
$result = [];
$printfArgs = array_fill(0, count($args) - 1, '');
foreach ($constArgTypes as $constArgType) {
$printfArgs[$checkArg - 1] = $constArgType->getValue();
try {
$result[] = new ConstantStringType(@sprintf($constantString->getValue(), ...$printfArgs));
$singlePlaceholderEarlyReturn[] = new ConstantStringType(@sprintf($constantString->getValue(), ...$printfArgs));
} catch (Throwable) {
continue 2;
}
}
$singlePlaceholderEarlyReturn = TypeCombinator::union(...$result);

continue;
}

$singlePlaceholderEarlyReturn = $checkArgType->toString();
$singlePlaceholderEarlyReturn[] = $checkArgType->toString();
} elseif ($matches['specifier'] !== 's') {
$singlePlaceholderEarlyReturn = $this->getStringReturnType(
$singlePlaceholderEarlyReturn[] = $this->getStringReturnType(
new AccessoryNumericStringType(),
$isLowercase,
);
Expand All @@ -149,11 +151,10 @@ public function getTypeFromFunctionCall(
}

$singlePlaceholderEarlyReturn = null;
break;
}

if ($singlePlaceholderEarlyReturn !== null) {
return $singlePlaceholderEarlyReturn;
if (is_array($singlePlaceholderEarlyReturn) && count($singlePlaceholderEarlyReturn) > 0) {
return TypeCombinator::union(...$singlePlaceholderEarlyReturn);
}

if ($allPatternsNonFalsy) {
Expand Down
43 changes: 43 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12065.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace Bug12065;

use function PHPStan\Testing\assertType;

final class Foo
{
public function bar(
string $key,
bool $preserveKeys,
bool $flatten = true,
): void {
$format = $preserveKeys ? '[%s]' : '';

if ($flatten) {
$_key = sprintf($format, $key);
} else {
$_key = sprintf($format, $key);
assertType('string', $_key);
// @phpstan-ignore identical.alwaysFalse
if ($_key === '') {
// ...
}
}
}

/**
* @param int<1,2> $key
* @param bool $preserveKeys
*
* @return void
*/
public function bar2(
string $key,
bool $preserveKeys,
): void {
$format = $preserveKeys ? '%s' : '%d';

$_key = sprintf($format, $key);
assertType("string", $_key);
}
}
Loading