Skip to content
Closed
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
6 changes: 5 additions & 1 deletion src/Rules/Functions/PrintfPlaceholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public function doesArgumentTypeMatchPlaceholder(Type $argumentType, bool $stric
return (new IntegerType())->accepts($argumentType, true)->yes();
case 'int':
return $strictPlaceholderTypes
? (new IntegerType())->accepts($argumentType, true)->yes()
? (new UnionType([
new IntegerType(),
// numeric-string is allowed for consistency with the float case and phpstan-strict-rules.
new IntersectionType([new StringType(), new AccessoryNumericStringType()]),
]))->accepts($argumentType, true)->yes()
: ! $argumentType->toInteger() instanceof ErrorType;
case 'float':
return $strictPlaceholderTypes
Expand Down
4 changes: 0 additions & 4 deletions tests/PHPStan/Rules/Functions/PrintfParameterTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,6 @@ public function testStrict(): void
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%d"), string given.',
36,
],
[
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%d"), string given.',
37,
],
[
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%d"), null given.',
38,
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Functions/data/printf-param-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,12 @@ public function __toString(): string
printf('%s %2$*s', 'a', 5, 'a');
printf('%1$-+\'X10.2f', 5);
printf('%1$*.*f %s %2$d', 5, 6, new FooStringable()); // 5.000000 foo 6

// Bug #13609 - numeric-string should be accepted by %d placeholder
/** @param numeric-string $numericStr */
function testNumericStringWithIntPlaceholder(string $numericStr): void {
printf('%d', $numericStr);
printf('%04d-%02d-%02d', $numericStr, $numericStr, $numericStr);
sprintf('%d', $numericStr);
printf('%f', $numericStr);
}
Loading