Repository navigation
Forward f(...) === 1 to type-specifying extensions for any function whose only truthy result is 1 - #7056
Open
spawnia wants to merge 4 commits into
Open
Forward f(...) === 1 to type-specifying extensions for any function whose only truthy result is 1#7056spawnia wants to merge 4 commits into
f(...) === 1 to type-specifying extensions for any function whose only truthy result is 1#7056spawnia wants to merge 4 commits into
Conversation
…fying extension
The identity comparison `preg_match(...) === 1` was forwarded to the
function's type-specifying extensions only when the function was literally
named `preg_match`, so wrappers such as `Safe\preg_match` kept `$matches` at
`array{}|array{...}`.
Forward `f(...) === 1` for any function whose call result is only truthy when
it is 1 (e.g. `0|1` or `0|1|false`), where the comparison is equivalent to the
call's truthiness. Other constants and result types are unchanged.
🤖 Generated with Claude Code
spawnia
marked this pull request as ready for review
November 13, 2026 11:08
Collaborator
|
This pull request has been marked as ready for review. |
…HP 7 - Port the type check to turbo-ext/src/IdenticalNarrowingHelper.cpp for both the === and == paths and regenerate its declarations - Skip the new type inference test below PHP 8.0, as its data file uses mixed and union types - Cover constants that include 1 and @phpstan-assert-if-true functions 🤖 Generated with Claude Code
`f(...) == 1` now checks the literal value and forwards to the strict comparison in every context, which applies its own context check. `isOnlyTruthyResultOne()` asks whether the call can be truthy instead of whether it can be 1, which is the same for the types that reach it. Tests cover `(f(...) !== 1) === true`, a call that is never truthy, and `f(...) == 0` staying loose. 🤖 Generated with Claude Code
Infection only runs tests that cover a mutated line, and code run from a data provider records no coverage, so the narrowing in IdenticalNarrowingHelper looked untested. 🤖 Generated with Claude Code
Contributor
Author
|
Notes on the remaining red checks:
🤖 Generated with Claude Code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
preg_match(...) === 1was forwarded to the function's type-specifying extensions only when the function was literally namedpreg_match. Wrappers such asSafe\preg_matchtherefore kept$matchesasarray{}|array{...}, while the truthy form narrows exactly. See thecodingmachine/phpstan-safe-rule#57.The name check came from d842380 without a stated reason. This replaces it with a check on the call's result type: when 1 is the only truthy value (
0|1,0|1|false),f(...) === 1/== 1equal the call's truthiness, so the truthy narrowing is delegated.!== 1reaches this through the negated context. Other constants, types such asint|false, and nativepreg_matchare unchanged.Tests use a stub function type-specifying extension and fail without the change. They cover
===,==, reversed operands,!==, and the unchanged cases=== 0,=== 2,int|false, a call that is never truthy,(f(...) !== 1) === true, andf(...) == 0staying loose.The turbo extension mirrors the change in
turbo-ext/src/IdenticalNarrowingHelper.cpp. Built against PHP 8.3, the analyser and comparison rule tests pass with it loaded.🤖 Generated with Claude Code