-
Notifications
You must be signed in to change notification settings - Fork 523
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
Improve sprintf support #3310
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,32 +125,45 @@ public function getTypeFromFunctionCall( | |
return $singlePlaceholderEarlyReturn; | ||
} | ||
|
||
if ($allPatternsNonFalsy) { | ||
return new IntersectionType([ | ||
new StringType(), | ||
new AccessoryNonFalsyStringType(), | ||
]); | ||
} | ||
|
||
$isNonEmpty = $allPatternsNonEmpty; | ||
if ( | ||
count($formatStrings) === 0 | ||
!$isNonEmpty | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
&& $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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
|
||
/** | ||
|
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, | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
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.