Skip to content

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
phpstan:2.3.xfrom
spawnia:identical-comparison-type-specifying-extensions
Open

spawnia wants to merge 4 commits into
phpstan:2.3.xfrom
spawnia:identical-comparison-type-specifying-extensions

Conversation

@spawnia

@spawnia spawnia commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

preg_match(...) === 1 was forwarded to the function's type-specifying extensions only when the function was literally named preg_match. Wrappers such as Safe\preg_match therefore kept $matches as array{}|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 / == 1 equal the call's truthiness, so the truthy narrowing is delegated. !== 1 reaches this through the negated context. Other constants, types such as int|false, and native preg_match are 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, and f(...) == 0 staying 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

…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
spawnia marked this pull request as ready for review November 13, 2026 11:08
@phpstan-bot

Copy link
Copy Markdown
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
@spawnia

spawnia commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Notes on the remaining red checks:

  • glpi: this PR causes it, and it is the intended effect. src/MailCollector.php uses Safe\preg_match(...) === 1, so $matches is now narrowed exactly, and glpi's defensive ?? 0 is reported 8 times as nullCoalesce.offset. e2e/integration/glpi-baseline.neon in phpstan/phpstan needs these errors added.
  • phpstan-laravel tests, phpstan-dba tests: these fail on other PRs too, for example https://github.com/phpstan/phpstan-src/actions/runs/44054648388.
  • Benchmarks: bug-4300.php peak memory is 322.2 KiB instead of 258.2 KiB, while instructions are unchanged (29.20M). The file has no === 1 or == 1. Locally, the same peak-memory measurement on 2.3.x and on this branch merged into 2.3.x gives the same peak, so I could not find a cause in this change.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants