Skip to content

Improve sprintf support #3310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2024
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
41 changes: 27 additions & 14 deletions src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,32 +125,45 @@ public function getTypeFromFunctionCall(
return $singlePlaceholderEarlyReturn;
}

if ($allPatternsNonFalsy) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Early return to avoid the following computation.

return new IntersectionType([
new StringType(),
new AccessoryNonFalsyStringType(),
]);
}

$isNonEmpty = $allPatternsNonEmpty;
if (
count($formatStrings) === 0
!$isNonEmpty
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If it's already known as non empty, we don't have to compute this.

&& $functionReflection->getName() === 'sprintf'
&& count($args) === 2
&& count($args) >= 2
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There was a special case for sprintf($nonEmpty, $nonEmpty, $nonEmpty) but this can be extended to sprintf($nonEmpty, $nonEmpty, $nonEmpty) and so on.

&& $formatType->isNonEmptyString()->yes()
&& $scope->getType($args[1]->value)->isNonEmptyString()->yes()
) {
$isNonEmpty = true;
$allArgsNonEmpty = true;
foreach ($args as $key => $arg) {
if ($key === 0) {
continue;
}

if (!$scope->getType($arg->value)->toString()->isNonEmptyString()->yes()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The toString was added for things like IntegerRange which need to be casted before the isNonEmptyString check.

$allArgsNonEmpty = false;
break;
}
}

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

if ($allPatternsNonFalsy) {
$returnType = new IntersectionType([
new StringType(),
new AccessoryNonFalsyStringType(),
]);
} elseif ($isNonEmpty) {
$returnType = new IntersectionType([
if ($isNonEmpty) {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
} else {
$returnType = new StringType();
}

return $returnType;
return new StringType();
}

/**
Expand Down
6 changes: 5 additions & 1 deletion tests/PHPStan/Analyser/nsrt/non-empty-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,12 @@ public function doFoo(string $s, string $nonEmpty, string $nonFalsy, int $i, boo
assertType('string', sprintf($s, $nonFalsy));
assertType('string', sprintf($nonFalsy, $s));
assertType('non-empty-string', sprintf($nonEmpty, $nonEmpty));
assertType('non-empty-string', sprintf($nonEmpty, $nonFalsy));
assertType('non-empty-string', sprintf($nonEmpty, $nonEmpty, $nonEmpty));
assertType('non-empty-string', sprintf($nonEmpty, $nonFalsy, $nonFalsy));
assertType('non-empty-string', sprintf($nonFalsy, $nonEmpty));
assertType('non-empty-string', sprintf($nonFalsy, $nonEmpty, $nonEmpty));
assertType('non-empty-string', sprintf($nonFalsy, $nonFalsy, $nonEmpty));
assertType('non-empty-string', sprintf($nonFalsy, $nonFalsy, $nonFalsy));
assertType('string', vsprintf($s, []));
assertType('string', vsprintf($nonEmpty, []));

Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1034,4 +1034,9 @@ public function testBug10721(): void
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-10721.php'], []);
}

public function testBug11491(): void
{
$this->analyse([__DIR__ . '/data/bug-11491.php'], []);
}

}
49 changes: 49 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-11491.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace PHPStan\Rules\Methods\data;

use function sprintf;

final class NamedFacet
{
private const string SEPARATOR = '+++';

private function __construct(
/** @var positive-int */
private int $id,
/** @var non-empty-string */
private string $name,
) {}

/**
* @param positive-int $id
* @param non-empty-string $name
*/
public static function fromIdAndName(int $id, string $name): self
{
return new self($id, $name);
}

/** @return positive-int */
public function id(): int
{
return $this->id;
}

/** @return non-empty-string */
public function name(): string
{
return $this->name;
}

/** @return non-empty-string */
public function toFacetValue(): string
{
return sprintf(
'%s%s%s',
$this->name,
self::SEPARATOR,
$this->id,
);
}
}
Loading