Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 12 additions & 9 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 @@ -132,14 +136,14 @@ public function getTypeFromFunctionCall(
continue 2;
}
}
$singlePlaceholderEarlyReturn = TypeCombinator::union(...$result);
$singlePlaceholderEarlyReturn[] = TypeCombinator::union(...$result);
Copy link
Member

Choose a reason for hiding this comment

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

Would be better to do one union at the end instead of these intermediate ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed ; fixed.


continue;
}

$singlePlaceholderEarlyReturn = $checkArgType->toString();
$singlePlaceholderEarlyReturn[] = $checkArgType->toString();
} elseif ($matches['specifier'] !== 's') {
$singlePlaceholderEarlyReturn = $this->getStringReturnType(
$singlePlaceholderEarlyReturn[] = $this->getStringReturnType(
new AccessoryNumericStringType(),
$isLowercase,
);
Expand All @@ -149,11 +153,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