Skip to content

Commit

Permalink
support fscanf(), because of same formatting rules
Browse files Browse the repository at this point in the history
  • Loading branch information
clxmstaab authored and ondrejmirtes committed Jun 17, 2022
1 parent 753b157 commit 4ed94b7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Type/Php/SscanfFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SscanfFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnT

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'sscanf';
return in_array($functionReflection->getName(), ['sscanf', 'fscanf'], true);
}

public function getTypeFromFunctionCall(
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 @@ -2,15 +2,14 @@

namespace Sscanf;

use sscanf;
use function PHPStan\Testing\assertType;

function foo(string $s) {
assertType('int|null', sscanf($s, $s, $first, $second));
assertType('array|null', sscanf($s, $s));
}

function bar(string $s) {
function sscanfFormatInference(string $s) {
assertType('int|null', sscanf('20-20', '%d-%d', $first, $second));
assertType('array{int, int}|null', sscanf('20-20', '%d-%d'));

Expand All @@ -30,3 +29,10 @@ function bar(string $s) {
assertType('int', $day);
assertType('int', $year);
}

function fscanfFormatInference($r) {
list($month, $day, $year) = fscanf($r, "%s %d %d");
assertType('string', $month);
assertType('int', $day);
assertType('int', $year);
}

0 comments on commit 4ed94b7

Please sign in to comment.