Skip to content

Commit 6f6c561

Browse files
phpstan-botclaude
authored andcommitted
Clarify ternary branch-inspection test: benevolent vs strict string|false
The previous `falsePositive` case used `stream_get_contents()`, which returns a *benevolent* string|false. PHPStan intentionally accepts benevolent unions for a string parameter, so the equivalent direct call is error-free too — and reporting the branch would re-introduce the very pg_escape_bytea false positive this branch inspection guards against (its second `string` parameter is fed the same `stream_get_contents()` benevolent union). Document that no-error case precisely and add a companion `strictFalseBranchReported` showing that a *strict* (non-benevolent) string|false branch under the same asymmetric `is_resource()` narrowing IS reported. This makes branch inspection's consistency with PHPStan's standard accept semantics explicit. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent d676451 commit 6f6c561

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

‎tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2989,6 +2989,10 @@ public function testBug11281(): void
29892989
'Parameter #1 $i of function Bug11281Functions\sayHello expects int, string given.',
29902990
35,
29912991
],
2992+
[
2993+
'Parameter #1 $s of function Bug11281Functions\expectsString expects string, string|false given.',
2994+
64,
2995+
],
29922996
]);
29932997
}
29942998

‎tests/PHPStan/Rules/Functions/data/bug-11281.php‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,30 @@ function expectsString(string $s): void
3939
{
4040
}
4141

42-
function falsePositive(mixed $value): void
42+
function benevolentBranchNotReported(mixed $value): void
4343
{
44-
// is_resource() only narrows asymmetrically (@phpstan-assert-if-true), so the
45-
// else branch must keep the type the ternary actually produces (mixed, accepted),
46-
// not a spurious narrowing. No error should be reported here.
44+
// stream_get_contents() returns a *benevolent* string|false. PHPStan intentionally
45+
// accepts benevolent unions for a string parameter, so the equivalent direct call
46+
// expectsString(stream_get_contents($r)) is error-free too — reporting it here would
47+
// re-introduce the pg_escape_bytea false positive this branch inspection guards
48+
// against. is_resource() also narrows asymmetrically (@phpstan-assert-if-true only),
49+
// so the else branch must keep the type the ternary actually produces (mixed,
50+
// accepted) instead of a spurious narrowing. No error should be reported here.
4751
expectsString(
4852
is_resource($value)
4953
? stream_get_contents($value)
5054
: $value,
5155
);
5256
}
57+
58+
function strictFalseBranchReported(mixed $value, string|false $sf): void
59+
{
60+
// A *strict* (non-benevolent) string|false branch is not accepted by the string
61+
// parameter, so branch inspection reports it even though is_resource() narrows
62+
// asymmetrically and the ternary's resulting type normalizes to mixed.
63+
expectsString(
64+
is_resource($value)
65+
? $sf
66+
: $value,
67+
);
68+
}

0 commit comments

Comments
 (0)