-
Notifications
You must be signed in to change notification settings - Fork 542
Handle lowercase string in sprintf #3498
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
Changes from 4 commits
cb6de45
69a8b53
c8d48b4
86ddf78
b52f1b5
b22c45b
bb3a706
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
use PHPStan\Internal\CombinationsHelper; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Reflection\InitializerExprTypeResolver; | ||
use PHPStan\Type\Accessory\AccessoryLowercaseStringType; | ||
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; | ||
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; | ||
use PHPStan\Type\Accessory\AccessoryNumericStringType; | ||
|
@@ -60,6 +61,13 @@ public function getTypeFromFunctionCall( | |
$formatType = $scope->getType($args[0]->value); | ||
$formatStrings = $formatType->getConstantStrings(); | ||
|
||
$isLowercase = $formatType->isLowercaseString()->yes() && $this->allValuesSatisfies( | ||
$functionReflection, | ||
$scope, | ||
$args, | ||
static fn (Type $type): bool => $type->toString()->isLowercaseString()->yes() | ||
); | ||
|
||
$singlePlaceholderEarlyReturn = null; | ||
$allPatternsNonEmpty = count($formatStrings) !== 0; | ||
$allPatternsNonFalsy = count($formatStrings) !== 0; | ||
|
@@ -130,10 +138,18 @@ public function getTypeFromFunctionCall( | |
|
||
$singlePlaceholderEarlyReturn = $checkArgType->toString(); | ||
} elseif ($matches['specifier'] !== 's') { | ||
$singlePlaceholderEarlyReturn = new IntersectionType([ | ||
new StringType(), | ||
new AccessoryNumericStringType(), | ||
]); | ||
if ($isLowercase) { | ||
$singlePlaceholderEarlyReturn = new IntersectionType([ | ||
new StringType(), | ||
new AccessoryLowercaseStringType(), | ||
new AccessoryNumericStringType(), | ||
]); | ||
} else { | ||
$singlePlaceholderEarlyReturn = new IntersectionType([ | ||
new StringType(), | ||
new AccessoryNumericStringType(), | ||
]); | ||
} | ||
|
||
} | ||
|
||
continue; | ||
|
@@ -148,6 +164,14 @@ public function getTypeFromFunctionCall( | |
} | ||
|
||
if ($allPatternsNonFalsy) { | ||
if ($isLowercase) { | ||
return new IntersectionType([ | ||
new StringType(), | ||
new AccessoryLowercaseStringType(), | ||
new AccessoryNonFalsyStringType(), | ||
]); | ||
} | ||
|
||
return new IntersectionType([ | ||
new StringType(), | ||
new AccessoryNonFalsyStringType(), | ||
|
@@ -165,12 +189,27 @@ public function getTypeFromFunctionCall( | |
} | ||
|
||
if ($isNonEmpty) { | ||
if ($isLowercase) { | ||
return new IntersectionType([ | ||
new StringType(), | ||
new AccessoryLowercaseStringType(), | ||
new AccessoryNonEmptyStringType(), | ||
]); | ||
} | ||
|
||
return new IntersectionType([ | ||
new StringType(), | ||
new AccessoryNonEmptyStringType(), | ||
]); | ||
} | ||
|
||
if ($isLowercase) { | ||
return new IntersectionType([ | ||
new StringType(), | ||
new AccessoryLowercaseStringType(), | ||
]); | ||
} | ||
|
||
return new StringType(); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace LowercaseStringSprintf; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
|
||
/** | ||
* @param lowercase-string $lowercase | ||
* @param lowercase-string&non-empty-string $nonEmptyLowercase | ||
* @param lowercase-string&non-falsy-string $nonFalsyLowercase | ||
*/ | ||
public function doSprintf( | ||
string $string, | ||
string $lowercase, | ||
string $nonEmptyLowercase, | ||
string $nonFalsyLowercase, | ||
bool $bool | ||
): void { | ||
$format = $bool ? 'Foo 1 %s' : 'Foo 2 %s'; | ||
$formatLower = $bool ? 'foo 1 %s' : 'foo 2 %s'; | ||
$constant = $bool ? 'A' : 'B'; | ||
$constantLower = $bool ? 'a' : 'b'; | ||
|
||
assertType("'A'|'B'", sprintf('%s', $constant)); | ||
assertType("'0'", sprintf('%d', $constant)); | ||
assertType("'Foo 1 A'|'Foo 1 B'|'Foo 2 A'|'Foo 2 B'", sprintf($format, $constant)); | ||
assertType("'foo 1 A'|'foo 1 B'|'foo 2 A'|'foo 2 B'", sprintf($formatLower, $constant)); | ||
assertType('string', sprintf($lowercase, $constant)); | ||
assertType('string', sprintf($string, $constant)); | ||
|
||
assertType("'a'|'b'", sprintf('%s', $constantLower)); | ||
assertType("'0'", sprintf('%d', $constantLower)); | ||
assertType("'Foo 1 a'|'Foo 1 b'|'Foo 2 a'|'Foo 2 b'", sprintf($format, $constantLower)); | ||
assertType("'foo 1 a'|'foo 1 b'|'foo 2 a'|'foo 2 b'", sprintf($formatLower, $constantLower)); | ||
assertType('lowercase-string', sprintf($lowercase, $constantLower)); | ||
assertType('string', sprintf($string, $constantLower)); | ||
|
||
assertType('lowercase-string', sprintf('%s', $lowercase)); | ||
assertType('lowercase-string&numeric-string', sprintf('%d', $lowercase)); | ||
assertType('non-falsy-string', sprintf($format, $lowercase)); | ||
assertType('lowercase-string&non-falsy-string', sprintf($formatLower, $lowercase)); | ||
assertType('lowercase-string', sprintf($lowercase, $lowercase)); | ||
assertType('string', sprintf($string, $lowercase)); | ||
|
||
assertType('lowercase-string&non-empty-string', sprintf('%s', $nonEmptyLowercase)); | ||
assertType('lowercase-string&numeric-string', sprintf('%d', $nonEmptyLowercase)); | ||
assertType('non-falsy-string', sprintf($format, $nonEmptyLowercase)); | ||
assertType('lowercase-string&non-falsy-string', sprintf($formatLower, $nonEmptyLowercase)); | ||
assertType('lowercase-string&non-empty-string', sprintf($nonEmptyLowercase, $nonEmptyLowercase)); | ||
assertType('string', sprintf($string, $nonEmptyLowercase)); | ||
|
||
assertType('lowercase-string&non-falsy-string', sprintf('%s', $nonFalsyLowercase)); | ||
assertType('lowercase-string&numeric-string', sprintf('%d', $nonFalsyLowercase)); | ||
assertType('non-falsy-string', sprintf($format, $nonFalsyLowercase)); | ||
assertType('lowercase-string&non-falsy-string', sprintf($formatLower, $nonFalsyLowercase)); | ||
assertType('lowercase-string&non-empty-string', sprintf($nonFalsyLowercase, $nonFalsyLowercase)); | ||
assertType('string', sprintf($string, $nonFalsyLowercase)); | ||
} | ||
|
||
/** | ||
* @param lowercase-string $lowercase | ||
* @param lowercase-string&non-empty-string $nonEmptyLowercase | ||
* @param lowercase-string&non-falsy-string $nonFalsyLowercase | ||
*/ | ||
public function doVSprintf( | ||
string $string, | ||
string $lowercase, | ||
string $nonEmptyLowercase, | ||
string $nonFalsyLowercase, | ||
bool $bool | ||
): void { | ||
$format = $bool ? 'Foo 1 %s' : 'Foo 2 %s'; | ||
$formatLower = $bool ? 'foo 1 %s' : 'foo 2 %s'; | ||
$constant = $bool ? 'A' : 'B'; | ||
$constantLower = $bool ? 'a' : 'b'; | ||
|
||
assertType("'A'|'B'", vsprintf('%s', [$constant])); | ||
assertType('numeric-string', vsprintf('%d', [$constant])); | ||
assertType('non-falsy-string', vsprintf($format, [$constant])); | ||
assertType('non-falsy-string', vsprintf($formatLower, [$constant])); | ||
assertType('string', vsprintf($lowercase, [$constant])); | ||
assertType('string', vsprintf($string, [$constant])); | ||
|
||
assertType("'a'|'b'", vsprintf('%s', [$constantLower])); | ||
assertType('lowercase-string&numeric-string', vsprintf('%d', [$constantLower])); | ||
assertType('non-falsy-string', vsprintf($format, [$constantLower])); | ||
assertType('lowercase-string&non-falsy-string', vsprintf($formatLower, [$constantLower])); | ||
assertType('lowercase-string', vsprintf($lowercase, [$constantLower])); | ||
assertType('string', vsprintf($string, [$constantLower])); | ||
|
||
assertType('lowercase-string', vsprintf('%s', [$lowercase])); | ||
assertType('lowercase-string&numeric-string', vsprintf('%d', [$lowercase])); | ||
assertType('non-falsy-string', vsprintf($format, [$lowercase])); | ||
assertType('lowercase-string&non-falsy-string', vsprintf($formatLower, [$lowercase])); | ||
assertType('lowercase-string', vsprintf($lowercase, [$lowercase])); | ||
assertType('string', vsprintf($string, [$lowercase])); | ||
|
||
assertType('lowercase-string&non-empty-string', vsprintf('%s', [$nonEmptyLowercase])); | ||
assertType('lowercase-string&numeric-string', vsprintf('%d', [$nonEmptyLowercase])); | ||
assertType('non-falsy-string', vsprintf($format, [$nonEmptyLowercase])); | ||
assertType('lowercase-string&non-falsy-string', vsprintf($formatLower, [$nonEmptyLowercase])); | ||
assertType('lowercase-string&non-empty-string', vsprintf($nonEmptyLowercase, [$nonEmptyLowercase])); | ||
assertType('string', vsprintf($string, [$nonEmptyLowercase])); | ||
|
||
assertType('lowercase-string&non-falsy-string', vsprintf('%s', [$nonFalsyLowercase])); | ||
assertType('lowercase-string&numeric-string', vsprintf('%d', [$nonFalsyLowercase])); | ||
assertType('non-falsy-string', vsprintf($format, [$nonFalsyLowercase])); | ||
assertType('lowercase-string&non-falsy-string', vsprintf($formatLower, [$nonFalsyLowercase])); | ||
assertType('lowercase-string&non-empty-string', vsprintf($nonFalsyLowercase, [$nonFalsyLowercase])); | ||
assertType('string', vsprintf($string, [$nonFalsyLowercase])); | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
I wonder this line is also missing lowercase handling
maybe instead of fiddling with this complex loop and all its paths, just add it once here:
Uh oh!
There was an error while loading. Please reload this page.
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.
I shouldn't add lowercase handling here and just rely on
toString
.I'll open a PR to update
IntegerType::toString
to fix https://phpstan.org/r/79f89e3e-f19f-4a44-bd58-63f3c77db487