Skip to content
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

infer non-falsy-string in sscanf() #1662

Merged
merged 1 commit into from
Sep 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Type/Php/SscanfFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
Expand Down Expand Up @@ -54,10 +55,17 @@ public function getTypeFromFunctionCall(

$type = new StringType();
if ($length !== '') {
$type = new IntersectionType([
$type,
new AccessoryNonEmptyStringType(),
]);
if (((int) $length) > 1) {
$type = new IntersectionType([
$type,
new AccessoryNonFalsyStringType(),
]);
} else {
$type = new IntersectionType([
$type,
new AccessoryNonEmptyStringType(),
]);
}
}

if (in_array($specifier, ['d', 'o', 'u', 'x'], true)) {
Expand Down
10 changes: 8 additions & 2 deletions tests/PHPStan/Analyser/data/sscanf.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ function fscanfFormatInference($r) {
assertType('int|null', $year);
}

function fooo() {
function fooo(string $s) {
// %0s returns the whole string
assertType('array{non-empty-string|null}|null', sscanf( "0" , "%0s"));
assertType('array{non-empty-string|null}|null', sscanf( "123456" , "%0s"));
Copy link
Contributor Author

@staabm staabm Aug 31, 2022

Choose a reason for hiding this comment

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

actually for a non-falsy-string $string this line could lead to a more precise 'array{non-falsy-string|null}|null', but I feel like %0s as a format is more or less a theoretical thing.. never seen it used anywhere.. I think people would use %s instead.

I figured its not important enough to complicate the extension for it

assertType('array{non-empty-string|null}|null', sscanf( "123456" , "%3s"));

assertType('array{non-empty-string|null}|null', sscanf( "123456" , "%1s"));
assertType('array{non-falsy-string|null}|null', sscanf( "123456" , "%2s"));
assertType('array{non-falsy-string|null}|null', sscanf( "123456" , "%3s"));

assertType('array{int|null, int|null, int|null}|null', sscanf('00ccff', '%2x%2x%2x'));
}