Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Type/Php/SscanfFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Rules\Functions\PrintfHelper;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
Expand All @@ -15,6 +17,8 @@
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand All @@ -26,6 +30,13 @@
final class SscanfFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

public function __construct(
private PrintfHelper $printfHelper,
private PhpVersion $phpVersion,
)
{
}

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array($functionReflection->getName(), ['sscanf', 'fscanf'], true);
Expand All @@ -48,6 +59,11 @@ public function getTypeFromFunctionCall(
return null;
}

$placeholderCount = $this->printfHelper->getScanfPlaceholdersCount($formatType->getValue());
if ($placeholderCount === null) {
return $this->phpVersion->throwsValueErrorForInternalFunctions() ? new NeverType() : new NullType();
}

if (preg_match_all('/%(\d*)(\[[^\]]+\]|[cdeEfosux]{1})/', $formatType->getValue(), $matches) > 0) {
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();

Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ private static function findTestFiles(): iterable
yield __DIR__ . '/../Rules/Variables/data/bug-14124.php';
yield __DIR__ . '/../Rules/Variables/data/bug-14124b.php';
yield __DIR__ . '/../Rules/Arrays/data/bug-14308.php';

if (PHP_VERSION_ID < 80000) {
yield __DIR__ . '/data/sscanf-php74.php';
} else {
yield __DIR__ . '/data/sscanf-php80.php';
}
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/data/sscanf-php74.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace SscanfPHP74;

use function PHPStan\Testing\assertType;

function sscanfInvalidFormatMixingPositionalWithSequential(string $s) {
assertType('null', sscanf($s, '%1$s %s'));
}
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/data/sscanf-php80.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace SscanfPHP80;

use function PHPStan\Testing\assertType;

function sscanfInvalidFormatMixingPositionalWithSequential(string $s) {
assertType('*NEVER*', sscanf($s, '%1$s %s'));
}
Loading