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
30 changes: 20 additions & 10 deletions src/Rules/Functions/PrintfHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use Nette\Utils\Strings;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Php\PhpVersion;
use ValueError;
use function array_filter;
use function array_keys;
use function count;
use function in_array;
use function max;
use function sprintf;
use function sscanf;
use function strlen;
use const PREG_SET_ORDER;

Expand All @@ -26,24 +28,36 @@ public function __construct(private PhpVersion $phpVersion)

public function getPrintfPlaceholdersCount(string $format): ?int
{
return $this->getPlaceholdersCount(self::PRINTF_SPECIFIER_PATTERN, $format, false);
return $this->getPlaceholdersCount(self::PRINTF_SPECIFIER_PATTERN, $format);
}

/** @phpstan-return array<int, non-empty-list<PrintfPlaceholder>> parameter index => placeholders */
public function getPrintfPlaceholders(string $format): ?array
{
return $this->parsePlaceholders(self::PRINTF_SPECIFIER_PATTERN, $format, false);
return $this->parsePlaceholders(self::PRINTF_SPECIFIER_PATTERN, $format);
}

public function getScanfPlaceholdersCount(string $format): ?int
{
return $this->getPlaceholdersCount('(?<specifier>[cdDeEfinosuxX%s]|\[[^\]]+\])', $format, true);
if ($this->phpVersion->throwsValueErrorForInternalFunctions()) {
try {
$result = sscanf('', '%*n' . $format);
} catch (ValueError) {
return null;
}
} else {
$result = @sscanf('', '%*n' . $format);
}
if ($result === null) {
return null;
}
return count($result);
}

/**
* @phpstan-return array<int, non-empty-list<PrintfPlaceholder>>|null parameter index => placeholders
*/
private function parsePlaceholders(string $specifiersPattern, string $format, bool $isScanf): ?array
private function parsePlaceholders(string $specifiersPattern, string $format): ?array
{
$addSpecifier = '';
if ($this->phpVersion->supportsHhPrintfSpecifier()) {
Expand Down Expand Up @@ -72,10 +86,6 @@ private function parsePlaceholders(string $specifiersPattern, string $format, bo
$showValueSuffix = false;

if (isset($placeholder['width']) && $placeholder['width'] !== '') {
if ($isScanf) {
// In scanf, * means assignment suppression - skip this placeholder entirely
continue;
}
$parsedPlaceholders[] = new PrintfPlaceholder(
sprintf('"%s" (width)', $placeholder[0]),
$parameterIdx++,
Expand Down Expand Up @@ -136,9 +146,9 @@ private function getAcceptingTypeBySpecifier(string $specifier): string
return 'mixed';
}

private function getPlaceholdersCount(string $specifiersPattern, string $format, bool $isScanf): ?int
private function getPlaceholdersCount(string $specifiersPattern, string $format): ?int
{
$placeholdersMap = $this->parsePlaceholders($specifiersPattern, $format, $isScanf);
$placeholdersMap = $this->parsePlaceholders($specifiersPattern, $format);
if ($placeholdersMap === null) {
return null;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Functions/PrintfParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public function testFile(): void
'Call to sprintf contains 2 placeholders, 1 value given.',
29,
],
[
'Call to sscanf contains an invalid placeholder.',
38,
],
[
'Call to fscanf contains an invalid placeholder.',
39,
],
[
'Call to sprintf contains 2 placeholders, 1 value given.',
45,
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Rules/Functions/data/printf.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
sscanf($str, "%20[^abcde]a%d", $string, $number); // ok
printf("%.E", 3.14159); // ok
sprintf("%.E", 3.14159); // ok
sscanf($str, '%.E', $number); // ok
fscanf($str, '%.E', $number); // ok
sscanf($str, '%.E', $number); // bad scan conversion character '.'
fscanf($resource, '%.E', $number); // bad scan conversion character '.'
sscanf($str, '%[A-Z]%d', $char, $number); // ok
sprintf('%s %s %s', ...[1]); // do not detect unpacked arguments
sprintf('%s %s %s', ...[1, 2, 3]); // ok
Expand Down
Loading