-
Notifications
You must be signed in to change notification settings - Fork 534
add optional strict check for printf parameter types #4349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ | |
class PrintfParameterTypeRuleTest extends RuleTestCase | ||
{ | ||
|
||
private bool $checkStrictPrintfPlaceholderTypes = false; | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$reflectionProvider = $this->createReflectionProvider(); | ||
|
@@ -30,6 +32,7 @@ protected function getRule(): Rule | |
true, | ||
false, | ||
), | ||
$this->checkStrictPrintfPlaceholderTypes, | ||
); | ||
} | ||
|
||
|
@@ -111,4 +114,139 @@ public function test(): void | |
]); | ||
} | ||
|
||
public function testStrict(): void | ||
{ | ||
$this->checkStrictPrintfPlaceholderTypes = true; | ||
$this->analyse([__DIR__ . '/data/printf-param-types.php'], [ | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%d"), PrintfParamTypes\\FooStringable given.', | ||
15, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%d"), int|PrintfParamTypes\\FooStringable given.', | ||
16, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be float by placeholder #1 ("%f"), PrintfParamTypes\\FooStringable given.', | ||
17, | ||
], | ||
[ | ||
'Parameter #2 of function sprintf is expected to be int by placeholder #1 ("%d"), PrintfParamTypes\\FooStringable given.', | ||
18, | ||
], | ||
[ | ||
'Parameter #3 of function fprintf is expected to be float by placeholder #1 ("%f"), PrintfParamTypes\\FooStringable given.', | ||
19, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%*s" (width)), string given.', | ||
20, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%*s" (width)), float given.', | ||
21, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%*s" (width)), SimpleXMLElement given.', | ||
22, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%*s" (width)), null given.', | ||
23, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%*s" (width)), true given.', | ||
24, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%.*s" (precision)), string given.', | ||
25, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #2 ("%3$.*s" (precision)), string given.', | ||
26, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be float by placeholder #1 ("%1$-\'X10.2f"), PrintfParamTypes\\FooStringable given.', | ||
27, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be float by placeholder #2 ("%1$*.*f" (value)), PrintfParamTypes\\FooStringable given.', | ||
28, | ||
], | ||
[ | ||
'Parameter #4 of function printf is expected to be float by placeholder #1 ("%3$f"), PrintfParamTypes\\FooStringable given.', | ||
29, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be float by placeholder #1 ("%1$f"), PrintfParamTypes\\FooStringable given.', | ||
30, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #2 ("%1$d"), PrintfParamTypes\\FooStringable given.', | ||
30, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%1$*d" (width)), float given.', | ||
31, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%1$*d" (value)), float given.', | ||
31, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%d"), float given.', | ||
34, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%d"), float|int given.', | ||
35, | ||
], | ||
[ | ||
'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, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%d"), true given.', | ||
39, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be int by placeholder #1 ("%d"), SimpleXMLElement given.', | ||
40, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be float by placeholder #1 ("%f"), string given.', | ||
42, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be float by placeholder #1 ("%f"), null given.', | ||
43, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be float by placeholder #1 ("%f"), true given.', | ||
44, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be float by placeholder #1 ("%f"), SimpleXMLElement given.', | ||
45, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be __stringandstringable by placeholder #1 ("%s"), null given.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @schlndh @ondrejmirtes After enabling this rule I noticed a lot of errors related to Personally, I think replacing those See https://3v4l.org/TlHU9#v8.4.13 I know this is a strict rule, but wonder if this was really intentional or something that should be reconsidered? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like it's useful that PHPStan lets you know you're potentially passing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I intentionally implemented it like that to match my understanding of "strict". However, I encountered the same thing: hundreds of issues like this, most of which I don't care about (though I'm fine with letting them all fall into the baseline). So I would be open to changing it. I'm just not sure what's the best way to go about it. Strict-rules do allow On the other hand, there are third-party rules (e.g. shipmonk rules) which do prohibit it, so clearly there is also interest in the fully strict version. Another thing to consider is whether it should be allowed only in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is an excellent point.
I would say only for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An idea: string|null should be reported only on level 8+, basicallh RuleLevelHelper should be utilized. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, that won't help me (level 9 🙈)
|
||
47, | ||
], | ||
[ | ||
'Parameter #2 of function printf is expected to be __stringandstringable by placeholder #1 ("%s"), true given.', | ||
48, | ||
], | ||
]); | ||
} | ||
|
||
} |