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
51 changes: 32 additions & 19 deletions src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,37 +155,50 @@ public function getTypeFromFunctionCall(
}

$isNonEmpty = $allPatternsNonEmpty;
if (
!$isNonEmpty
&& $functionReflection->getName() === 'sprintf'
&& count($args) >= 2
&& $formatType->isNonEmptyString()->yes()
) {
$allArgsNonEmpty = true;
if (!$isNonEmpty && $formatType->isNonEmptyString()->yes()) {
$isNonEmpty = $this->allValuesSatisfies(
$functionReflection,
$scope,
$args,
static fn (Type $type): bool => $type->toString()->isNonEmptyString()->yes()
);
}

if ($isNonEmpty) {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}

return new StringType();
}

/**
* @param array<Arg> $args
* @param callable(Type): bool $cb
*/
private function allValuesSatisfies(FunctionReflection $functionReflection, Scope $scope, array $args, callable $cb): bool
{
if ($functionReflection->getName() === 'sprintf' && count($args) >= 2) {
foreach ($args as $key => $arg) {
if ($key === 0) {
continue;
}

if (!$scope->getType($arg->value)->toString()->isNonEmptyString()->yes()) {
$allArgsNonEmpty = false;
break;
if (!$cb($scope->getType($arg->value))) {
return false;
}
}

if ($allArgsNonEmpty) {
$isNonEmpty = true;
}
return true;
}

if ($isNonEmpty) {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
if ($functionReflection->getName() === 'vsprintf' && count($args) >= 2) {
return $cb($scope->getType($args[1]->value)->getIterableValueType());
}

return new StringType();
return false;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-7387.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function vsprintf(array $array)
assertType('numeric-string', vsprintf("%4d", explode('-', '1988-8-1')));
assertType('numeric-string', vsprintf("%4d", $array));
assertType('numeric-string', vsprintf("%4d", ['123']));
assertType('string', vsprintf("%s", ['123'])); // could be '123'
assertType('non-empty-string', vsprintf("%s", ['123'])); // could be '123'
// too many arguments.. php silently allows it
assertType('numeric-string', vsprintf("%4d", ['123', '456']));
}
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/nsrt/non-empty-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,13 @@ public function doFoo(string $s, string $nonEmpty, string $nonFalsy, int $i, boo
assertType('non-empty-string', sprintf($nonFalsy, $nonFalsy, $nonFalsy));
assertType('string', vsprintf($s, []));
assertType('string', vsprintf($nonEmpty, []));
assertType('non-empty-string', vsprintf($nonEmpty, [$nonEmpty]));
assertType('non-empty-string', vsprintf($nonEmpty, [$nonEmpty, $nonEmpty]));
assertType('non-empty-string', vsprintf($nonEmpty, [$nonFalsy, $nonFalsy]));
assertType('non-empty-string', vsprintf($nonFalsy, [$nonEmpty]));
assertType('non-empty-string', vsprintf($nonFalsy, [$nonEmpty, $nonEmpty]));
assertType('non-empty-string', vsprintf($nonFalsy, [$nonFalsy, $nonEmpty]));
assertType('non-empty-string', vsprintf($nonFalsy, [$nonFalsy, $nonFalsy]));

assertType('non-empty-string', sprintf("%s0%s", $s, $s));
assertType('non-empty-string', sprintf("%s0%s%s%s%s", $s, $s, $s, $s, $s));
Expand Down
Loading