-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cb6de45
Handle lowercase string in sprintf
VincentLanglet 69a8b53
Fix
VincentLanglet c8d48b4
More tests
VincentLanglet 86ddf78
Improve
VincentLanglet b52f1b5
Fix
VincentLanglet b22c45b
Simplify
VincentLanglet bb3a706
Fix cs
VincentLanglet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
tests/PHPStan/Analyser/nsrt/lowercase-string-sprintf.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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