Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/Rules/Functions/PrintfHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ public function __construct(private PhpVersion $phpVersion)

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

/** @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);
return $this->parsePlaceholders(self::PRINTF_SPECIFIER_PATTERN, $format, false);
}

public function getScanfPlaceholdersCount(string $format): ?int
{
return $this->getPlaceholdersCount('(?<specifier>[cdDeEfinosuxX%s]|\[[^\]]+\])', $format);
return $this->getPlaceholdersCount('(?<specifier>[cdDeEfinosuxX%s]|\[[^\]]+\])', $format, true);
}

/**
* @phpstan-return array<int, non-empty-list<PrintfPlaceholder>>|null parameter index => placeholders
*/
private function parsePlaceholders(string $specifiersPattern, string $format): ?array
private function parsePlaceholders(string $specifiersPattern, string $format, bool $isScanf): ?array
{
$addSpecifier = '';
if ($this->phpVersion->supportsHhPrintfSpecifier()) {
Expand Down Expand Up @@ -72,6 +72,10 @@ private function parsePlaceholders(string $specifiersPattern, string $format): ?
$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 @@ -132,9 +136,9 @@ private function getAcceptingTypeBySpecifier(string $specifier): string
return 'mixed';
}

private function getPlaceholdersCount(string $specifiersPattern, string $format): ?int
private function getPlaceholdersCount(string $specifiersPattern, string $format, bool $isScanf): ?int
{
$placeholdersMap = $this->parsePlaceholders($specifiersPattern, $format);
$placeholdersMap = $this->parsePlaceholders($specifiersPattern, $format, $isScanf);
if ($placeholdersMap === null) {
return null;
}
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/nsrt/sscanf.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ function fooo(string $s) {

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

function sscanfSuppression(string $s) {
// %* means assignment suppression - these should not appear in return array
assertType('array{int|null}|null', sscanf($s, '%*s %d'));
assertType('array{string|null}|null', sscanf($s, '%*d %s'));
assertType('array{int|null}|null', sscanf($s, '%*[a-z]%d'));
}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Functions/PrintfParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,9 @@ public function testBug8774(): void
$this->analyse([__DIR__ . '/data/bug-8774.php'], []);
}

public function testBug10260(): void
{
$this->analyse([__DIR__ . '/data/bug-10260.php'], []);
}

}
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-10260.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Bug10260;

// %* in scanf means "assignment suppression" - match but don't store
// These should NOT count as placeholders

// From the issue: %*[a-z] matches lowercase letters but doesn't assign
sscanf('appletone_day_1', '%*[a-z]_day_%s', $day_number);

// %*d means match an integer but don't assign it
sscanf('123 456', '%*d %d', $number);

// Multiple suppressed placeholders
sscanf('foo 123 bar', '%*s %*d %s', $str);

// Suppressed with character class
sscanf('ABC123', '%*[A-Z]%d', $num);

// Mix of suppressed and non-suppressed
sscanf('hello world 42', '%s %*s %d', $word, $num2);

// fscanf with suppression
fscanf(STDIN, '%*d %s', $value);

// No values needed when all are suppressed (returned as array)
sscanf('123 abc', '%*d %*s');
Loading