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 placeholder.
new IntersectionType([new StringType(), new AccessoryNumericStringType()]),
]))->accepts($argumentType, true)->yes()
: ! $argumentType->toInteger() instanceof ErrorType;
case 'float':
return $strictPlaceholderTypes
Expand Down
15 changes: 11 additions & 4 deletions tests/PHPStan/Rules/Functions/PrintfParameterTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ public function test(): void
]);
}

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

public function testBug13609Strict(): void
{
$this->checkStrictPrintfPlaceholderTypes = true;
$this->analyse([__DIR__ . '/data/bug-13609.php'], []);
}

public function testStrict(): void
{
$this->checkStrictPrintfPlaceholderTypes = true;
Expand Down Expand Up @@ -206,10 +217,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
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-13609.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace Bug13609;

class Datehelper
{
/**
* @param numeric-string $year
* @param numeric-string $month
* @param numeric-string $day
*/
public static function dateFormat(string $year, string $month, string $day): string
{
return \sprintf('%04d-%02d-%02d', $year, $month, $day);
}

/**
* @param numeric-string $value
*/
public static function formatFloat(string $value): string
{
return \sprintf('%f', $value);
}
}
Loading