Skip to content

Commit 806f1a1

Browse files
schlndhondrejmirtes
authored andcommitted
relax strict printf parameter check
1 parent 53439c2 commit 806f1a1

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/Rules/Functions/PrintfPlaceholder.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
namespace PHPStan\Rules\Functions;
44

55
use PHPStan\ShouldNotHappenException;
6+
use PHPStan\Type\Accessory\AccessoryNumericStringType;
67
use PHPStan\Type\ErrorType;
78
use PHPStan\Type\FloatType;
89
use PHPStan\Type\IntegerType;
10+
use PHPStan\Type\IntersectionType;
11+
use PHPStan\Type\NullType;
912
use PHPStan\Type\StringAlwaysAcceptingObjectWithToStringType;
13+
use PHPStan\Type\StringType;
1014
use PHPStan\Type\Type;
1115
use PHPStan\Type\TypeCombinator;
1216

@@ -34,7 +38,11 @@ public function doesArgumentTypeMatchPlaceholder(Type $argumentType, bool $stric
3438
: ! $argumentType->toInteger() instanceof ErrorType;
3539
case 'float':
3640
return $strictPlaceholderTypes
37-
? (new FloatType())->accepts($argumentType, true)->yes()
41+
? TypeCombinator::union(
42+
new FloatType(),
43+
// numeric-string is allowed for consistency with phpstan-strict-rules.
44+
new IntersectionType([new StringType(), new AccessoryNumericStringType()]),
45+
)->accepts($argumentType, true)->yes()
3846
: ! $argumentType->toFloat() instanceof ErrorType;
3947
case 'string':
4048
case 'mixed':
@@ -46,6 +54,8 @@ public function doesArgumentTypeMatchPlaceholder(Type $argumentType, bool $stric
4654
new StringAlwaysAcceptingObjectWithToStringType(),
4755
// float also accepts int.
4856
new FloatType(),
57+
// null is allowed for consistency with phpstan-strict-rules (e.g. $string . $null).
58+
new NullType(),
4959
)->accepts($argumentType, true)->yes();
5060
// Without this PHPStan with PHP 7.4 reports "...should return bool but return statement is missing."
5161
// Presumably, because promoted properties are turned into regular properties and the phpdoc isn't applied to the property.

tests/PHPStan/Rules/Functions/PrintfParameterTypeRuleTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,6 @@ public function testStrict(): void
222222
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%d"), SimpleXMLElement given.',
223223
40,
224224
],
225-
[
226-
'Parameter #2 of function printf is expected to be float by placeholder #1 ("%f"), string given.',
227-
42,
228-
],
229225
[
230226
'Parameter #2 of function printf is expected to be float by placeholder #1 ("%f"), null given.',
231227
43,
@@ -238,10 +234,6 @@ public function testStrict(): void
238234
'Parameter #2 of function printf is expected to be float by placeholder #1 ("%f"), SimpleXMLElement given.',
239235
45,
240236
],
241-
[
242-
'Parameter #2 of function printf is expected to be string by placeholder #1 ("%s"), null given.',
243-
47,
244-
],
245237
[
246238
'Parameter #2 of function printf is expected to be string by placeholder #1 ("%s"), true given.',
247239
48,

tests/PHPStan/Rules/Functions/data/printf-param-types.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public function __toString(): string
3939
printf('%d', true);
4040
printf('%d', new \SimpleXMLElement('<a>aaa</a>'));
4141

42-
printf('%f', '1.2345678901234567890123456789013245678901234567989');
42+
4343
printf('%f', null);
4444
printf('%f', true);
4545
printf('%f', new \SimpleXMLElement('<a>aaa</a>'));
4646

47-
printf('%s', null);
47+
4848
printf('%s', true);
4949

5050
// Error, but already reported by CallToFunctionParametersRule
@@ -55,6 +55,10 @@ public function __toString(): string
5555
printf('%s');
5656
printf('%s', 1, 2);
5757

58+
// Arguable
59+
printf('%f', '1.2345678901234567890123456789013245678901234567989');
60+
printf('%s', null);
61+
5862
// OK
5963
printf('%s', 'a');
6064
printf('%s', new FooStringable());

0 commit comments

Comments
 (0)